viakite
Dashboard

SDKs/Server/Go

Go

viakite-go: events from your server

The Go SDK sends events from a Go server with a secret key: purchases, signups, webhooks and jobs. Events wait in memory, go in batches from a background goroutine, and are tried again when the network fails, so a passing error loses nothing. It uses only the standard library.

Not published yet

The Go SDK is built and tested, but its module isn't public yet, so go get github.com/viakite/viakite-go doesn't work today. Until it is, send events from your server with the API: see Server-side events.

Install

terminal
1go get github.com/viakite/viakite-go

It needs Go 1.22 or later.

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 program; it is safe to share between goroutines:

main.go
1package main
2
3import (
4 "context"
5 "log"
6 "os"
7
8 viakite "github.com/viakite/viakite-go"
9)
10
11func main() {
12 client, err := viakite.New(viakite.Options{
13 SecretKey: os.Getenv("VIAKITE_SECRET_KEY"),
14 Project: "prj_…",
15 })
16 if err != nil {
17 log.Fatal(err) // not a secret key, or not a prj_… id
18 }
19 defer client.Shutdown(context.Background())
20
21 // … your server
22}

Track an event

webhooks.go
1client.Track(viakite.Event{
2 Name: "Purchase",
3 UserID: "user_8231",
4 Props: viakite.Props{"revenue": 499, "currency": "INR", "plan": "pro"},
5})
FieldDoes
NameThe event's name. Required.
PropsUp to 20 flat properties. revenue and currency record money.
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.
TimestampWhen 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:

signup.go
1client.Track(viakite.Event{
2 Name: "Signup",
3 UserID: user.ID,
4 IP: clientIP(r), // the visitor's address, as your proxy passes it
5 UserAgent: r.UserAgent(),
6})

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.

Track only queues the event; it answers an error only when the event can't be queued: ErrNoName, ErrQueueFull (more than MaxQueueSize wait) or ErrShutDown.

Identify a user

go
1client.Identify(viakite.Identify{UserID: "user_8231", Traits: viakite.Props{"plan": "pro"}})

Identify also takes IP, UserAgent and Timestamp, and answers ErrNoUserID without a user id.

Send before your program exits

Events go when 20 wait, every 10 seconds, and when you call Flush. Call Shutdown before the program exits: it stops the background sender and sends what waits. Events still waiting when the program ends without it are lost. In a short job, call client.Flush(ctx) before it ends. Both return the batches that failed, joined into one error.

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.

A batch that fails for good is a *viakite.Error with Status, Code and Events (how many were lost). Failures of the background sender go to OnError; by default the standard logger prints them.

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.
FlushAtint
Send once this many events wait. Default 20, at most 100.
FlushIntervaltime.Duration
Send what waits this often. Default 10s; negative turns the timer off.
MaxRetriesint
How many times a failed request is tried again. Default 3; negative turns retries off.
Timeouttime.Duration
How long one request may take. Default 10s.
MaxQueueSizeint
The most events that may wait. Default 10000.
OnErrorfunc(error)
Hears about events the background sender couldn't send. Default: the standard logger.
HTTPClient*http.Client
Sends the requests. Default: a client with Timeout.
Debugbool
Log every request and answer.