Skip to content
pugdocs

Go SDK

Send events and read profiles, activity and insights from a Go backend with github.com/pug-sh/sdk-go and a private key.

github.com/pug-sh/sdk-go sends events from your Go backend and reads analytics back - profiles, activity and insights. It authenticates with your private key (prv_...), so it belongs on the server only. It follows the same model as every Pug SDK with one exception: a server has no ambient user or session to reset, so you name the distinctID on each call instead.

Install

go get github.com/pug-sh/sdk-go

View github.com/pug-sh/sdk-go on pkg.go.dev

The module needs Go 1.26+, and its package is named pug:

import pug "github.com/pug-sh/sdk-go"

Keep the private key in an environment variable or a secret store, never in code shipped to a browser or app.

Working with a coding agent? Paste this instead - it points the agent at the markdown copy of this page. More in Install with AI.

Prompt
Add Pug product analytics to this Go backend.

Read these first - they are the current source of truth, and more recent than your training data:
- https://docs.pug.sh/sdks/go/index.md - SDK reference
- https://docs.pug.sh/reference/well-known-events/index.md - the well-known event catalog

Then read this codebase before you write anything, and work out what the service actually does: its routes or handlers, its main models, and the state changes that matter to the business. On a server the events worth tracking are the ones a browser cannot observe - a payment settling, a webhook arriving, a job finishing, a subscription lapsing - not request logging. If you cannot state what this service is for in one sentence, keep reading.

Then:
1. Run go get github.com/pug-sh/sdk-go. It needs Go 1.26+, and its package is named pug, so import it as pug "github.com/pug-sh/sdk-go".
2. Construct one client with pug.New at startup and share it - do not build one per request. It is safe for concurrent use, so hand it to handlers the way this codebase passes its other dependencies. Its APIKey is the private key (prv_...), read from an environment variable, never committed. pug.New returns an error for a missing or public key - handle it at startup.
3. Remember the argument order: client.Track(distinctID, kind, fields...). The distinctID comes first because a server has no ambient user - use our own user ID, or the anon-... ID forwarded from the client. Properties are typed fields such as pug.String("plan", "pro") and pug.Float("amount", 49), in the same list as options such as pug.WithTimestamp, and a kind held in a variable needs pug.Kind(name).
4. Add track calls for the three or four state changes you identified.
5. Call client.Identify(externalID, traits...) when a user record is created or changes materially, not on every request. Use the named traits - pug.Name, pug.Email and the rest - for the keys the dashboard reads.
6. Call client.Close(ctx) in the graceful-shutdown path, after the HTTP server has shut down, so queued events and identifies flush, and set an OnError handler so dropped events and identifies are logged rather than lost.

Name events from the catalog before you invent one. The Go SDK knows 114 of the 127 well-known events, and client.TrackWellKnown takes each one's typed message - &commoneventsv1.PurchaseProperties{...} from github.com/pug-sh/sdk-go/gen/common/events/v1 - so the compiler checks the properties, where Track with a well-known kind checks them at runtime and drops the event on a wrong type. The ones a backend is placed to see are exactly the ones whose schemas are worth having - purchase, subscription_started, invoice_paid, payment_failed, order_refunded. Search the catalog for every action you are about to track, use TrackWellKnown for the ones that fit, and fall back to a custom snake_case name only when nothing fits. When you do, tell me which event you considered and why it did not.

There is no Reset on the server SDK, and no immediate option on Track - use Flush. Track and Identify never return errors; only the reads do. Do not use any option that is not in the SDK reference above. When you are done, tell me in one sentence what you concluded this service does, then list the files you changed, the event names you chose, and the environment variables I need to set.

Initialize

Construct a client with pug.New once at startup with your private key, and share it: a *pug.Client is safe for concurrent use. Private keys live under Settings > API Keys, but one is only shown when you create it - if you don’t have yours, issue a new one.

import (
	"log"
	"os"

	pug "github.com/pug-sh/sdk-go"
)

