Skip to content

Node.js SDK

Official Node.js server-side SDK for Mitr Analytics. Use this to emit events from a backend service, webhook handler, cron job, or CLI tool — anywhere there's no browser and no app UI, just a server.

Install

Not yet published to npm — add it as a local/git dependency:

npm install @mitr/node

Get your Site ID and Secret Key

From the dashboard: Integration Hub → Add New App → Server. You'll get a Site ID and a Secret Key — copy the secret key now, it's shown in full only once.

Initialize

import { MitrAnalytics } from '@mitr/node';

const mitr = new MitrAnalytics({
  siteId: 'YOUR_SITE_ID',
  secretKey: 'YOUR_SECRET_KEY',
});

Multi-user servers

A Node process usually serves many users concurrently, so there's no single "current user" the way a browser tab or app instance has. Pass userId per call rather than calling identify() if this server handles requests for multiple end users — the common case for an API backend:

app.post('/checkout', async (req, res) => {
  await mitr.track('checkout_completed', {
    userId: req.user.id,
    metadata: { amount: req.body.amount },
  });
});

identify()/reset() set a default userId for calls that don't pass their own — only useful for a single-tenant script, worker, or cron job that genuinely acts as one identity end to end.

Delivery guarantees

Events are batched (flushed every 10s or every 20 events, whichever comes first) and retried once on failure. Unlike the browser/mobile SDKs, nothing is persisted to disk — always call await mitr.close() in your shutdown handler so the final batch actually gets sent instead of being dropped:

process.on('SIGTERM', async () => {
  await mitr.close();
  process.exit(0);
});

Have questions about integration or security? Drop us a line directly.