/** * 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 } 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, }) }