viakite
Dashboard

Guides/Use cases/Backend and server events

Backend and server events

Events from your server that nothing can block

Some events happen where no browser can see them: a payment webhook, a sign-up your backend finishes, a renewal from a nightly job, an action in a CLI. Send them from your server with a secret key, so an ad blocker or a closed tab never loses them, and they land in the same reports as your page views.

Who it's for

Backend developers, and any team whose key actions happen on a server, in a worker or behind an API.

What you get

FeatureWhat it doesGuide
@viakite/nodeBatches, retries with backoff, flush and shutdown, for Node.js 18 and laterNode.js
The events endpointPlain JSON over HTTP from any languageServer-side events
Event idsA retry is stored onceRetries never count twice
The visitor's address and user agentServer events tied to the visitor and their sourceWho the visitor is
Revenue and identifyPayments and user traits from your backendRevenue, Identify users

Set it up

  1. Create a secret key

    In Settings → API keys, choose New key, pick only the analytics.send scope, and limit it to the project. Copy the Project ID (prj_…) from Settings → General. Keep the key in your server's environment as VIAKITE_SECRET_KEY.

  2. Send events

    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});
    7
    8// In a request handler:
    9viakite.track("Signup", { method: "google" }, {
    10 userId: user.id,
    11 ip: req.ip,
    12 userAgent: req.headers["user-agent"],
    13});
    14
    15// In a payment webhook, with the payment's id so a retry counts once:
    16viakite.track("Purchase", { revenue: 499, currency: "INR", plan: "pro" }, { id: payment.id, userId: user.id });
    terminal
    1curl https://us.e.vk.viabio.me/v1/events \
    2 -H "Authorization: Bearer $VIAKITE_SECRET_KEY" \
    3 -H "Content-Type: application/json" \
    4 -d '{
    5 "type": "event",
    6 "id": "pay_01JD8X2M4N5P6Q7R8S",
    7 "project": "prj_…",
    8 "name": "Purchase",
    9 "user_id": "user_8231",
    10 "props": { "revenue": 499, "currency": "INR", "plan": "pro" }
    11 }'

    The answer is 202 with { "accepted": 1, "dropped": 0 }. A list of up to 100 events goes in one request.

  3. Send what waits before you exit

    @viakite/node sends when 20 events wait, every 10 seconds, and on flush(). Call shutdown() before the process exits, and flush() before a serverless function returns:

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

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

    Over HTTP, send "type": "identify" with a user_id and traits.

What to watch

  • No location. Server events have no country or city: the edge in front of a browser gives those, and your server's would be wrong.
  • One visitor without the request. Without ip and user agent, events with a user_id count that user as one visitor for the day; events with none of the three all count as your server, one visitor a day.
  • Duplicates. A repeated id is caught for 10 minutes on the server that got it; a much later retry can still count twice.
  • Plan limits. Events past your plan's limit or a spend cap are answered 202 and counted as dropped. Don't retry them. Sending events isn't counted against the API rate limit.

Go: not published yet

The Go SDK is built, but its module isn't public yet, so go get github.com/viakite/viakite-go doesn't work today. From Go, post to the events endpoint over HTTP as above.