mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat(analytics): record where paying customers actually came from
Adds first-party acquisition attribution and a sixteen-event funnel, written to this deployment's own database and read back on /admin/growth. Nothing is sent anywhere else, and the whole subsystem is off unless OPENFRAME_ENABLE_ANALYTICS is set, so a self-hosted instance carries the tables empty and pays nothing. The proxy gives a visitor an anonymous id and stores what brought them in two first-party cookies; signup copies that onto the account and claims the events the visitor produced before they had one, which is what joins the two halves of the funnel. Recording happens where each step actually happens rather than in the browser: an ad blocker cannot undercount landing views, and blocking rates differ by channel, so an undercounted denominator would have made GitHub traffic look like it converts better than it does. Every event carries a dedupe key on a UNIQUE column, so "recorded exactly once" is a property of the schema rather than of fifteen call sites. Subscription events are derived by comparing the row being overwritten with the row being written inside the existing Stripe sync, which makes them order-independent and replay-safe. The scoreboard reports step-to-step conversion with the denominator beside it, and splits by source over a rolling 28-day window rather than a week: at this volume a weekly per-source cell holds single digits, and a percentage computed from three visits reads exactly as confidently as one computed from three hundred. "How did you hear about us?" is asked on the first onboarding screen, not on the registration form. The number being measured is the signup conversion rate, and a question added to that form would move it.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Signup is the seam where an anonymous visitor becomes an account, so it is the
|
||||
// one place the two halves of the funnel are joined. Both ways of creating an
|
||||
// account (the credentials form and an OAuth provider) go through here, because
|
||||
// a channel that only shows up for one of them is worse than no channel at all.
|
||||
|
||||
import { eventKey, recordEvent, attachAcquisitionToUser } from '@/lib/analytics/record';
|
||||
import { readVisitorContext, type VisitorContext } from '@/lib/analytics/visitor';
|
||||
import { isProductAnalyticsEnabled } from '@/lib/feature-flags';
|
||||
|
||||
const NO_VISITOR: VisitorContext = { anonymousId: null, firstTouch: null };
|
||||
|
||||
/**
|
||||
* The visitor context of the request being handled.
|
||||
*
|
||||
* For the OAuth path there is no NextRequest to read: the account is created by
|
||||
* the adapter, from inside a NextAuth event. `cookies()` still resolves there,
|
||||
* and when it does not the signup is simply recorded without a channel rather
|
||||
* than not recorded at all.
|
||||
*/
|
||||
export async function readVisitorContextFromHeaders(): Promise<VisitorContext> {
|
||||
if (!isProductAnalyticsEnabled()) return NO_VISITOR;
|
||||
try {
|
||||
const { cookies } = await import('next/headers');
|
||||
return readVisitorContext(await cookies());
|
||||
} catch {
|
||||
return NO_VISITOR;
|
||||
}
|
||||
}
|
||||
|
||||
export async function recordSignupCompleted(params: {
|
||||
userId: string;
|
||||
visitor: VisitorContext;
|
||||
}): Promise<void> {
|
||||
if (!isProductAnalyticsEnabled()) return;
|
||||
|
||||
await attachAcquisitionToUser({
|
||||
userId: params.userId,
|
||||
anonymousId: params.visitor.anonymousId,
|
||||
touch: params.visitor.firstTouch,
|
||||
});
|
||||
|
||||
await recordEvent({
|
||||
name: 'SIGNUP_COMPLETED',
|
||||
dedupeKey: eventKey('SIGNUP_COMPLETED', params.userId),
|
||||
userId: params.userId,
|
||||
anonymousId: params.visitor.anonymousId,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user