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.
131 lines
4.9 KiB
TypeScript
131 lines
4.9 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
classifyChannel,
|
|
extractReferrerHost,
|
|
normalizeHost,
|
|
sanitizeLandingPath,
|
|
sanitizeTag,
|
|
} from '@/lib/analytics/channel';
|
|
|
|
// Every expected value below is written by hand. Deriving them from the lookup
|
|
// tables in the module would mean deleting an entry from a table also deletes
|
|
// its own test case.
|
|
|
|
describe('sanitizeTag', () => {
|
|
it('lowercases and trims', () => {
|
|
expect(sanitizeTag(' GitHub ')).toBe('github');
|
|
});
|
|
|
|
it('rejects a tag carrying markup or control characters', () => {
|
|
expect(sanitizeTag('<script>')).toBeNull();
|
|
expect(sanitizeTag('news\nletter')).toBeNull();
|
|
});
|
|
|
|
it('caps the length at 64 characters', () => {
|
|
expect(sanitizeTag('a'.repeat(200))).toHaveLength(64);
|
|
});
|
|
|
|
it('treats an empty or non-string value as absent', () => {
|
|
expect(sanitizeTag(' ')).toBeNull();
|
|
expect(sanitizeTag(null)).toBeNull();
|
|
expect(sanitizeTag(undefined)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('normalizeHost', () => {
|
|
it('drops the www prefix and the port', () => {
|
|
expect(normalizeHost('WWW.GitHub.com:443')).toBe('github.com');
|
|
});
|
|
|
|
it('rejects a value that is not a host', () => {
|
|
expect(normalizeHost('not a host')).toBeNull();
|
|
expect(normalizeHost('https://github.com')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('extractReferrerHost', () => {
|
|
it('returns the host of a full referrer URL', () => {
|
|
expect(extractReferrerHost('https://news.ycombinator.com/item?id=1')).toBe(
|
|
'news.ycombinator.com'
|
|
);
|
|
});
|
|
|
|
it('drops the path and query, so a share token cannot be stored', () => {
|
|
expect(extractReferrerHost('https://example.com/share/[email protected]')).toBe(
|
|
'example.com'
|
|
);
|
|
});
|
|
|
|
it('ignores our own host, because that is a click inside the site', () => {
|
|
expect(extractReferrerHost('https://open-frame.net/pricing', 'open-frame.net')).toBeNull();
|
|
expect(extractReferrerHost('https://www.open-frame.net/pricing', 'open-frame.net')).toBeNull();
|
|
});
|
|
|
|
it('returns null for a missing or unparseable referrer', () => {
|
|
expect(extractReferrerHost(null)).toBeNull();
|
|
expect(extractReferrerHost('android-app://com.example')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('sanitizeLandingPath', () => {
|
|
it('keeps the path and drops the query string', () => {
|
|
expect(sanitizeLandingPath('/vs/frameio')).toBe('/vs/frameio');
|
|
});
|
|
|
|
it('falls back to / for anything that is not a path', () => {
|
|
expect(sanitizeLandingPath('https://open-frame.net/x')).toBe('/');
|
|
expect(sanitizeLandingPath(null)).toBe('/');
|
|
});
|
|
});
|
|
|
|
describe('classifyChannel', () => {
|
|
it('is DIRECT with no tags and no referrer', () => {
|
|
expect(classifyChannel({})).toBe('DIRECT');
|
|
});
|
|
|
|
it('reads the referring host when there are no tags', () => {
|
|
expect(classifyChannel({ referrerHost: 'github.com' })).toBe('GITHUB');
|
|
expect(classifyChannel({ referrerHost: 'gist.github.com' })).toBe('GITHUB');
|
|
expect(classifyChannel({ referrerHost: 'youtu.be' })).toBe('YOUTUBE');
|
|
expect(classifyChannel({ referrerHost: 'www.producthunt.com' })).toBe('REVIEW_LINK');
|
|
expect(classifyChannel({ referrerHost: 'news.ycombinator.com' })).toBe('COMMUNITY');
|
|
});
|
|
|
|
it('treats every Google country domain as search', () => {
|
|
expect(classifyChannel({ referrerHost: 'google.com' })).toBe('GOOGLE');
|
|
expect(classifyChannel({ referrerHost: 'google.com.tr' })).toBe('GOOGLE');
|
|
expect(classifyChannel({ referrerHost: 'news.google.co.uk' })).toBe('GOOGLE');
|
|
});
|
|
|
|
it('does not mistake a lookalike domain for the real one', () => {
|
|
expect(classifyChannel({ referrerHost: 'notgithub.com' })).toBe('REFERRAL');
|
|
expect(classifyChannel({ referrerHost: 'google.com.evil.example' })).toBe('REFERRAL');
|
|
});
|
|
|
|
it('counts an unrecognised site that links to us as a referral', () => {
|
|
expect(classifyChannel({ referrerHost: 'someblog.example' })).toBe('REFERRAL');
|
|
});
|
|
|
|
it('prefers an explicit utm_source over the referring host', () => {
|
|
expect(classifyChannel({ utmSource: 'youtube', referrerHost: 'google.com' })).toBe('YOUTUBE');
|
|
});
|
|
|
|
it('reads a utm_source that was written as a domain', () => {
|
|
expect(classifyChannel({ utmSource: 'github.com' })).toBe('GITHUB');
|
|
});
|
|
|
|
it('files a tagged campaign we do not recognise as OTHER, not DIRECT', () => {
|
|
expect(classifyChannel({ utmSource: 'conference-flyer' })).toBe('OTHER');
|
|
});
|
|
|
|
it('lets the medium that names the motion win over the source that names the place', () => {
|
|
expect(classifyChannel({ utmSource: 'linkedin', utmMedium: 'outbound' })).toBe('OUTBOUND');
|
|
expect(classifyChannel({ utmSource: 'github', utmMedium: 'email' })).toBe('OUTBOUND');
|
|
expect(classifyChannel({ utmSource: 'someone', utmMedium: 'referral' })).toBe('REFERRAL');
|
|
});
|
|
|
|
it('ignores a source that fails sanitizing and falls back to the referrer', () => {
|
|
expect(classifyChannel({ utmSource: '<script>', referrerHost: 'youtube.com' })).toBe('YOUTUBE');
|
|
});
|
|
});
|