viakite
Dashboard

SDKs/Server/Node.js

Node.js

@viakite/node: events from your server

@viakite/node sends events from a Node.js server with a secret key: purchases, signups, webhooks and jobs. Events wait in memory, go in batches in the background, and are tried again when the network fails, so a passing error loses nothing.

Install

terminal
1npm install @viakite/node
terminal
1pnpm add @viakite/node
terminal
1yarn add @viakite/node

It needs Node.js 18 or later, ships ES modules and CommonJS with types, and has no dependencies.

Start it

Create a secret key with the analytics.send scope in Settings → API keys, and copy the Project ID (prj_…) from Settings → General. Make one client for your app:

src/viakite.ts
1import { Viakite } from "@viakite/node";
2
3export const viakite = new Viakite({
4 secretKey: process.env.VIAKITE_SECRET_KEY!,
5 project: "prj_…",
6});

A key that isn't a secret key, or a project that isn't a prj_… id, throws here, so a wrong setup shows at start and not as missing numbers.

Track an event

src/webhooks.ts
1viakite.track("Purchase", { revenue: 499, currency: "INR", plan: "pro" }, { userId: "user_8231" });

track(name, props?, options?) takes the event's name, up to 20 flat properties, and these options:

OptionDoes
userIdYour own id for the signed-in user. Never an e-mail address or phone number.
ipThe visitor's IP address, from the incoming request. Hashed into the visitor id, then dropped.
userAgentThe visitor's user agent. Gives the device and browser, and the bot check.
urlThe page the event belongs to, a full address.
timestampA Date, when it happened. Default: now.
idYour own unique id for the event, so sending it twice stores it once. Default: a new ULID.

Pass the visitor from the request your server received, to tell visitors apart:

src/signup.ts
1viakite.track("Signup", { method: "google" }, {
2 userId: user.id,
3 ip: req.ip,
4 userAgent: req.headers["user-agent"],
5});

Without ip and userAgent, an event with a userId counts that user as one visitor for the day. Events from a server have no location. More in Server-side events.

Identify a user

ts
1viakite.identify("user_8231", { plan: "pro", company: "Acme" });

identify(userId, traits?, options?) takes ip, userAgent and timestamp as options.

Send before your process exits

Events go when 20 wait, every 10 seconds, and when you call flush(). Call shutdown() before the process exits: it stops the timer and sends what waits. Events still waiting when the process ends without it are lost.

src/server.ts
1process.on("SIGTERM", async () => {
2 await viakite.shutdown();
3 process.exit(0);
4});

In a serverless function, await viakite.flush() before you return. flush() and shutdown() never reject.

Retries and errors

A request that fails with a network error, a timeout, 429 or a 5xx is tried again up to 3 times, waiting longer each time (from half a second, doubling, or what Retry-After asks). Every event keeps its id across retries, so it is stored once. Other errors, such as 401 for a wrong key or 403 without analytics.send, are not tried again.

Events that can't be sent go to onError as a ViakiteError with status, code and events (how many were lost). By default it is a warning in the console:

ts
1new Viakite({
2 secretKey: process.env.VIAKITE_SECRET_KEY!,
3 project: "prj_…",
4 onError: (err) => logger.error({ status: err.status, code: err.code, lost: err.events }, err.message),
5});

Options

secretKeystringrequired
A secret key, sk_live_… or sk_test_…, with the analytics.send scope.
projectstringrequired
The project's id, prj_….
endpointstring
Send to another address, such as a proxy. Default: your key's region, https://us.e.vk.viabio.me/v1/events.
flushAtnumber
Send once this many events wait. Default 20, at most 100.
flushIntervalnumber
Send what waits every this many milliseconds. Default 10000; 0 turns the timer off.
maxRetriesnumber
How many times a failed request is tried again. Default 3.
timeoutnumber
How long one request may take, in milliseconds. Default 10000.
maxQueueSizenumber
The most events that may wait; newer ones go to onError and are dropped. Default 10000.
onError(error: ViakiteError) => void
Hears about events that couldn't be sent. Default: a console warning.
debugboolean
Log every request and answer in the console.
fetchtypeof fetch
The fetch to use. Default: Node's global fetch.