/** * PostHog usage analytics. EU-hosted project, shared with substandard.blog: * https://eu.posthog.com/project/244032 * * Unlike the frontpage, this uses posthog-js's default persistence, so a * visitor is recognized across visits through a cookie and localStorage. The * cookie is host-only: on .substandard.blog it would reach the frontpage, * which stores nothing. * * Nothing here identifies an account. Autocapture records the text and href of * what was clicked, so anything that renders a handle, a DID or an at:// link * carries `ph-no-capture`. before_send removes the OAuth callback parameters * and the hash route's query, which names thread URIs and so DIDs. */ import type { CaptureResult } from 'posthog-js' /** Write-only project key. It ships in every page and cannot read data. */ const KEY = 'phc_yBvzGPADSPgFV7hBaHJWQxHa4ZniXUhdd6nkqaHgk2Mi' export const API_HOST = 'https://eu.i.posthog.com' /** posthog-js loads its lazy extensions from here; the CSP must allow it. */ export const ASSETS_HOST = 'https://eu-assets.i.posthog.com' /** The only hostname that reports, so forks and previews stay out of the project. */ const SITE_HOST = 'threadbare.substandard.blog' const OAUTH_PARAMS = ['code', 'state', 'iss'] export function scrubUrl(value: string): string { let url: URL try { url = new URL(value) } catch { return value } if (url.protocol !== 'http:' && url.protocol !== 'https:') return value for (const name of OAUTH_PARAMS) url.searchParams.delete(name) const query = url.hash.indexOf('?') if (query !== -1) url.hash = url.hash.slice(0, query) return url.href } function scrub(value: unknown): unknown { if (typeof value === 'string') return scrubUrl(value) if (Array.isArray(value)) return value.map(scrub) if (value && typeof value === 'object') { return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, scrub(item)])) } return value } export function beforeSend(event: CaptureResult | null): CaptureResult | null { if (!event) return event event.properties = scrub(event.properties) as CaptureResult['properties'] if (event.$set) event.$set = scrub(event.$set) as CaptureResult['$set'] if (event.$set_once) event.$set_once = scrub(event.$set_once) as CaptureResult['$set_once'] return event } export type EventProperties = Record export type ScanStage = 'load' | 'scan' | 'refresh' export type ExportKind = 'copy_markdown' | 'copy_link' | 'download' export type ExportSurface = 'thread' | 'row' let capture: ((event: string, properties: EventProperties) => void) | undefined /** Send an explicit event. Neither the name nor the properties carry identifiers. */ export function captureEvent(event: string, properties: EventProperties): void { capture?.(event, properties) } function bucket(value: number, stops: readonly [number, string][], last: string): string { for (const [limit, label] of stops) if (value < limit) return label return last } export function durationBucket(ms: number): string { return bucket( ms, [ [1_000, '<1s'], [5_000, '1-5s'], [15_000, '5-15s'], [60_000, '15-60s'], [300_000, '1-5m'], ], '5m+', ) } export function rowsBucket(rows: number): string { return bucket( rows, [ [1, '0'], [10, '1-9'], [50, '10-49'], [200, '50-199'], ], '200+', ) } export function rankBucket(rank: number): string { if (!Number.isFinite(rank) || rank < 1) return 'unknown' if (rank <= 5) return '1-5' if (rank <= 20) return '6-20' if (rank <= 50) return '21-50' return '51+' } export function errorStatus(error: unknown): string { const message = error instanceof Error ? error.message : String(error) return message.match(/\b([45]\d\d)\b/)?.[1] ?? 'unknown' } export function scanDoneProperties(input: { durationMs: number rows: number truncated: boolean fromCache: boolean }): EventProperties { return { duration_bucket: durationBucket(input.durationMs), rows_bucket: rowsBucket(input.rows), truncated: input.truncated, from_cache: input.fromCache, } } export function scanFailedProperties(stage: ScanStage, error: unknown): EventProperties { return { stage, status: errorStatus(error) } } export function threadOpenedProperties(rank: number): EventProperties { return { rank_bucket: rankBucket(rank) } } export function exportProperties( kind: ExportKind, surface: ExportSurface, ok: boolean, ): EventProperties { return { kind, surface, ok } } let started = false /** Load posthog-js as its own chunk, after the page, and only on the real site. */ export async function startAnalytics(): Promise { if (started || !import.meta.env.PROD || location.hostname !== SITE_HOST) return started = true const posthog = (await import('posthog-js')).default posthog.init(KEY, { api_host: API_HOST, ui_host: 'https://eu.posthog.com', defaults: '2025-05-24', before_send: beforeSend, cross_subdomain_cookie: false, // A recording would show the signed-in handle and the account's posts. disable_session_recording: true, // Survey widgets inject inline styles the CSP refuses. disable_surveys: true, }) capture = (event, properties) => posthog.capture(event, properties) }