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.
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { NextRequest } from 'next/server';
|
|
import { auth } from '@/lib/auth';
|
|
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
|
|
import { rateLimit } from '@/lib/rate-limit';
|
|
import { setSelfReportedSource } from '@/lib/analytics/record';
|
|
import { isAcquisitionChannel } from '@/lib/analytics/cookies';
|
|
import { isProductAnalyticsEnabled } from '@/lib/feature-flags';
|
|
|
|
// "How did you hear about us?", answered on the first onboarding screen.
|
|
//
|
|
// It is stored beside the cookie-derived channel rather than instead of it. The
|
|
// cookie is precise but loses cross-device visits and cleared browsers; the
|
|
// answer survives both, and it is the only thing that can name a channel no UTM
|
|
// tag ever carries, like being told about it by a friend.
|
|
export async function POST(request: NextRequest) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return apiErrors.unauthorized();
|
|
}
|
|
|
|
const limited = await rateLimit(request, 'onboarding-complete');
|
|
if (limited) return limited;
|
|
|
|
if (!isProductAnalyticsEnabled()) {
|
|
return apiErrors.badRequest('Analytics are disabled by this host');
|
|
}
|
|
|
|
const body = await request.json().catch(() => null);
|
|
const source = body?.source;
|
|
if (!isAcquisitionChannel(source)) {
|
|
return apiErrors.badRequest('Unknown source');
|
|
}
|
|
|
|
const note = typeof body?.note === 'string' ? body.note : null;
|
|
|
|
await setSelfReportedSource({ userId: session.user.id, selfReported: source, note });
|
|
|
|
const response = successResponse({ recorded: true });
|
|
return withCacheControl(response, 'private, no-store');
|
|
}
|