Analytics Script

The Analytics script is the easiest way to measure user activity in your browser applications. Once Analytics is enabled, you can copy the script URL from the Analytics settings page.

Including the script

Once you have the URL at hand, you can include the script in your application by adding the following snippet to your HTML:

<script src="script-url-here" async></script>
html

Initializing the script

Once the script is loaded, you can call the init function to initialize the script.

window.analytics.init({
  environmentId: "environment-id-here",
})
js

Tracking user activity

While the Analytics script automatically tracks page views, you may want to add additional events to track user activity. You can do this by calling the push function.

window.analytics.push({
  category: "category-here",
  kind: "kind-here",
})
js

API

Once loaded, the analytics property is attached to the global window object.

declare global {
  interface Window {
    analytics?: {
      init: (config: { environmentId: string }) => void;
      identify: (userIdentityId: string) => void;
      push: (event: { category: string; kind: string }) => void;
    };
  }
}
typescript

init

The init function initializes the Analytics script. It must be called before any other functions.

identify

The identify function links the current Analytics user to a User Management user identity. This allows you to identify users across devices and browsers.

push

The push function pushes an event to the Anzu Analytics API. The event will be associated with the current Analytics user and session.