Connect Hexclave and Stripe billing analytics to PostHog
Merged
LA
4mo ago+989+989+012345678901234567890123456789-54-54-01234567890123456789
Summary
- identify signed-in web sessions in PostHog with the canonical Hexclave/Stack user id
- emit idempotent Stripe billing lifecycle events against the same identity, including plan, status, amount, and Stripe join keys
- preserve anonymous funnels, keep analytics failures non-authoritative, and disclose account-linked billing analytics in every privacy-policy locale
Testing
cd web && bun run typecheckcd web && bun test tests/stack-analytics-identity.test.ts tests/analytics-identity-route.test.ts tests/stripe-billing-analytics.test.ts tests/stripe-webhook-route.test.ts(21 passed)cd web && bunx eslint ...(changed files)cd web && bun run build- full web suite: 1,149 passed, 147 skipped, 6 pre-existing
client-config-envfailures reproduced onorigin/main
Research
- Hexclave user IDs remain the server-issued identity across authenticated SDK and analytics flows.
- PostHog requires one distinct id across browser and server events; server billing events use
$insert_idfor retry deduplication and$setfor current subscription properties. - Stripe remains billing source of truth; PostHog receives analytics projections only.
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Connect Hexclave/Stack identity to posthog-js and join Stripe billing analytics on the same distinct id. Gate analytics until a server-authenticated identity resolves or times out; buffer during revalidation; drop buffered events on account change; replay only when the identity is stable; billing analytics send async so payments never block.
- New Features
- Identify signed-in sessions via
/api/analytics/identityusing Stack cookie tokens; observe auth with@stackframe/stack(StackProvider); backfill plan; reset on sign-in/out; anonymous checkout stays anonymous; passive refreshes fail-closed. - Gate
posthog-jswithbefore_senduntil identity resolves; add timeout and abortable gating; buffer pageviews/autocapture during revalidation; persiststack_user_idin PostHog/local storage; coalesce refresh; drop buffered events across account changes; replay only when identity matches. - Capture idempotent, order-independent
Stripecheckout/subscription/invoice events to PostHog; join to user/team$groups; set$insert_idand$set; defer withnext/serverafter(2s cap); emit only for active subscriptions; failures never affect entitlements.
- Identify signed-in sessions via
Summary by CodeRabbit
- New Features
- Signed-in activity is now associated with account identities while anonymous sessions remain protected.
- Analytics captures pageviews, checkout, subscription, and invoice payment events with relevant billing details.
- Billing analytics runs asynchronously without affecting successful payment updates.
- Documentation
- Privacy policies across supported languages now describe analytics, billing, and Stripe identifier collection.
- Retention, deletion, and legal-retention terms were clarified, including periods of up to 24 months.
- Tests
- Added coverage for identity handling, logout behavior, billing event mapping, deferred processing, and analytics failures.