# Analytics

**Purpose:** this page says what the portal measures for you, and documents the SDK calls for reporting a purchase (`trackOrder`) and for your own events (`track`).

Playtime, sessions, retention, ad impressions and conversion are measured by the portal automatically. You do not instrument them and there is no SDK call for them. The [gameplay events](https://docs.zokogames.com/sdk/game#gameplay-events) are what make those numbers accurate.

The analytics module has two methods. `trackOrder` reports a purchase:

```ts
trackOrder(provider: string, order: unknown): Promise<void>
```

`provider` is a non-empty string (up to 64 characters) naming the payment provider, and `order` is that provider's order object (any value that serialises to at most 16 KiB). Both are sent to the portal. The call resolves with nothing. An empty or non-string provider, or an oversized order, rejects with `invalidArgument`; more than 20 reports a minute from one game session rejects with `rateLimited`; the general `sdkNotInitialized` and `sdkDisabled` apply too (see [errors](https://docs.zokogames.com/sdk/introduction#errors)). The portal counts each accepted call as one anonymous order event for the game (no visitor id, and a daily cap per player and game); the order object itself is not stored, because provider orders carry personal data. In local mode the call is accepted and does nothing. Purchases made through `SDK.store` are already recorded by the portal, so do not call `trackOrder` for them.

The counted orders appear, per day, under **In-game orders** in your game's analytics in the developer console, and as the `orders` field of the [game stats API](https://docs.zokogames.com/guides/developer-api) (`/games/{id}/stats` and `/games/{id}/stats/daily`). They are self-reported by your game, so they are never ZokoGames revenue or part of a payout.

```js
async function onProviderPurchase(order) {
  try {
    await SDK.analytics.trackOrder("provider-name", order);
  } catch (e) {
    console.warn("trackOrder failed:", e.code);
  }
}
```

## At a glance

Every member of `SDK.analytics`, with its signature and the SDK version that added it. The sections below describe each call's errors and how it behaves in local mode, for guests and offline.

| Signature | Resolves to | Can fail with | Since |
| --- | --- | --- | --- |
| `trackOrder(provider: string, order: unknown): Promise<void>` | Nothing, when the portal accepted the report (also in kids mode, where nothing is counted). | `invalidArgument` (empty or non-string `provider`, a provider over 64 characters, an order that cannot be copied or is over 16 KiB), `rateLimited` (more than 20 reports a minute), `timeout`, `sdkNotInitialized`, `sdkDisabled`. | 1.0.0 |
| `track(name: string, params?: Record<string, string \| number \| boolean>): void` | Nothing, always. It is fire-and-forget and never throws or rejects. | Nothing reaches the game: an invalid or over-limit call is dropped silently. | 1.7.0 |

The type `AnalyticsApi` is in the declaration file described in the SDK introduction. The module has no other member and no events.

## Custom events: `track`

```js
SDK.analytics.track("level_done", { level: 3, mode: "hard", won: true });
```

`track` records one event of your own. It returns nothing, so there is nothing to `await` or catch. Limits, checked by the SDK and again by the portal; a call that breaks any of them is dropped without an error:

- `name` matches `^[a-z][a-z0-9_]{0,39}$` (lower case, digits and underscores, starting with a letter, at most 40 characters).
- `params` is optional, has at most 10 entries, and each key matches the same pattern.
- A string value is at most 100 characters, a number must be finite (no `NaN`, no infinity), and a boolean is allowed. Nested objects and arrays are not.
- At most 60 calls a minute from one game session; the rest are dropped.

Custom events are counted only when the player has allowed analytics cookies, and never in kids mode or in a kids game. They are kept apart from the portal's own events (a separate `game_custom` kind), so a name can never collide with one. At present the portal counts that the event happened; the name and parameters stay in the browser and are not stored or shown in the developer console. In local mode the call is accepted and does nothing. Do not put personal data in an event.

**Common mistakes.**

| Mistake | Result | Fix |
| --- | --- | --- |
| `trackOrder` is called for a purchase made through `SDK.store` | The order is counted twice | Do not: the portal already records store purchases. |
| The whole provider response with card or personal data is passed as `order` | The portal does not store it, but a large object is refused | Pass only the order object, under 16 KiB, with no data you would not want in logs. |
| The call is made for every click or in a loop | `rateLimited` after 20 reports a minute | Report once per completed order. |
| The result is used as a revenue figure | The numbers are self-reported and never revenue | Use your payment provider's own records for money. |
| `await SDK.analytics.trackOrder(...)` blocks the purchase flow | A slow or offline portal delays the game by up to 10 seconds | Do not await it in the flow that delivers the item, and catch the error. |

## In local mode, as a guest, offline or ad-free

| Situation | Behaviour |
| --- | --- |
| Local mode | Argument check only: the SDK itself rejects only an empty or non-string `provider`. The call resolves and nothing is sent anywhere, so the 64-character provider limit, the 16 KiB order limit and the 20-a-minute limit are enforced by the portal only and cannot fail locally. |
| Guest | Same as a signed-in player. |
| Offline | Needs the portal like any call; it rejects with `timeout` if the portal does not answer within 10 seconds. |
| Ad blocker / ad-free | No effect. |

This call is only meaningful for games that have in-game purchases enabled, which is currently by invitation. See [in-game purchases](https://docs.zokogames.com/sdk/purchases).

## Your own analytics

You may add your own analytics tools, as long as they respect these limits:

- No third-party advertising or tracking SDKs that profile players.
- If you collect personal data beyond what the portal provides, show a clear privacy notice and link to your policy.
- Do not slow the first frame; load analytics scripts after `gameplayStart`.

## Next steps

- [In-game purchases](https://docs.zokogames.com/sdk/purchases)
- [Analytics and metrics reference](https://docs.zokogames.com/guides/analytics-metrics)
