@@ -4,6 +4,7 @@ import { usePathname, useSearchParams } from "next/navigation"
44import posthog from "posthog-js"
55import { PostHogProvider as OriginalPostHogProvider } from "posthog-js/react"
66import { useEffect , Suspense } from "react"
7+ import { hasConsent } from "@/lib/analytics/consent-manager"
78
89function PageViewTracker ( ) {
910 const pathname = usePathname ( )
@@ -49,7 +50,10 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
4950 )
5051 }
5152
52- // Initialize PostHog with cookieless mode support
53+ // Check if user has already consented to cookies
54+ const userHasConsented = hasConsent ( )
55+
56+ // Initialize PostHog with appropriate persistence based on consent
5357 posthog . init ( posthogKey , {
5458 api_host : posthogHost || "https://us.i.posthog.com" ,
5559 capture_pageview : false , // We handle pageview tracking manually
@@ -61,7 +65,7 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
6165 save_referrer : true , // Save referrer information
6266 save_campaign_params : true , // Save UTM parameters
6367 respect_dnt : true , // Respect Do Not Track
64- persistence : " memory", // Default persistence with cookies
68+ persistence : userHasConsented ? "localStorage+cookie" : " memory", // Use localStorage if consented, otherwise memory-only
6569 opt_out_capturing_by_default : false , // Start tracking immediately
6670 } )
6771 }
0 commit comments