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

# Track with the Web SDK

> Install the Web SDK to track players on your website, game page or web game.

Install the Web SDK to send page views, events and player identity from your website, game page or web game to Audience.

## Before you start

* Your project's publishable key, from **Game Analytics** > **Connect your game** or **Settings** > **General** in Audience. It starts with `pk_imapik-`.
* A consent prompt, if your players need one. The SDK has no consent UI of its own. See [Consent](https://docs.immutable.com/audience/track/consent.md).

## Install

```bash theme={null}
npm install @imtbl/audience
```

pnpm and yarn work too: `pnpm add @imtbl/audience` or `yarn add @imtbl/audience`.

## Start the SDK

```ts theme={null}
import { Audience } from '@imtbl/audience';

const audience = Audience.init({
  publishableKey: 'YOUR_PUBLISHABLE_KEY',
  consent: 'anonymous',
  onError: (err) => console.error(err.code, err.message),
});
```

`consent` defaults to `none`, which sends nothing. This example starts at `anonymous`, so events flow straight away without identifying the player.

## Track page views and events

```ts theme={null}
audience.page();
audience.track('purchase', { currency: 'USD', value: '9.99' });
```

Call `page()` on every page view. In a single-page app, call it on each route change. Use a predefined event name, such as `purchase`, or your own.

## Identify players

```ts theme={null}
// identify() needs 'full' consent; at 'anonymous' the SDK ignores it.
audience.setConsent('full');
audience.identify('user@example.com', 'email', { name: 'Jane' });
```

Call `identify()` when a player signs in or gives you their email. Raise consent to `full` first, and only after the player agrees: at `anonymous` the SDK drops the call and logs a warning.

## When your app closes

```ts theme={null}
audience.shutdown();
```

Call `shutdown()` when your app unmounts, so queued events are sent.

## What the SDK captures by itself

| Event | When it fires |
| - | - |
| `link_clicked` | A player clicks a link to another site |
| `form_submitted` | A player submits a form. At `full` consent it includes a hash of the email address. |
| `scroll_depth` | A player scrolls past 25%, 50%, 75%, 90% and 100% of the page |
| `button_clicked` | A player clicks a button. Off by default: set `autocapture: { buttons: true }` to turn it on. |

To turn one off, pass `autocapture` to `init`:

```ts theme={null}
const audience = Audience.init({
  publishableKey: 'YOUR_PUBLISHABLE_KEY',
  consent: 'anonymous',
  autocapture: { forms: false },
});
```

## Without a bundler

Load the SDK from Immutable's CDN with a script tag instead:

```html theme={null}
<script src="https://cdn.immutable.com/audience/v1/imtbl.js"></script>
<script>
  const { init } = window.ImmutableAudience;
  const audience = init({
    publishableKey: 'YOUR_PUBLISHABLE_KEY',
    consent: 'anonymous',
  });
  audience.page();
</script>
```

For a marketing site or landing page where you only need page views, clicks and form sign-ups, the tracking pixel needs no code beyond this snippet in your page's `<head>`:

```html theme={null}
<script>
(function(){
var w=window,i="__imtbl";
w[i]=w[i]||[];
w[i].push(["init",{"key":"YOUR_PUBLISHABLE_KEY","consent":"anonymous"}]);
var s=document.createElement("script");s.async=1;
s.src="https://cdn.immutable.com/pixel/v1/imtbl.js";
document.head.appendChild(s);
})();
</script>
```

The pixel can also read consent from your consent manager. See [Consent](https://docs.immutable.com/audience/track/consent.md#read-consent-from-your-consent-manager).

## Test your setup

While you build, pass `testMode: true` to `init`. Test events show in the event log but stay out of your boards. Pass `debug: true` to log every event to the browser console. Remove both before you go live.

## Check it's working

In Audience, open **Connections** and select the **Event log** tab: your events show there as they land. **Game Analytics** counts only events from a game, so website events don't show on its boards. See [Check your events arrive](https://docs.immutable.com/audience/track/check-events.md).

## Related

* [Consent](https://docs.immutable.com/audience/track/consent.md)
* [Check your events arrive](https://docs.immutable.com/audience/track/check-events.md)
* [Web SDK reference](https://docs.immutable.com/docs/products/audience/analytics/web-sdk.md): every option and method


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.