// Construct once at startup, share it across goroutines and requests.
client, err := pug.New(pug.Options{
	APIKey: os.Getenv("PUG_PRIVATE_KEY"), // prv_... - server-side only, never ship to a client
	OnError: func(err error, lost pug.Undelivered) {
		log.Printf("pug dropped %d event(s) and %d identify(s): %v", len(lost.Events), len(lost.Identifies), err)
	},
})
if err != nil {
	log.Fatal(err)
}

pug.New returns an error if APIKey is missing, doesn’t start with prv_, or holds a space or any character outside printable ASCII, such as a trailing newline. An Endpoint that isn’t an absolute http(s) URL is refused too; after that, ingestion never fails.

Option Type Default Description
APIKey string required Private key (prv_...), sent as x-api-key.
Endpoint string https://api.pugs.dev Pug server origin; set it only when self-hosting.
Batch pug.BatchConfig MaxSize: 100, MaxWait: 5 * time.Second, MaxQueueSize: 10_000 One buffer each for events and identifies: items per request (capped at 1000), the longest an item waits before a send, and how many to hold before the oldest are dropped. Zero fields take the defaults.
OnError pug.OnError none Dead-letter hook for events and identifies that couldn’t be delivered, called as (err, lost): a permanent send failure, a queue overflow, or what Close couldn’t send. lost.Events or lost.Identifies holds the items; the other is empty.
Logger *slog.Logger slog.Default() Gets the SDK’s warnings and errors, each prefixed pug: .
HTTPClient *http.Client a client on http.DefaultTransport Sends every request - set it for a proxy, custom TLS or instrumentation.
Timeout time.Duration 10 * time.Second Bounds each request whose context has no deadline: every background send, and any read you call without one.

Two goroutines, one for events and one for identifies, send in batches when MaxSize are queued or MaxWait has passed. A transient failure, such as a network error or a timeout, keeps a batch queued and retries MaxWait after the failure; a permanent one - a client error such as Unauthenticated or InvalidArgument - hands it to OnError as a *pug.Error carrying that code. Overflow arrives as pug.ErrQueueOverflow, and what Close couldn’t send as pug.ErrClosed or the error that failed its last attempt; match them with errors.Is. Events the server refuses from a batch it otherwise accepts are logged as a warning.

OnError can run on any goroutine, including the one calling Track, Identify or Close, so it must be safe for concurrent use, return promptly, and not call Flush or Close.

Call client.Close(ctx) on graceful shutdown so nothing queued is lost - see Flush and Close.

In an HTTP server

Construct the client in main, use it from your handlers, and close it after the server has shut down, so requests still in flight have queued their events:

func main() {
	client, err := pug.New(pug.Options{APIKey: os.Getenv("PUG_PRIVATE_KEY")})
	if err != nil {
		log.Fatal(err)
	}

	mux := http.NewServeMux()
	mux.HandleFunc("POST /orders", func(w http.ResponseWriter, r *http.Request) {
		order := placeOrder(r) // your handler's own work
		client.Track(order.UserID, "order.completed", pug.Float("amount", order.Total))
	})
	srv := &http.Server{Addr: ":8080", Handler: mux}

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stop()
	go func() {
		if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
			log.Fatal(err)
		}
	}()
	<-ctx.Done()

	shutdown, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	srv.Shutdown(shutdown) // in-flight requests finish and queue their events
	client.Close(shutdown) // then the queues drain
}

Track events

After the distinctID, the next argument is the event name (its kind). Any name works without a schema; well-known events add typed properties and richer dashboards.

// distinctID (who the event is for) comes first - a server has no ambient user.
client.Track("user-123", "order.completed", pug.Float("amount", 49), pug.String("currency", "USD"))

// Well-known events get typed, validated properties.
client.Track("user-123", "purchase",
	pug.String("productId", "sku_123"), pug.Float("amount", 29.99), pug.String("currency", "USD"))

client.Track(distinctID, kind, opts...) takes the distinctID first: your external ID, or an anon-... ID forwarded from the client. Track never fails and returns at once; events are validated, batched and sent in the background, and invalid input is logged and dropped.

