2025-12-25 13:53:41 -08:00
|
|
|
import posthog from "posthog-js";
|
|
|
|
|
|
|
|
|
|
if (process.env.NEXT_PUBLIC_POSTHOG_KEY) {
|
|
|
|
|
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
|
2025-12-25 14:22:48 -08:00
|
|
|
// Use reverse proxy to bypass ad blockers
|
|
|
|
|
api_host: "/ingest",
|
|
|
|
|
// Required for toolbar and other UI features to work correctly
|
|
|
|
|
ui_host: "https://us.posthog.com",
|
2025-12-25 13:53:41 -08:00
|
|
|
defaults: "2025-11-30",
|
|
|
|
|
// Disable automatic pageview capture, as we capture manually with PostHogProvider
|
|
|
|
|
// This ensures proper pageview tracking with Next.js client-side navigation
|
|
|
|
|
capture_pageview: "history_change",
|
|
|
|
|
// Enable session recording
|
|
|
|
|
capture_pageleave: true,
|
2026-02-20 09:37:49 -05:00
|
|
|
before_send: (event) => {
|
|
|
|
|
if (event.properties) {
|
|
|
|
|
event.properties.$set = {
|
|
|
|
|
...event.properties.$set,
|
|
|
|
|
last_seen_at: new Date().toISOString(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return event;
|
|
|
|
|
},
|
2026-01-27 13:23:45 -05:00
|
|
|
loaded: (posthog) => {
|
|
|
|
|
// Expose PostHog to window for console access and toolbar
|
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
window.posthog = posthog;
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-12-25 13:53:41 -08:00
|
|
|
});
|
|
|
|
|
}
|
2026-01-27 13:23:45 -05:00
|
|
|
|
|
|
|
|
// Always expose posthog to window for debugging/toolbar access
|
|
|
|
|
// This allows testing feature flags even without POSTHOG_KEY configured
|
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
window.posthog = posthog;
|
|
|
|
|
}
|