> ## Documentation Index
> Fetch the complete documentation index at: https://help.standards.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Analytics API

<Badge color="red" size="lg" shape="pill">Enterprise</Badge>

The analytics API is a JavaScript API available on all published projects. It lets your custom scripts listen to visitor interactions like page views, downloads, and more.

***

## Quickstart

In your [header or footer scripts](/custom-scripts), register a listener:

```html theme={null}
<script>
    standards.on("page_view", function (event) {
        console.log("Page viewed:", event.properties.page_path);
    });
</script>
```

Or listen to all events at once using `*`:

```html theme={null}
<script>
    standards.on("*", function (event) {
        console.log(event.event, event);
    });
</script>
```

***

## How it works

A `window.standards` object is initialized in the `<head>` of every published project, before any custom scripts run. This means your header scripts can call `standards.on()` immediately — no need to wait for a `load` or `DOMContentLoaded` event.

Events are emitted by the application after the page renders. All listeners registered by the time the page renders will receive events.

### Event shape

Every event has the same structure:

```javascript theme={null}
{
  event: 'page_view',          // Event name
  timestamp: 1707840000000,    // Unix timestamp in milliseconds
  properties: {
    page_path: '/brand',       // Current page path
    page_title: 'Brand Guide', // Current page title
    // ...additional properties depending on the event
  },
  user: {
    session_id: 'a1b2c3d4-...',  // Unique session ID (always present)
    email: 'user@company.com',   // Only if Include User Identity is enabled
    user_id: 'abc123',           // Only if Include User Identity is enabled
    ip: '192.168.1.1'            // Only if Include User Identity is enabled
  }
}
```

***

## Events

### `page_view`

Fired on initial page load and when the visitor navigates between pages.

| Property     | Type   | Description                                                    |
| ------------ | :----- | -------------------------------------------------------------- |
| `page_path`  | string | The path of the page viewed                                    |
| `page_title` | string | The title of the page viewed                                   |
| `language`   | string | The locale code of the active language (e.g. `en`,` fr`, `es`) |

### `download_file`

Fired when a visitor clicks a download link.

| Property    | Type   | Description                            |
| ----------- | ------ | -------------------------------------- |
| `file_id`   | string | The file’s unique ID                   |
| `file_name` | string | The file’s display name                |
| `file_type` | string | The file extension (e.g. `pdf`, `png`) |

### `play_video`

Fired when a visitor explicitly triggers video playback (click or hover). **Not** fired when a video autoplays on scroll.

| Property      | Type   | Description                       |
| ------------- | ------ | --------------------------------- |
| `video_id`    | string | The video file’s unique ID        |
| `video_title` | string | The video’s alt text or file name |

### `change_slide`

Fired when a visitor navigates slides in a Sequence or Toggle element. **Not** fired on autoplay transitions.

| Property      | Type   | Description                           |
| ------------- | ------ | ------------------------------------- |
| `slide_id`    | string | The slide element’s unique ID         |
| `slide_title` | string | The slide element’s name              |
| `slide_index` | number | The zero-based index of the new slide |

### `change_language`

Fired when a visitor switches the project’s language.

| Property   | Type   | Description                                      |
| ---------- | ------ | ------------------------------------------------ |
| `language` | string | The locale code selected (e.g. `en`, `fr`, `es`) |

### `copy_hex_code`

Fired when a visitor clicks a color swatch to copy its value.

| Property   | Type   | Description            |
| ---------- | ------ | ---------------------- |
| `hex_code` | string | The copied color value |

### `copy_share_link`

Fired when a visitor clicks a share link button.

| Property     | Type   | Description                                      |
| ------------ | ------ | ------------------------------------------------ |
| `share_link` | string | The URL or text that was copied to the clipboard |

***

## User identity

By default, events include only a `session_id` — a randomly generated UUID that persists for the browser session.

To include the visitor’s `email`, `user_id`, and `ip` in events, enable **Include User Identity** in the [Custom Scripts settings](/custom-scripts). This only applies to projects that require a sign in to view. Public and password-protected projects do not have user identity data available.

***

## Example

Forwarding to a Third-Party Analytics Platform

```html theme={null}
<script>
    standards.on("*", function (event) {
        fetch("https://your-analytics-endpoint.com/collect", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
                event: event.event,
                timestamp: event.timestamp,
                session_id: event.user.session_id,
                properties: event.properties,
            }),
        });
    });
</script>
```

***

## API reference

### `standards.on(event, callback)`

Register a listener for a specific event or all events.

| Parameter  | Type     | Description                                             |
| ---------- | -------- | ------------------------------------------------------- |
| `event`    | string   | Event name (e.g. `'page_view'`) or `'*'` for all events |
| `callback` | function | Called with the event object when the event fires       |

### `standards.hasGoogleAnalytics`

A boolean indicating whether the native Google Analytics integration is active on this project. Use this to avoid sending duplicate events to GA.