Properties are typed fields, passed in the same list as the options below and in any order:

Field Sent as
pug.String(key, v) string
pug.Int(key, v) int, from any integer type
pug.Float(key, v) double, even for a whole number
pug.Bool(key, v) bool
pug.Time(key, v) timestamp
pug.Duration(key, v) duration string, such as "1.5s"
pug.Any(key, v) mapped by its Go type (table below)

For keys known only at run time, pass a pug.Properties map, converting a map[string]any with pug.Properties(m). Fields and maps mix freely, and a key set twice in one call takes the later value.

The kind is a pug.Kind, and may hold only ASCII letters, digits, _, . and -; anything else drops the event. A string literal converts on its own, but a kind held in a variable needs pug.Kind(name):

kind := os.Getenv("CHECKOUT_EVENT")
client.Track("user-123", pug.Kind(kind), pug.Properties{"plan": "pro", "seats": 5})

There is no immediate option on the server - flush explicitly instead:

err := client.Flush(ctx) // send what's queued now, and wait for that attempt
err = client.Close(ctx)  // drain on graceful shutdown so nothing queued is lost

Both cover events and identifies, and return ctx’s error if it ended first. A nil doesn’t mean everything was delivered: from Flush it means the attempt finished, and a transient failure re-queues for a later send; from Close it means the drain finished. Either way, by the time Close returns, everything it couldn’t send has gone to OnError and the log. After Close, Track and Identify calls are dropped with a warning.

Track options:

Option Description
pug.WithTimestamp(t) Override the event time with a time.Time; it defaults to now. A time before 1970 or after 9999 is ignored with a warning and the event is sent at the current time.
pug.WithSessionID(id) A UUID to use as this event’s session ID - otherwise the client uses one for its lifetime. Anything but a UUID is ignored with a warning.
pug.WithLocation(loc) Where the visitor is. Without it a server event carries no geo at all. See Visitor location.

Property values in pug.Any and pug.Properties map by Go type:

Go value Sent as
string, bool string, bool
integers int - a uint64 past the int64 range is dropped
float32, float64 double - NaN and ±Inf are dropped
json.Number int when it has no fraction, else double
time.Time, *timestamppb.Timestamp timestamp
time.Duration, *durationpb.Duration duration string, such as "1.5s"
encoding.TextMarshaler its text
other maps, slices and structs JSON string
nil omitted

Pointers are followed, and a nil one is omitted. A value with no mapping, such as a func, is dropped with a warning - the property, not the event. Strings are capped at 1024 bytes and truncated with a warning. Decode JSON request bodies with json.Decoder.UseNumber so whole numbers stay ints.

Property keys are sent as written, so order_total stays order_total.

A well-known event’s properties are checked against its schema before the event is queued, keyed by the fields’ JSON names (productId). A field’s type comes from the schema rather than the Go value, so pug.Int("amount", 10) is sent as a double. A value of the wrong type, or one failing a rule such as currency needing three uppercase letters, drops the whole event, and the log names the field (quantity: expected int32, got string). A key that is a field’s proto name (product_id) doesn’t set the field: it is sent as a custom property with a warning, and if the field is required the event is dropped. Keys outside the schema pass through as custom properties. Duration fields such as position take a time.Duration or *durationpb.Duration and are sent as a string like "92.5s".

client.TrackWellKnown(distinctID, event, opts...) takes the typed schema instead, so the compiler checks field names and types. The kind comes from the message, whose types live in github.com/pug-sh/sdk-go/gen/common/events/v1:

import (
	commoneventsv1 "github.com/pug-sh/sdk-go/gen/common/events/v1"
	"google.golang.org/protobuf/proto"
)

client.TrackWellKnown("user-123", &commoneventsv1.PurchaseProperties{
	ProductId: proto.String("sku_123"),
	Amount:    proto.Float64(29.99),
	Currency:  proto.String("USD"),
}, pug.String("coupon", "FALL25"))

