posthogkit

package
v11.3.24 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package posthogkit provides a non-blocking, batched PostHog analytics client. Events are buffered in memory and flushed periodically via tick or when the buffer reaches FlushSize. All capture methods are no-ops when Enabled is false.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a non-blocking, batched PostHog analytics client. Capture methods synchronously snapshot event data before returning, so callers retain ownership of their maps and slices.

func New

func New(cfg Config) *Client

New creates a new PostHog client. Defaults are applied for Host ("https://us.i.posthog.com"), FlushInterval (30s), and FlushSize (100).

func (*Client) Capture

func (c *Client) Capture(distinctID, eventName string, props map[string]any)

Capture records an analytics event.

func (*Client) CaptureWithGroups

func (c *Client) CaptureWithGroups(distinctID, eventName string, props map[string]any, groups map[string]string)

CaptureWithGroups records an event with group context.

func (*Client) Check

func (c *Client) Check() health.Check

Check returns a health.Check that reports healthy when the client is enabled and configured with an API key. Register with health.All().

func (*Client) Close

func (c *Client) Close()

Close stops and joins the flush goroutine before performing a final synchronous flush. Safe to call multiple times and concurrently.

func (*Client) Flusher

func (c *Client) Flusher() func(context.Context) error

Flusher returns a tick-compatible function that periodically flushes the event buffer. Pass the result to lifecycle.Run.

func (*Client) GroupIdentify

func (c *Client) GroupIdentify(groupType, groupKey string, props map[string]any)

GroupIdentify associates properties with a group.

func (*Client) HashID

func (c *Client) HashID(id string) string

HashID returns an HMAC-SHA256 hex digest of id using the configured HMACSecret. Returns id unchanged if no secret is configured.

func (*Client) Identify

func (c *Client) Identify(distinctID string, props map[string]any)

Identify associates properties with a distinct user.

type Config

type Config struct {
	APIKey        string        `env:"POSTHOG_API_KEY"        required:"false"`
	Host          string        `env:"POSTHOG_HOST"           default:"https://us.i.posthog.com"`
	FlushInterval time.Duration `env:"POSTHOG_FLUSH_INTERVAL" default:"30s"`
	FlushSize     int           `env:"POSTHOG_FLUSH_SIZE"     default:"100"`
	Enabled       bool          `env:"POSTHOG_ENABLED"        default:"true"`
	HMACSecret    string        `env:"POSTHOG_HMAC_SECRET"    required:"false"`
}

Config holds PostHog client configuration. Fields are populated by config.MustLoad[Config]() from environment variables using the env tags.

Jump to

Keyboard shortcuts

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