What it is
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.
Where it fits
- 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.
How it works
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.