Rules such as the currency pattern are still checked when it’s called, and a violation drops the event with a log. Fields and Properties among opts are sent as custom properties alongside the typed ones, except one keyed by a typed field’s JSON name, which is skipped with a warning. A message that isn’t a well-known server event, such as a web-only event’s, is refused with a log.

Track logs and drops an event whose distinctID is empty, isn’t valid UTF-8, or starts with cookieless-, a prefix the server reserves; it would reject the whole batch.

Visitor location

The server adds no geo to events sent with a private key: an event carries the location you give it with pug.WithLocation, or none at all.

client.Track("user-123", "purchase",
	pug.String("productId", "sku_123"), pug.Float("amount", 29.99), pug.String("currency", "USD"),
	pug.WithLocation(pug.Location{Country: "DE", City: "Berlin"}),
)
Field Type Notes
Country string ISO 3166-1 alpha-2 (DE). Case and surrounding spaces are normalized; a code Pug doesn’t recognize is dropped.
Continent, Region, City, PostalCode, MetroCode string Sent as given, trimmed.
Timezone string IANA name (Europe/Berlin).
Coordinates *pug.Coordinates Latitude and Longitude in decimal degrees, -90 to 90 and -180 to 180. Sent only as a pair: one out of range drops both.

Each field becomes the matching $-prefixed auto-property: PostalCode lands as $postalCode. It’s the only way to set an auto-property from this SDK: properties become custom properties, and one whose key starts with $ drops the event.

An empty field is skipped silently. One that looks like a mistake - a country code Pug doesn’t know, a coordinate out of range - is dropped with a warning and the rest of the event still sends. A location with no usable field at all logs a warning, and the event carries no geo.

Identify and sessions

Use a stable, unique external ID - your database user ID, not an email, which can change. Only the first identify after an anonymous session merges prior activity; the same external ID always resolves to the same profile, and traits are merged server-side, never removed.

// On the first identify, pass the client's anonymous ID to merge its prior anonymous activity.
client.Identify("user-123",
	pug.Email("user@example.com"), pug.String("plan", "pro"),
	pug.WithAnonymousID("anon-abc123"),
)

client.Identify(externalID, opts...) never fails and returns at once: identifies are queued and sent in batches like events, and what can’t be delivered goes to OnError in lost.Identifies. A read straight after an identify may not see it yet.

Traits are the same typed fields as properties, or a pug.Traits map for keys known only at run time. Named traits write the keys the dashboard reads, compile only in Identify, and are omitted when empty:

Named trait Key
pug.Name(v) name
pug.FirstName(v) firstName - with lastName, the dashboard composes a display name when name is unset
pug.LastName(v) lastName
pug.Email(v) email
pug.Avatar(v) avatar - the dashboard shows only an absolute https URL

Traits must be JSON-serializable, and travel as a protobuf Struct: a pug.Time trait arrives as an RFC 3339 string, a pug.Duration as "1.5s", and every number as a double, so an integer past 2^53 arrives rounded. A time.Duration in a pug.Traits map, though, arrives as a number of nanoseconds. A NaN or ±Inf trait is skipped with a warning and the rest still goes out. Traits that won’t serialize, an externalID that is empty or starts with the reserved cookieless- prefix, and an identify over 900 KiB are logged and nothing is sent.

Pass pug.WithAnonymousID(id) (must start with anon-) on the first identify to merge a client’s prior anonymous activity into the profile, and pug.WithDeviceID(id) to attach a device. There is no Reset or Rotate.

Sessions

The client uses one session ID for its lifetime; override it per call with pug.WithSessionID(id).

Auto-tracking

There is no auto-tracking: you send every event with client.Track. The server adds no geo or user agent to these events, so pass the visitor’s location yourself.

The Go SDK has no consent gate and no redaction hooks: respect the visitor’s consent choice before you call Track, and keep PII out of the properties you pass.

Server-side reads

