Skip to content

Product Analytics

Package analytics reports a small, registered set of product events from the server (ADR-026). Package analytics/posthog sends them to PostHog.

Set it up

Declare the application's events and the properties each may carry, and add the standard ones:

events, err := analytics.MergeEvents(analytics.StandardEvents(), analytics.Events{
    "report_created": {"plan", "format"},
})
if err != nil {
    return err
}
sender, idKey, err := posthog.FromEnv()
if err != nil {
    return err
}
client, err := analytics.New(analytics.Config{IDKey: idKey, Events: events}, sender)
if err != nil {
    return err
}
defer client.Close(shutdownCtx)

Then capture an event. It never blocks and never fails:

client.Capture(ctx, analytics.Event{
    Name: "report_created", Site: "example",
    PrincipalID: principalID, OrganizationID: orgID,
    Props: map[string]any{"plan": "free", "format": "pdf"},
})

Environment

Variable Meaning
TELEMETRY off turns analytics off
TELEMETRY_TOKEN The PostHog project token, which is public. Without it nothing is sent
TELEMETRY_ID_KEY The key IDs are hashed under: openssl rand -hex 32. A secret
TELEMETRY_HOST The ingestion host; PostHog's EU cloud by default

Leave the token unset in development, staging and tests.

Choose the ID key before the first event, one for each application. It cannot be rotated: a new key makes every person new. Applications that share a key can have their people matched with each other.

What is sent

  • The event's name, time, site and surface (app), and its registered properties. Anything else is removed and logged by name, never by value.
  • The principal and organization as keyed hashes, never as they are. Use DistinctID to find a person's events for export or erasure.
  • No person profile and no location.

A property named like personal data (email, billing_address) cannot be registered. The check reads names, not values, so never put personal data or free text in a property with another name.

Hashed IDs are pseudonymous, not anonymous: cover them in the privacy policy and in account deletion. Do not capture events for children's accounts without a review.

Events can be lost: a full queue drops the oldest, and a failed batch is not retried. Don't use them for billing, limits or audit.

Another service

Implement analytics.Sender. One that imports a vendor's SDK belongs in its own module.

Browser page views are counted by @plexusone/analytics.