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

# Event Tracking Overview

> Send events from your MCP server to the Waniwani dashboard.

> [!NOTE]
>
> **Platform feature.** Requires `WANIWANI_API_KEY`. Works whether your MCP server is self-hosted or on Managed Hosting. [About the Platform](https://docs.waniwani.ai/sdk/platform/overview.md).

The tracking module ships events from your MCP server to the Waniwani backend. You call `client.track()`, the SDK enqueues the event in memory, and a background transport batches and posts it to `POST /api/mcp/events/v2/batch`. Tracking never blocks your tool handler.

## Mental model

> [card] [Events](https://docs.waniwani.ai/sdk/tracking/events.md)
>
> A typed record of something that happened (a tool call, a quote, a purchase). Has a name, properties, and correlation ids.

> [card] [Sessions](https://docs.waniwani.ai/sdk/tracking/sessions.md)
>
> A session groups events from one conversation. On every SDK surface except the top-level backend client, the session id attaches to events automatically.

> [card] [Widgets & chat](https://docs.waniwani.ai/sdk/tracking/widgets.md)
>
> The same `track` client in the browser: `useWaniwani()` in MCP-app widgets, `chat.track` on chat host pages. Session identity included.

> [card] [Identify](https://docs.waniwani.ai/sdk/tracking/identify.md)
>
> Attach an `externalUserId` to the current session so events get linked to a real user.

> [card] [Automatic tool tracking](https://docs.waniwani.ai/sdk/configuration/wrap-server.md)
>
> `withWaniwani(server)` wraps every tool handler and emits `tool.called` for you with timing and status.

## Track your first event

```ts theme={null}
import { waniwani } from "@waniwani/sdk";

const wani = waniwani(); // reads WANIWANI_API_KEY from env

// Inside a tool handler
await wani.track.priceShown({
  amount: 1299,
  currency: "EUR",
  meta: extra._meta, // associates the event with the current session
});

// In serverless runtimes, flush before the function returns
await wani.flush();
```

That is the whole happy path. Everything else on the following pages is detail.

## How the transport behaves

When you call `track()`, the SDK:

1. Normalizes the input into a canonical Events API V2 envelope (see [events-api-v2-contract](https://github.com/waniwani-ai/sdk)).
2. Generates a deterministic event id (`evt_<uuid>`).
3. Pushes the envelope on an in-memory buffer and returns `{ eventId }` immediately.
4. A background timer flushes the buffer every `flushIntervalMs` (default `1000`), or sooner when the batch fills (`maxBatchSize`, default `20`).
5. Retries transient failures (`408`, `425`, `429`, `5xx`) with exponential backoff, up to `maxRetries` (default `3`).
6. Stops retrying on `401` / `403` auth errors to avoid retry storms.

### Long-running vs. serverless

| Runtime | How to make sure events ship |
| - | - |
| Long-running Node process | Nothing. The SDK attaches `beforeExit`, `SIGINT`, and `SIGTERM` hooks that call `shutdown()` automatically. |
| Serverless / short-lived | Call `await wani.flush()` before the function returns, or pass `flushAfterToolCall: true` to `withWaniwani()`. |

## Client API

```ts theme={null}
interface TrackingClient {
  track(event: TrackInput): Promise<{ eventId: string }>;
  identify(
    userId: string,
    properties?: Record<string, unknown>,
    meta?: Record<string, unknown>,
  ): Promise<{ eventId: string }>;
  flush(): Promise<void>;
  shutdown(options?: { timeoutMs?: number }): Promise<{
    timedOut: boolean;
    pendingEvents: number;
  }>;
}
```

> [card] [track(event)](https://docs.waniwani.ai/sdk/tracking/events.md)
>
> Enqueue an event.

> [card] [identify(userId, properties?, meta?)](https://docs.waniwani.ai/sdk/tracking/identify.md)
>
> Send a `user.identified` event and attach the user id to the session.

> [card] flush()
>
> Wait for the current buffer to drain.

> [card] shutdown(\{ timeoutMs? })
>
> Flush and stop the transport. Returns `{ timedOut, pendingEvents }`.

> [!NOTE]
>
> All four methods throw synchronously if no `apiKey` was resolved (from config or `WANIWANI_API_KEY`). If you instantiate `waniwani()` without an api key, the client is inert and calling `track()` will throw.


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