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
1npm install @viakite/node
1pnpm add @viakite/node
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:
1import { Viakite } from "@viakite/node";23export const viakite = new Viakite({4secretKey: process.env.VIAKITE_SECRET_KEY!,5project: "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
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:
| Option | Does |
|---|---|
userId | Your own id for the signed-in user. Never an e-mail address or phone number. |
ip | The visitor's IP address, from the incoming request. Hashed into the visitor id, then dropped. |
userAgent | The visitor's user agent. Gives the device and browser, and the bot check. |
url | The page the event belongs to, a full address. |
timestamp | A Date, when it happened. Default: now. |
id | Your 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:
1viakite.track("Signup", { method: "google" }, {2userId: user.id,3ip: req.ip,4userAgent: 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
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.
1process.on("SIGTERM", async () => {2await viakite.shutdown();3process.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:
1new Viakite({2secretKey: process.env.VIAKITE_SECRET_KEY!,3project: "prj_…",4onError: (err) => logger.error({ status: err.status, code: err.code, lost: err.events }, err.message),5});
Options
sk_live_… or sk_test_…, with the analytics.send scope.prj_….https://us.e.vk.viabio.me/v1/events.20, at most 100.10000; 0 turns the timer off.3.10000.onError and are dropped. Default 10000.fetch.