Node.js

Track events and identify users from a Node.js backend.

Use the Node SDK for backend events: purchases confirmed server-side, webhook activity, background jobs. The server SDK is stateless, so you pass the user's distinctId on every call, or bind it once.

Install

npm install trodo-node

Node 16+ (18+ for built-in fetch).

Initialise once at boot

import trodo from 'trodo-node';

trodo.init({
  siteId: process.env.TRODO_SITE_ID,
  autoEvents: true, // capture uncaught exceptions as server_error
});

Use the same site ID as your browser install, so server and browser events land on one user.

Track, identify, people, groups

// Direct — distinct id first
await trodo.track('user-42', 'invoice_paid', { amount_cents: 9999 });

// Bound — bind once, call many
const user = trodo.forUser('user-42');
await user.track('invoice_paid', { amount_cents: 9999 });
await user.people.set({ plan: 'pro' });
await user.set_group('company', 'acme');

Flush on exit

When batching or auto-events are on, flush queued events before the process exits:

process.on('SIGTERM', async () => {
  await trodo.shutdown();
});

Next

On this page