Context & shutdown
Set per-request identity once with setContext() so capture() calls stay clean. Always shutdown() before the process exits.
Set per-request identity with setContext() so you don't repeat distinctId/anonymousId on every capture(). Always call shutdown() before process exit to flush queued events.
#setContext / clearContext
Set distinctId and optional anonymousId from session or cookie at the start of a request; clear at the end.
vtilt.setContext({
distinctId: req.user?.id,
anonymousId: req.cookies?.vt_anon,
})
vtilt.capture({ event: 'page_view', properties: { path: req.path } })
vtilt.clearContext()typescript
In Express / Fastify, wrap this in middleware so every handler downstream inherits the identity. In Next.js route handlers, set context at the top of the handler, run your logic, and call clearContext() in a finally block.
#flush & shutdown
flush() sends the queue immediately. shutdown() flushes and stops the timer (use before process exit).
await vtilt.flush() // optional: send now
await vtilt.shutdown(30_000) // flush + stop, 30s timeouttypescript
| Method | When to call | Notes |
|---|---|---|
flush() | Right before a known boundary (e.g. webhook response, lambda return) where queued events would otherwise be lost. | Idempotent. Safe to call multiple times. |
shutdown(timeoutMs) | Process termination — SIGINT, SIGTERM, lambda extension shutdown, container stop. | Stops the internal flush timer. Subsequent calls are no-ops. |