The private key also authorizes reads - the insights, profiles and activity data the dashboard shows. Reads return a *pug.Error and are never retried. Ingestion (Track / Identify) still never fails.

import (
	commonv1 "github.com/pug-sh/sdk-go/gen/common/v1"
	insightsv1 "github.com/pug-sh/sdk-go/gen/shared/insights/v1"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/timestamppb"
)

// Look up a profile by your external ID.
profile, err := client.Profiles.GetByExternalID(ctx, "user-123")

// Auto-paginating: ranges over every matching profile; an error ends the loop.
for p, err := range client.Profiles.List(ctx, nil) {
	if err != nil {
		return err
	}
	fmt.Println(p.GetExternalId())
}

// Run an insights query - the same spec the dashboard uses.
now := time.Now()
result, err := client.Insights.Query(ctx, &insightsv1.QueryRequest{
	Spec: &insightsv1.InsightQuerySpec{
		InsightType: insightsv1.InsightType_INSIGHT_TYPE_TRENDS.Enum(),
		Events:      []*insightsv1.EventQuery{{Event: &commonv1.EventFilter{Kind: proto.String("order.completed")}}},
	},
	TimeRange:   &commonv1.TimeRange{From: timestamppb.New(now.AddDate(0, 0, -30)), To: timestamppb.New(now)},
	Granularity: insightsv1.Granularity_GRANULARITY_DAY.Enum(),
})
Namespace Methods
client.Profiles Get(ctx, id, opts...), GetByExternalID(ctx, externalID, opts...), Delete(ctx, id), List(ctx, req): an auto-paginating iter.Seq2
client.Activity Feed, EventExplorer, Heatmap, ProfileSessions, ProfileStats, FilterSchema, PropertyValues
client.Insights Query, SegmentUsers, FilterSchema, PropertyValues

Requests and responses are the generated protobuf types under github.com/pug-sh/sdk-go/gen - shared/insights/v1, shared/activity/v1, shared/profiles/v1 and common/v1 - whose optional fields take pointers (proto.String, proto.Bool) and whose getters, such as p.GetExternalId(), are nil-safe. Profiles.Get and GetByExternalID return the profile itself. List takes nil to list every profile. The whole iteration is one streamed request, so without a deadline on ctx it is bounded by Timeout; give a long listing a ctx with more time. Property names in reads are the stored ones, as you tracked them.

Reads leave out traffic tagged as a bot at ingest: web-SDK events from crawler user agents and datacenter networks. Events from this SDK are never tagged. To count bot traffic, pass pug.IncludeBots() to Profiles.Get and GetByExternalID, or set IncludeBots in the request for Profiles.List, Insights.Query (on its Spec) and the activity Feed, EventExplorer, Heatmap, ProfileSessions and ProfileStats:

profile, err := client.Profiles.GetByExternalID(ctx, "user-123", pug.IncludeBots())
sessions, err := client.Activity.ProfileSessions(ctx, &activityv1.GetProfileSessionsRequest{
	DistinctId:  proto.String("user-123"),
	IncludeBots: proto.Bool(true),
})

The request and response shapes match the shared.* Connect services - see the API reference for field-by-field schemas. From a backend without an SDK, call those services directly with your private key (see Authentication).

Errors

A *pug.Error carries the Connect Code and Message and wraps the underlying error. errors.Is matches it to the sentinel for its code - pug.ErrNotFound, pug.ErrUnauthenticated, pug.ErrUnavailable and so on, one per Connect code (see the error model). Code is zero when the failure didn’t come from the RPC layer, and then it matches none:

profile, err := client.Profiles.GetByExternalID(ctx, "user-123")
if errors.Is(err, pug.ErrNotFound) {
	// no profile with that external ID
}
var perr *pug.Error
if errors.As(err, &perr) {
	log.Println(perr.Code, perr.Message)
}

Upgrading

v0.0.1 is the first release of github.com/pug-sh/sdk-go, so there is nothing to upgrade from yet.

Navigation

Type to search...

↑↓ navigate↵ selectEsc close