Use the server SDK where the thing happened on your server — a payment confirmed, a subscription
renewed, an invite accepted. In browser code, use @togohq/sdk-browser.
Node 20 or later; zero runtime dependencies.
Install and create one client
npm install @togohq/sdk-node
import { init } from '@togohq/sdk-node';
// One client per process, shared by every request.
export const analytics = init({ writeKey: process.env.TOGO_WRITE_KEY! });
Use the test key (togo_pkt_…) in development and staging and the live key
(togo_pkl_…) in production. They are the same public write keys the browser uses.
Name the person on every call
A server handles many people at once, so the SDK remembers nobody. Every call says who it is about:
analytics.track('order_paid', { plan: 'pro', seats: 5 }, { userId: user.id });
userId— your id for the signed-in user. Use the same one the browser passes toidentify(), or each user becomes two people.anonymousId— for a visitor who has not signed in: the browser’s anonymous id, forwarded from the page (analytics.getAnonymousId()in the browser).sessionId— optional: pass only the browser’sgetSessionId(), so the event joins the visit it belongs to.
An event with neither userId nor anonymousId is dropped with an error in your logs, because
nobody could be credited with it. The SDK never makes an id up.
Send events from your server for the right person shows how to
forward the browser’s ids.
Make sure events leave
Events are batched — 100 per request, or every 10 seconds — and retried with backoff when Togo is unreachable. How you end depends on where the code runs:
| Where | Do this |
|---|---|
| Serverless function (Vercel, Netlify, AWS Lambda) | Wrap the handler in analytics.withFlush() |
| Long-running server | await analytics.shutdown() on SIGTERM |
| Script or job | Nothing — queued events are sent before the process exits |
Serverless is the case that loses events. The platform may freeze the function the moment the
response is sent, with the batch still in memory. withFlush() sends it before the handler
returns, on every return path and when it throws:
export const POST = analytics.withFlush(async (request: Request) => {
// ...
analytics.track('invite_accepted', {}, { userId });
return Response.json({ ok: true });
});
Or call await analytics.flush() yourself before every return.
To send after the response instead of before it, hand the flush to the platform’s waitUntil. The
function keeps running until the flush resolves, and the caller gets its response first:
import { waitUntil } from '@vercel/functions';
export async function POST(request: Request) {
analytics.track('invite_accepted', {}, { userId });
waitUntil(analytics.flush());
return Response.json({ ok: true });
}
On Netlify, pass analytics.flush() to context.waitUntil(), with context the function’s second
argument. In a Next.js route handler, use after() (Next.js). Use one pattern per
handler: withFlush(), an awaited flush(), or waitUntil.
Wrap every handler that tracks, including the ones you add later. One wrapped handler does not
cover another: each invocation sends only what it tracked, before it returns. If the repository has
an AGENTS.md or CLAUDE.md, add a line saying so, so the next handler gets it too.
A local run will not show a missing flush. Your machine never freezes the process, so the SDK’s
10-second timer sends the batch anyway and the check below passes. The events go missing only once
the function runs on the platform, so read the handler for withFlush() rather than relying on the
test. When the SDK runs where a serverless platform’s environment variable is set
(AWS_LAMBDA_FUNCTION_NAME, FUNCTIONS_WORKER_RUNTIME, VERCEL or NETLIFY) and its 10-second
timer sends events twice with no flush() call between, it logs one
[togo] events were sent twice by the 10-second timer … warning naming the fix. vercel dev and
Azure Functions Core Tools set theirs locally, so two requests there show it.
process.on('SIGTERM', async () => {
await analytics.shutdown();
process.exit(0);
});
Nothing on the client throws. A problem is logged as [togo] … and the event is retried or
dropped. At most 10,000 events or 32 MiB wait in memory while Togo is unreachable; past either, the
oldest are dropped and reported with their ids (Limits).
Profile properties
analytics.setPersonProperties(
{ email: user.email, set: { plan: 'pro' }, setOnce: { first_plan: 'free' } },
{ userId: user.id },
);
Set person properties explains which keys are kept.
Check it works
Run the code path with the test key and confirm the events arrived:
Check that events arrive. For a serverless function, check again after you deploy:
open Events → Live View in the dashboard and switch to Production, then trigger the
handler. Its events show up there. All options are in the
@togohq/sdk-node reference.