Posthog

Tracks analytics events and feature flags in PostHog from within a SpacetimeDB module, with automatic batched delivery, retries, and admin controls.

  • TypeScript
npm i @spacetimedb/posthog

A SpacetimeDB module that connects your application to PostHog for product analytics. It sits inside your module as a submodule (or runs standalone), queuing events transactionally alongside your own database writes and delivering them to PostHog in the background with automatic retries.

  • E-commerce order tracking: Record purchase completions, cart abandons, or refunds atomically with the database changes that represent those events, so analytics and application state never diverge.
  • User lifecycle events: Capture account creation, subscription changes, or churn signals from within the same reducer that performs the underlying mutation.
  • Feature flag evaluation: Gate new features at the server side by querying PostHog flags during a reducer, keeping flag logic out of client code.
  • Audit-friendly analytics pipelines: Administrators can inspect the outbox and delivery log, requeue failed events after fixing credentials, and flush events on demand without touching application code.
  • Multi-tenant SaaS modules: Embed the submodule into a larger SpacetimeDB module so analytics share the same deployment and transaction boundaries as the rest of the product.

When your application performs an action worth tracking, it places an event into an outbox table as part of the same database transaction. Because the event is written transactionally, it either lands alongside your application data or not at all. Each event carries a stable identifier so PostHog can deduplicate it if it is delivered more than once.

A background scheduler runs every few seconds, picks up queued events in batches, and sends them to PostHog. If a delivery attempt fails due to a network problem or a temporary server error, the module waits an increasing amount of time before retrying, up to a 24-hour window. Permanent errors (such as a malformed event or an invalid API key) mark the event as rejected immediately rather than retrying indefinitely. Administrators can view the current outbox and recent delivery attempts through subscribable views, and can requeue failed events after correcting configuration.

Access to all delivery controls and configuration is restricted to designated administrators. Application reducers are expected to wrap the event-queuing call inside their own authorization logic, so analytics operations cannot be triggered by arbitrary clients or roll back the host reducer if something goes wrong with the event data.

Related modules

Agents

Stores conversations and runs AI assistants inside your SpacetimeDB module, with tools that can read and update your application's data.

SpacetimeDB0/wk

Auth

Adds user accounts, password and OAuth sign-in, email verification, and session management to a SpacetimeDB module. Exposes helpers to identify the caller in reducers and HTTP routes.

SpacetimeDB0/wk

Authz

Roles, scopes and grants for multi-user modules: wildcard permissions, role inheritance, scope trees, expiring grants, invite links, an audit log and row-level security, with no privilege escalation.

Lethalchip0/wk
Submodule

Edit details

One short paragraph describing the module.

A few bullets for scenarios where this is a good fit.

A few short paragraphs at the conceptual level.