import posthog from 'posthog-js' const POSTHOG_KEY = process.env.NEXT_PUBLIC_POSTHOG_KEY const POSTHOG_HOST = process.env.NEXT_PUBLIC_POSTHOG_HOST ?? 'https://eu.i.posthog.com' const COOKIE_CONSENT_KEY = 'innungsapp_cookie_consent' const COOKIE_CONSENT_EVENT = 'innungsapp:cookie-consent-granted' declare global { interface Window { __innungsappPosthogInitialized?: boolean } } export function initPosthog() { if (typeof window === 'undefined' || !POSTHOG_KEY) return false if (window.__innungsappPosthogInitialized) return true posthog.init(POSTHOG_KEY, { api_host: POSTHOG_HOST, defaults: '2026-01-30', capture_pageview: false, respect_dnt: true, }) window.__innungsappPosthogInitialized = true return true } if (typeof window !== 'undefined' && POSTHOG_KEY) { const consent = window.localStorage.getItem(COOKIE_CONSENT_KEY) if (consent === 'accepted') { initPosthog() } window.addEventListener(COOKIE_CONSENT_EVENT, initPosthog) }