Connect Hexclave and Stripe billing analytics to PostHog

Merged
LA
4mo ago
+989-54

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 typecheck
  • cd 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-env failures reproduced on origin/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_id for retry deduplication and $set for current subscription properties.
  • Stripe remains billing source of truth; PostHog receives analytics projections only.

View with [code]smith Autofix with [code]smith
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/identity using 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-js with before_send until identity resolves; add timeout and abortable gating; buffer pageviews/autocapture during revalidation; persist stack_user_id in PostHog/local storage; coalesce refresh; drop buffered events across account changes; replay only when identity matches.
    • Capture idempotent, order-independent Stripe checkout/subscription/invoice events to PostHog; join to user/team $groups; set $insert_id and $set; defer with next/server after (2s cap); emit only for active subscriptions; failures never affect entitlements.

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.