33import { usePathname , useSearchParams } from "next/navigation"
44import posthog from "posthog-js"
55import { PostHogProvider as OriginalPostHogProvider } from "posthog-js/react"
6- import { useEffect , Suspense , useState } from "react"
7- import { hasConsent , onConsentChange } from "@/lib/analytics/consent-manager"
6+ import { useEffect , Suspense } from "react"
7+ import { hasConsent } from "@/lib/analytics/consent-manager"
88
99function PageViewTracker ( ) {
1010 const pathname = usePathname ( )
@@ -28,11 +28,9 @@ function PageViewTracker() {
2828}
2929
3030export function PostHogProvider ( { children } : { children : React . ReactNode } ) {
31- const [ isInitialized , setIsInitialized ] = useState ( false )
32-
3331 useEffect ( ( ) => {
34- // Initialize PostHog only on the client side AND when consent is given
35- if ( typeof window !== "undefined" ) {
32+ // Initialize PostHog immediately on the client side
33+ if ( typeof window !== "undefined" && ! posthog . __loaded ) {
3634 const posthogKey = process . env . NEXT_PUBLIC_POSTHOG_KEY
3735 const posthogHost = process . env . NEXT_PUBLIC_POSTHOG_HOST
3836
@@ -52,48 +50,32 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
5250 )
5351 }
5452
55- const initializePosthog = ( ) => {
56- if ( ! isInitialized ) {
57- posthog . init ( posthogKey , {
58- api_host : posthogHost || "https://us.i.posthog.com" ,
59- capture_pageview : false ,
60- loaded : ( posthogInstance ) => {
61- if ( process . env . NODE_ENV === "development" ) {
62- posthogInstance . debug ( )
63- }
64- } ,
65- respect_dnt : true , // Respect Do Not Track
66- } )
67- setIsInitialized ( true )
68- }
69- }
70-
71- // Check initial consent status
72- if ( hasConsent ( ) ) {
73- initializePosthog ( )
74- }
53+ // Check if user has already consented to cookies
54+ const userHasConsented = hasConsent ( )
7555
76- // Listen for consent changes
77- const unsubscribe = onConsentChange ( ( consented ) => {
78- if ( consented && ! isInitialized ) {
79- initializePosthog ( )
80- }
56+ // Initialize PostHog with appropriate persistence based on consent
57+ posthog . init ( posthogKey , {
58+ api_host : posthogHost || "https://us.i.posthog.com" ,
59+ capture_pageview : false , // We handle pageview tracking manually
60+ loaded : ( posthogInstance ) => {
61+ if ( process . env . NODE_ENV === "development" ) {
62+ posthogInstance . debug ( )
63+ }
64+ } ,
65+ save_referrer : true , // Save referrer information
66+ save_campaign_params : true , // Save UTM parameters
67+ respect_dnt : true , // Respect Do Not Track
68+ persistence : userHasConsented ? "localStorage+cookie" : "memory" , // Use localStorage if consented, otherwise memory-only
69+ opt_out_capturing_by_default : false , // Start tracking immediately
8170 } )
82-
83- return ( ) => {
84- unsubscribe ( )
85- }
8671 }
87- } , [ isInitialized ] )
72+ } , [ ] )
8873
89- // Only provide PostHog context if it's initialized
9074 return (
9175 < OriginalPostHogProvider client = { posthog } >
92- { isInitialized && (
93- < Suspense fallback = { null } >
94- < PageViewTracker />
95- </ Suspense >
96- ) }
76+ < Suspense fallback = { null } >
77+ < PageViewTracker />
78+ </ Suspense >
9779 { children }
9880 </ OriginalPostHogProvider >
9981 )
0 commit comments