-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation-client.ts
More file actions
36 lines (34 loc) · 2.16 KB
/
Copy pathinstrumentation-client.ts
File metadata and controls
36 lines (34 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import posthog from "posthog-js";
import { POSTHOG_API_HOST, POSTHOG_UI_HOST, posthogKey } from "@/lib/analytics/posthog";
import { installConsoleCapture } from "@/lib/analytics/console-capture";
// Client-side instrumentation. Runs AFTER the HTML loads but BEFORE React
// hydration (node_modules/next/dist/docs/01-app/03-api-reference/03-file-conventions/
// instrumentation-client.md), so analytics is live as early as possible.
//
// Initialized opt-out-by-default: PostHog is set up here, but captures NOTHING
// (pageviews, autocaptured clicks, exceptions, console logs) until the user
// accepts the cookie-consent banner (components/analytics/cookie-consent.tsx).
// With an empty key (NEXT_PUBLIC_POSTHOG_KEY="") this is a no-op. Wrapped in
// try/catch, per the Next docs, so analytics can never break app startup.
const key = posthogKey();
if (key) {
try {
posthog.init(key, {
api_host: POSTHOG_API_HOST, // same-origin /ingest → reverse-proxied (CSP- and ad-block-safe)
ui_host: POSTHOG_UI_HOST,
defaults: "2026-05-30", // PostHog's recommended defaults bundle (from the project's snippet)
person_profiles: "always", // create profiles for anonymous visitors too (per the project's snippet)
autocapture: true, // product analytics: button/link clicks, form submits, input changes
capture_pageview: false, // web analytics: captured manually on real route changes — see posthog-pageview.tsx.
// (The binder stashes share tokens in the URL via history.replaceState on every tweak; the dated `defaults`
// bundle would otherwise turn on history-based pageview autocapture and fire one on each tweak.)
capture_pageleave: true, // keep page-leave events for time-on-page / bounce
capture_exceptions: true, // Error Tracking: unhandled errors + unhandled promise rejections
disable_session_recording: true, // analytics only — no session replay
opt_out_capturing_by_default: true, // GDPR: capture nothing until the consent banner is accepted
});
installConsoleCapture(); // forward console.error/warn (also respects opt-out)
} catch {
// Analytics is best-effort — never let it break app startup.
}
}