Server-side events
Send events from your server with a secret key
Some events happen on your server, not in a browser: a payment webhook, a signup finished by your backend, a renewal from a nightly job. Send them from your server with a secret key, so an ad blocker or a closed tab can never lose them.
Before you start
You need two things:
- A secret key with the
analytics.sendscope. Create it in Settings → API keys with New key, and pick only that scope. You can limit it to the projects it sends to. Keep it on your server: it is shown once, and never goes in a browser. - Your project's id,
prj_…. It is on Settings → General as Project ID.
Send an event
Post the event to your region's events address with the key in the Authorization header. With a secret key, the event's project is the project's id:
1curl https://us.e.vk.viabio.me/v1/events \2-H "Authorization: Bearer sk_live_us_…" \3-H "Content-Type: application/json" \4-d '{5"type": "event",6"id": "01JD8X2M4N5P6Q7R8S9T0VWXYZ",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 how many events were stored and how many dropped:
1{ "accepted": 1, "dropped": 0 }
You can send a list of up to 100 events (500 KB) in one request. Every event in it must name the same project. An identify event ("type": "identify", a user_id and traits) works the same way. All the fields are in POST/v1/events.
Retries never count twice
Give every event its own id (a ULID or any unique text up to 64 characters). Viakite keeps each id once, so when a request times out you can send it again without counting the event twice. Without an id, Viakite makes one, and a retry is a new event.
Duplicates within 10 minutes
A repeated id is caught for 10 minutes on the server that received it. A retry sent much later, or one that reaches another server, can still be counted twice.
Who the visitor is
A request from your server carries your server's address and user agent, not your visitor's. So for events sent with a secret key:
- your server's user agent (
curl,node-fetch,Go-http-client) is never treated as a bot; - there is no location: the country and city come from the edge in front of a browser, and your server's would be wrong;
- Do Not Track headers on your server's request are ignored.
To tie the event to the visitor, pass their address and user agent from the request your server received:
1{2"type": "event",3"project": "prj_…",4"name": "Signup",5"user_id": "user_8231",6"ip": "203.0.113.9",7"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) …"8}
| Field | What Viakite does with it |
|---|---|
ip | Hashed with the day's salt into the visitor id, checked against your excluded IP ranges, then dropped. Never stored. |
user_agent | Read for the device, browser and system, checked against the bot rules (a crawler's event is dropped as a bot), hashed into the visitor id, then dropped. |
Without ip and user_agent, an event with a user_id counts that user as one visitor for the day. An event with none of the three is counted against your server's own address and user agent, so all such events of the day make one visitor. With a public key, ip and user_agent in the body are ignored: a browser can't pretend to be someone else.
Errors
| Status | Code | Why |
|---|---|---|
401 | unauthorized | The key is unknown, revoked or past its rotation |
403 | forbidden | The key doesn't have the analytics.send scope |
404 | not_found | The project doesn't exist, or the key can't reach it |
422 | missing_project | An event has no project id (prj_…) in project |
422 | mixed_projects | The events of one request name different projects |
413 | too_many_events | More than 100 events, or more than 500 KB |
Events over your plan's limit, or past a spend cap set to stop, are answered 202 and counted as dropped, like bots. Don't retry those.