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.
Who is this for? Web developers building web games, marketing sites, or SPAs who need explicit event tracking, user identity, or SPA support. Works with any framework (React, Next.js, Svelte, vanilla JS).
session_start, session_end) is handled automatically. Page views are tracked by calling page() on each route change, and in-game moments like progressions, purchases, and sign-ups are triggered by your code. All events flow to the same pipeline as the Tracking Pixel, Unity SDK, and REST API.
What You Need
- An Immutable Hub account with a project (get started here)
- A publishable key from your project settings (API keys guide)
Installation
- npm
- yarn
- pnpm
- CDN
Quick Start
Set consent
The SDK uses a three-tier consent model (At
'none', 'anonymous', 'full'). The SDK does not provide a consent UI. Build your own cookie banner or privacy prompt, then call setConsent with the appropriate level when the user responds.'anonymous', page views and events are tracked but identify() and alias() calls are dropped. At 'full', all methods are available. See the Data Dictionary for what is collected at each level.Track page views
Call
page() on each route change. The first call in each session includes attribution parameters.Track events
Log player actions with
track(). The SDK ships with predefined events for common player actions (purchases, sign-ups, progression, wishlist actions, and more) that give you typed properties and autocomplete. You can also pass any custom event string.- npm
- CDN
Identify users
Identifies the player by associating their userId with their activity and traits. Call at login or account connection. Requires
'full' consent.- npm
- CDN
Complete Example
- npm
- CDN
Verify the Integration
Initialize withdebug: true to see SDK activity in the browser console:
- Console: Look for log entries showing
session_start,page, and anytrack()calls - Network tab: Look for POST requests to
https://api.immutable.com(orhttps://api.sandbox.immutable.comif you’re using a test key). Events flush every 5 seconds or when 20 events accumulate
Remove
debug: true before deploying to production.Content Security Policy
If your site enforces a Content-Security-Policy header, add the event endpoint so the SDK can deliver events:If you install the SDK as a package dependency (npm/yarn/pnpm), only
connect-src is required. The CDN directive is only needed for the <script>-tag installation.nonce attribute to your inline <script> tag. The nonce covers the inline code only. The CDN-loaded script is authorized by the script-src directive above.
API Reference
Audience.init(config)
Audience.init(config)
Creates and returns an
Audience instance. Call once when your app loads to set up tracking.| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
publishableKey | string | Yes | — | API key from Hub (starts with pk_imapik- for production, pk_imapik-test- for sandbox) |
consent | 'none' | 'anonymous' | 'full' | No | 'none' | Initial consent level |
debug | boolean | No | false | Log SDK activity to the browser console |
cookieDomain | string | No | Current domain | Share cookies across subdomains (e.g. '.studio.com'). The Tracking Pixel uses domain for this same setting. |
flushInterval | number | No | 5000 | How often the queue flushes, in milliseconds |
flushSize | number | No | 20 | Number of queued messages that triggers a flush |
baseUrl | string | No | Immutable API | Override the default API base URL |
onError | (err: AudienceError) => void | No | undefined | Callback for flush and consent sync failures |
setConsent(level)
setConsent(level)
Controls what the SDK is allowed to collect. Call when the user accepts or changes their cookie preferences. Takes effect immediately, no page reload needed. See the Data Dictionary for consent levels and downgrade behavior.
canTrack(level)
canTrack(level)
Checks whether a consent level allows tracking. Returns
true when level is 'anonymous' or 'full'. Use before calling track() or page() when you need to guard behavior based on the current consent level, for example when conditionally showing a tracking-dependent UI element.| Parameter | Type | Description |
|---|---|---|
level | 'none' | 'anonymous' | 'full' | The consent level to check |
canIdentify(level)
canIdentify(level)
Checks whether a consent level allows player identification. Returns
true when level is 'full'. Use before calling identify() or alias() when you need to guard identity-dependent logic, for example to only prompt a player to link accounts when consent allows it.| Parameter | Type | Description |
|---|---|---|
level | 'none' | 'anonymous' | 'full' | The consent level to check |
page(properties?)
page(properties?)
Records a page view. Call on every route change to track which pages players visit and in what order. The first call in each session includes attribution parameters.
Requires:
| Parameter | Type | Description |
|---|---|---|
properties | Record<string, unknown> | Optional custom properties |
'anonymous' or 'full' consent. Calls at 'none' are silently dropped.track(event, properties?)
track(event, properties?)
Records a player action. Call when a player does something you want to measure, such as a purchase, sign-up, level completion, or any custom action. Predefined events give you typed properties with autocomplete.
Requires:
| Parameter | Type | Description |
|---|---|---|
event | string | An AudienceEvents value (e.g. AudienceEvents.PURCHASE) or any custom string |
properties | Record<string, unknown> | Event properties. Required for predefined events that have required fields (e.g. purchase), optional otherwise. |
'anonymous' or 'full' consent. Calls at 'none' are silently dropped.identify(id, identityType, traits?)
identify(id, identityType, traits?)
Associates the player’s userId with their activity and traits. Call at login or account connection.
Requires: Identity types:
| Parameter | Type | Description |
|---|---|---|
id | string | The player’s identifier in that provider |
identityType | IdentityType | Which provider the ID comes from (see table below) |
traits | object | Optional. email, name, or custom key-value pairs |
'full' consent.| Value | Provider |
|---|---|
'passport' | Immutable Passport |
'steam' | Steam |
'epic' | Epic Games |
'google' | |
'apple' | Apple |
'discord' | Discord |
'email' | Email address |
'custom' | Custom identity system |
alias(from, to)
alias(from, to)
Links two account IDs that belong to the same player. Call when a player connects a second account with a different provider, for example a player previously identified via Steam who later links a Passport account.
Requires:
| Parameter | Type | Description |
|---|---|---|
from | { id: string, identityType: IdentityType } | The account being linked |
to | { id: string, identityType: IdentityType } | The player’s main account |
'full' consent.reset()
reset()
Wipes the current player’s identity and starts a fresh anonymous session. Call when a player logs out so the next player on the same device isn’t mixed up.
flush()
flush()
Sends all queued events to the server immediately. Call when you need events delivered right now instead of waiting for the next automatic flush. Returns a
Promise that resolves when the batch is sent.shutdown()
shutdown()
Sends any remaining events and shuts down the SDK. Call when the app unmounts or the page is about to unload. Fires a
session_end event if consent is above 'none'.Event Delivery
- Events are batched and flushed every 5 seconds or when 20 events accumulate (configurable via
flushIntervalandflushSize) - On page unload (navigate away, close tab), the queue flushes remaining events so they are not lost
FAQ
Can I use the Tracking Pixel and Web SDK together?
Can I use the Tracking Pixel and Web SDK together?
Yes, they’re complementary, not alternatives. The Tracking Pixel handles passive capture (page views, attribution, form submissions, outbound clicks) with no code beyond a snippet. The Web SDK handles explicit events, user identity, and SPA route tracking. Use both on the same site if you need passive and custom tracking. All events flow to the same pipeline. See the Data Dictionary for the full per-event schema.
What happens if I call track() before consent is set?
What happens if I call track() before consent is set?
The call is a no-op. No events are queued or sent. Once you call
setConsent('anonymous') or higher, subsequent calls will be tracked.What browsers are supported?
What browsers are supported?
Chrome 80+, Firefox 78+, Safari 14+, Edge 80+.
How do I handle a GDPR erasure request?
How do I handle a GDPR erasure request?
Erasure is a server-side operation. Call
DELETE /v1/audience/data from your backend with the user’s userId or anonymousId. See Deleting User Data in the REST API docs for the full request shape.On the client side, call reset() after the erasure is accepted. This clears the user’s identity, wipes the event queue, and generates a fresh anonymous ID so the next session is not linked to the erased user:Next Steps
Attribution
How tracking data powers player attribution and Hub reports
Data Dictionary
Full reference of event schemas and consent levels
Tracking Pixel
Passive tracking snippet for marketing sites and landing pages
Unity SDK
In-game tracking for Unity desktop builds
REST API
Send events from your backend or game server
API Keys
Manage your publishable and secret keys