Documentation
¶
Overview ¶
Package posthog is the packaged, one-call version of the PostHog recipe from gofastr's analytics-recipes docs: PostHog's script and ingestion endpoints served first-party through battery/relay, a host-authored bootstrap that loads the real posthog-js loader through the relay, identity from the app's session via a same-origin whoami endpoint, and pageviews that track GoFastr's client-side navigation.
This is an integration, not one of this repo's sandboxed heavy-JS plugins: posthog-js instruments the whole host document, so it runs in the host page, unfenced by design. The isolation story here is the relay's — the visitor's browser talks only to your origin, the strict default CSP stays untouched, and no third-party cookie ever lands on it. See posthog/README.md for the full traffic map.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Key is the PostHog project API key (phc_...). Required.
//
// It is a public project identifier the browser ships on every
// beacon anyway, not a secret — which is why New panics on the key
// shapes that ARE secrets (phx_ personal, sk_ server): one of
// those baked into the served bootstrap leaks to every visitor.
Key string
// Region is the project's region: "us" (the default) or "eu". It
// picks both relay upstreams and the ui_host the bootstrap
// configures. Anything else panics at New.
Region string
// SelfHost points every route — assets, ingestion, and the
// bootstrap's ui_host — at one self-hosted PostHog origin
// (e.g. "http://localhost:8000" for the docker hobby deploy;
// loopback http is the only http the relay accepts). Mutually
// exclusive with Region: a self-hosted instance has no region.
SelfHost string
// Path overrides the relay mount. Default relay.DefaultPath
// ("/__gofastr/t"). Every route this package serves — ph/, ph-assets/,
// boot.js, whoami — lives under it; relay.New validates it.
Path string
// SessionReplay raises the ingestion route's request-body cap from
// the relay's 8 MiB default to 64 MiB, the size PostHog
// session-replay uploads can reach. Off by default, deliberately:
// the cap is an egress number, and every accepted byte is billed
// to your bandwidth.
SessionReplay bool
// RespectDNT makes the bootstrap a no-op for visitors whose browser
// reports Do-Not-Track: no SDK script loads, no beacon fires.
RespectDNT bool
// PersonProfiles sets posthog-js's person_profiles init option:
// "" (the default) omits it and the SDK uses its own default
// ("identified_only"), or one of "identified_only", "always",
// "never". Anything else panics at New. Read it as a billing
// question: "always" creates a person for every anonymous visitor,
// which is exactly what "never" is for avoiding.
PersonProfiles string
// Identify resolves the visitor's identity for the whoami endpoint.
// Default: handler.GetUser with the recipes' normalization — a
// string principal passes through, a fmt.Stringer is String()ed,
// anything else (or nobody) is anonymous. Return ok=false to answer
// anonymous regardless of the session.
Identify func(*http.Request) (string, bool)
}
Config constructs a Plugin with New. Key is the only required field; the zero value of the rest is the recommended posture.
type Plugin ¶
Plugin is the packaged PostHog integration: a framework.Plugin that embeds the battery/relay instance it is built on (so Base() returns the mount) and adds the rendered bootstrap, its serving route, and the identity endpoint. Construct with New, register with App.RegisterPlugin.
func New ¶
New validates cfg and constructs the Plugin. It panics on invalid configuration with a message prefixed "posthog:" — a mistyped key shape or region is a construction-time programmer error, the same posture as relay.New. Path validation (and everything else about the relay table) panics inside relay.New with its own "relay:" prefix.
func (*Plugin) Attach ¶
Attach registers the bootstrap on the host in one call: h.RegisterExternalScript(p.ScriptURL()). The parameter is the one-method interface *uihost.UIHost already satisfies, so hosts wire this without this package reaching for the concrete host type.
func (*Plugin) Init ¶
Init wires the integration: the embedded relay's routes (ph-assets/, ph/), the rendered bootstrap served at {mount}/boot.js with the framework's versioned-script policy (strong ETag, immutable on a matching ?v=), and the identity endpoint at {mount}/whoami.
func (*Plugin) Name ¶
Name implements framework.Plugin. It shadows the embedded relay's "relay" so the plugin registers (and fails, and logs) under its own name.