Files
OpenFrame/lib/analytics/bots.ts
T
yusufipk 7ca5abd041 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.
2026-08-01 20:00:27 +03:00

43 lines
2.0 KiB
TypeScript

// Traffic that is not a person.
//
// This matters more than it looks. Visitors are the denominator of every
// conversion rate in the scoreboard, so counting a crawler as a visit does not
// add noise evenly: it quietly makes every channel look worse, and the channels
// that attract the most crawling (an indexed landing page, a GitHub README link)
// look worst of all.
const BOT_PATTERN =
/bot\b|bots\b|crawler|spider|crawl|slurp|facebookexternalhit|embedly|quora link preview|whatsapp|telegram|discordbot|slackbot|preview|monitor|uptime|pingdom|curl\/|wget\/|python-requests|python-urllib|scrapy|axios\/|node-fetch|go-http-client|okhttp|java\/|headlesschrome|phantomjs|lighthouse|semrush|ahrefs|mj12|dotbot|petalbot|bytespider|gptbot|claudebot|ccbot/i;
/**
* A missing user agent counts as a bot. Every real browser sends one, so the
* blank case is a script that did not bother.
*/
export function isLikelyBot(userAgent: string | null | undefined): boolean {
if (typeof userAgent !== 'string') return true;
const value = userAgent.trim();
if (!value) return true;
return BOT_PATTERN.test(value);
}
/**
* Whether a request is a real page load rather than a prefetch, an asset or a
* client-side navigation payload.
*
* Next prefetches the register page as soon as a CTA scrolls into view, so
* without this the funnel would show more signup starts than landing views.
*/
export function isCountableDocumentRequest(headers: Headers): boolean {
if (headers.get('sec-purpose')?.includes('prefetch')) return false;
if (headers.get('purpose') === 'prefetch') return false;
if (headers.get('next-router-prefetch')) return false;
// An RSC navigation is the same visitor moving inside the app, not a new view.
if (headers.get('rsc')) return false;
const dest = headers.get('sec-fetch-dest');
if (dest) return dest === 'document';
// Older browsers and anything behind a proxy that strips fetch metadata.
return headers.get('accept')?.includes('text/html') ?? false;
}