analytics

package
v0.4.3-dev.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package analytics provides a thin, non-blocking wrapper around PostHog for capturing product analytics events. It is intentionally separate from the logging package so that analytics events and log forwarding can evolve independently and a failure in one never affects the other.

Usage:

tracker := analytics.New(cfg.PostHogAPIKey, cfg.PostHogEndpoint)
defer tracker.Close()
tracker.Capture("note.created", userID, map[string]any{"note_id": noteID})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NoopTracker

type NoopTracker struct{}

NoopTracker silently discards all events. Returned by New when no API key is configured so callers never need to guard against a nil Tracker.

func (NoopTracker) Capture

func (NoopTracker) Capture(_ string, _ string, _ map[string]any)

func (NoopTracker) Close

func (NoopTracker) Close()

type PostHogTracker

type PostHogTracker struct {
	// contains filtered or unexported fields
}

PostHogTracker forwards events to the PostHog API via the official SDK.

func (*PostHogTracker) Capture

func (t *PostHogTracker) Capture(event string, distinctID string, properties map[string]any)

Capture enqueues a PostHog Capture event. The call is non-blocking — the SDK batches events internally and flushes them on its own interval. Any enqueue error is intentionally swallowed so a PostHog outage never surfaces to callers.

func (*PostHogTracker) Close

func (t *PostHogTracker) Close()

Close flushes any buffered events and shuts down the underlying PostHog client.

type Tracker

type Tracker interface {
	// Capture enqueues an analytics event. It is non-blocking and always
	// succeeds from the caller's perspective.
	Capture(event string, distinctID string, properties map[string]any)

	// Close flushes any buffered events and releases resources. The caller
	// must invoke Close before the process exits — typically via defer.
	Close()
}

Tracker captures product analytics events. Implementations must be safe for concurrent use. A Tracker must never return an error from Capture — a PostHog outage or misconfiguration must not affect application availability.

func New

func New(apiKey, endpoint string) Tracker

New creates a Tracker backed by PostHog when apiKey is non-empty. If apiKey is empty or client initialisation fails, a NoopTracker is returned so the application keeps running. Failures are written to stderr rather than the application logger to avoid a dependency cycle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL