import { IS_PREVIEW } from '@/lib/constants' const METRICS_ENDPOINT = 'https://misc-functions.vercel.app/api/metrics' type FrontendMetricName = 'pageview' | 'global_crash' /** * Fire-and-forget frontend metric. Sent as text/plain so the request stays * CORS-simple (no preflight). Failures are intentionally swallowed. */ export function sendFrontendMetric(metricName: FrontendMetricName): void { try { fetch(METRICS_ENDPOINT, { method: 'POST', keepalive: true, credentials: 'omit', headers: { 'Content-Type': 'text/plain' }, body: JSON.stringify({ environment: 'staging', events: [{ metric_name: metricName, value: 1, metadata: { app: 'docs' } }], }), }).catch(() => {}) } catch { // fetch can throw synchronously (e.g. invalid arguments); never surface it } } const countedErrors = new WeakSet() /** * Counts one `global_crash` metric per caught error. Call it only for crashes * that reach a crash screen. */ export function reportGlobalCrashMetric(error: unknown) { if (!IS_PREVIEW) return try { if (typeof error === 'object' && error !== null) { if (countedErrors.has(error)) return countedErrors.add(error) } sendFrontendMetric('global_crash') } catch { // Never let metrics break the error handler } }