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
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:
1package main23import (4"context"5"log"6"os"78viakite "github.com/viakite/viakite-go"9)1011func main() {12client, err := viakite.New(viakite.Options{13SecretKey: os.Getenv("VIAKITE_SECRET_KEY"),14Project: "prj_…",15})16if err != nil {17log.Fatal(err) // not a secret key, or not a prj_… id18}19defer client.Shutdown(context.Background())2021// … your server22}
Track an event
1client.Track(viakite.Event{2Name: "Purchase",3UserID: "user_8231",4Props: viakite.Props{"revenue": 499, "currency": "INR", "plan": "pro"},5})
| Field | Does |
|---|---|
Name | The event's name. Required. |
Props | Up to 20 flat properties. revenue and currency record money. |
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 | 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:
1client.Track(viakite.Event{2Name: "Signup",3UserID: user.ID,4IP: clientIP(r), // the visitor's address, as your proxy passes it5UserAgent: 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
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
sk_live_… or sk_test_…, with the analytics.send scope.prj_….https://us.e.vk.viabio.me/v1/events.20, at most 100.10s; negative turns the timer off.3; negative turns retries off.10s.10000.Timeout.