Integration

Product analytics for Go

Send events from your Go services with the Pug Go SDK. Every call names the user, since a server has no browser session to infer one from, and the same private key reads profiles and insights back into your own code.

Pug Go SDK: one SDK for Go services github.com/pug-sh/sdk-go Open source: MIT SDK, AGPL-3.0 server

Setup

Add Pug to Go

Four steps, start to first insight. Everything below uses the Go SDK exactly as it ships.

  1. Install the Go SDK

    One module, for Go 1.26 or newer. Its package is named pug, and the generated request and response types for reads live under its gen directory.

    go get github.com/pug-sh/sdk-go
  2. Construct one client at startup

    pug.New returns an error unless the key starts with prv_, so a public key pasted in by mistake fails at boot instead of dropping events later. A *pug.Client is safe for concurrent use: build it once and share it across goroutines and requests.

    main.go
    import (
    	"log"
    	"os"
    
    	pug "github.com/pug-sh/sdk-go"
    )
    
    client, err := pug.New(pug.Options{
    	APIKey: os.Getenv("PUG_PRIVATE_KEY"), // prv_…: never ship this to a client
    })
    if err != nil {
    	log.Fatal(err)
    }
  3. Track from your handlers, drain on shutdown

    Track takes the user first and returns without waiting on the network, and an invalid call is logged through log/slog and dropped, so it can sit in a request path. Close the client after the HTTP server has shut down: requests still in flight queue their events first, then the queues drain.

    main.go
    mux.HandleFunc("POST /orders", func(w http.ResponseWriter, r *http.Request) {
    	order := placeOrder(r)
    	client.Track(order.UserID, "order.completed",
    		pug.Float("amount", order.Total), pug.String("currency", "USD"))
    })
    
    srv.Shutdown(ctx) // in-flight requests finish and queue their events
    client.Close(ctx) // then the queues drain
  4. Identify people and read the data back

    Identify writes traits to the profile, and named traits such as pug.Email write the keys the dashboard reads. Reads run in your control flow and return a *pug.Error, which errors.Is matches to a sentinel such as pug.ErrNotFound. They are never retried, so the retry policy is yours.

    users.go
    client.Identify("user_123",
    	pug.Email("ada@example.com"), pug.String("plan", "pro"))
    
    profile, err := client.Profiles.GetByExternalID(ctx, "user_123")
    if errors.Is(err, pug.ErrNotFound) {
    	// no profile with that external ID
    }
Framework notes

What to watch for in Go

The specifics that decide whether the numbers you see are the numbers that happened.

A private key is not a public key

The client SDKs’ key (pub_…) is write-scoped and meant to ship in an app. The server key (prv_…) also reads every profile and insight in the project, so it belongs in an environment variable or a secret store, and pug.New refuses to start without one.

Ingestion never fails, reads do

Track, TrackWellKnown, and Identify return nothing: invalid input is logged and dropped, and anything that can’t be delivered goes to the OnError hook. Reads return errors, bounded by your context’s deadline, or by the 10-second Timeout without one.

No location unless you pass it

Pug derives location from CDN headers only on requests made with a public key, so an event from this SDK has no geo at all. Pass the visitor’s country and city with pug.WithLocation when a report needs them.

No consent gate on the server

The Go SDK has no consent state and no redaction hooks. Check the visitor’s consent choice before you call Track, and keep personal data out of the properties you pass.

  • Tracking that never waits on the network

    Track and Identify validate, queue and return. Background goroutines send events and identifies in batches of up to 100 every five seconds, and retry a transient failure.

  • Typed properties

    Properties are typed fields such as pug.String and pug.Float, and TrackWellKnown takes a well-known event’s generated message, so the compiler checks its fields.

  • Reads as well as writes

    The same private key reads profiles, activity, and the insight queries the dashboard runs; Profiles.List ranges over every profile as an iterator.

  • Pairs with the client SDKs

    Web, mobile, and server events share one timeline per person when they name the same user id.

Does it work in a CLI or a one-off job?

Yes, as long as the process calls client.Close before it exits. Events wait in memory until a batch goes out, so a process that exits without Close loses whatever was still queued.

Does it need middleware for Gin, Echo, or Chi?

No. The SDK has no framework integration and needs none: one *pug.Client is safe for concurrent use, so any handler can call client.Track, whichever router serves it.

Does the Go SDK autocapture anything?

No. Autocapture is a browser feature, so you send every server event with Track or TrackWellKnown. The server adds no user agent to these events either.

What happens to events when a send fails?

A transient failure, such as a network error or a timeout, keeps the batch queued and retries it. A permanent one, such as a rejected key, goes to the OnError hook as a *pug.Error carrying the code. Close reports anything it couldn’t send to OnError too.

Does the AGPL apply to my service?

No. The SDK is MIT-licensed; AGPL-3.0 covers the Pug server. Self-host it and set Endpoint to your deployment, or use Pug Cloud, free up to 100k events a month.

Add Pug to your Go app.

Create a project, add the snippet, and watch the Live view fill up. Or open the demo first: the real dashboard on a sample pet shop, with no sign-up.

Questions? Email hello@pug.sh