Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168/** * 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<string, string | number | boolean>
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<void> { 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)}