mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
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.
73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
// Reading the acquisition cookies, and recording the events that happen before
|
|
// an account exists.
|
|
//
|
|
// The proxy sets the cookies but cannot write rows (it runs on the edge). These
|
|
// helpers close that gap from Node, and are the reason no visitor event depends
|
|
// on client-side JavaScript running: a landing view is recorded by the server
|
|
// rendering the landing page, so an ad blocker has nothing to block. That is not
|
|
// a purity argument. Blocking rates differ by channel, and an undercounted
|
|
// denominator would make GitHub and Hacker News traffic look like it converts
|
|
// better than it does.
|
|
|
|
import type { AnalyticsEventName } from '@prisma/client';
|
|
import {
|
|
ANONYMOUS_ID_COOKIE,
|
|
FIRST_TOUCH_COOKIE,
|
|
decodeFirstTouch,
|
|
isValidAnonymousId,
|
|
type FirstTouch,
|
|
} from '@/lib/analytics/cookies';
|
|
import { dailyEventKey, recordEvent, recordFirstTouch } from '@/lib/analytics/record';
|
|
import { isProductAnalyticsEnabled } from '@/lib/feature-flags';
|
|
|
|
/** Both `cookies()` from next/headers and `request.cookies` satisfy this. */
|
|
export interface AnalyticsCookieReader {
|
|
get(name: string): { value: string } | undefined;
|
|
}
|
|
|
|
export interface VisitorContext {
|
|
anonymousId: string | null;
|
|
firstTouch: FirstTouch | null;
|
|
}
|
|
|
|
export function readVisitorContext(store: AnalyticsCookieReader): VisitorContext {
|
|
const rawId = store.get(ANONYMOUS_ID_COOKIE)?.value;
|
|
return {
|
|
anonymousId: isValidAnonymousId(rawId) ? rawId : null,
|
|
firstTouch: decodeFirstTouch(store.get(FIRST_TOUCH_COOKIE)?.value),
|
|
};
|
|
}
|
|
|
|
const DIRECT_TOUCH: FirstTouch = {
|
|
channel: 'DIRECT',
|
|
utmSource: null,
|
|
utmMedium: null,
|
|
utmCampaign: null,
|
|
referrerHost: null,
|
|
landingPath: '/',
|
|
};
|
|
|
|
/**
|
|
* Records an event for a visitor with no account, once per visitor per UTC day.
|
|
*
|
|
* The first touch row is written here rather than in the proxy because this is
|
|
* the first moment the visitor is known to be a browser that kept the cookie.
|
|
*/
|
|
export async function recordVisitorEvent(
|
|
name: AnalyticsEventName,
|
|
visitor: VisitorContext
|
|
): Promise<void> {
|
|
if (!isProductAnalyticsEnabled()) return;
|
|
if (!visitor.anonymousId) return;
|
|
|
|
const touch = visitor.firstTouch ?? DIRECT_TOUCH;
|
|
|
|
await recordFirstTouch(visitor.anonymousId, touch);
|
|
await recordEvent({
|
|
name,
|
|
dedupeKey: dailyEventKey(name, visitor.anonymousId),
|
|
anonymousId: visitor.anonymousId,
|
|
channel: touch.channel,
|
|
});
|
|
}
|