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"
87
98function PageViewTracker ( ) {
109 const pathname = usePathname ( )
@@ -28,11 +27,9 @@ function PageViewTracker() {
2827}
2928
3029export function PostHogProvider ( { children } : { children : React . ReactNode } ) {
31- const [ isInitialized , setIsInitialized ] = useState ( false )
32-
3330 useEffect ( ( ) => {
34- // Initialize PostHog only on the client side AND when consent is given
35- if ( typeof window !== "undefined" ) {
31+ // Initialize PostHog immediately on the client side
32+ if ( typeof window !== "undefined" && ! posthog . __loaded ) {
3633 const posthogKey = process . env . NEXT_PUBLIC_POSTHOG_KEY
3734 const posthogHost = process . env . NEXT_PUBLIC_POSTHOG_HOST
3835
@@ -52,48 +49,29 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
5249 )
5350 }
5451
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- }
75-
76- // Listen for consent changes
77- const unsubscribe = onConsentChange ( ( consented ) => {
78- if ( consented && ! isInitialized ) {
79- initializePosthog ( )
80- }
52+ // Initialize PostHog with cookieless mode support
53+ posthog . init ( posthogKey , {
54+ api_host : posthogHost || "https://us.i.posthog.com" ,
55+ capture_pageview : false , // We handle pageview tracking manually
56+ loaded : ( posthogInstance ) => {
57+ if ( process . env . NODE_ENV === "development" ) {
58+ posthogInstance . debug ( )
59+ }
60+ } ,
61+ save_referrer : true , // Save referrer information
62+ save_campaign_params : true , // Save UTM parameters
63+ respect_dnt : true , // Respect Do Not Track
64+ persistence : "memory" , // Default persistence with cookies
65+ opt_out_capturing_by_default : false , // Start tracking immediately
8166 } )
82-
83- return ( ) => {
84- unsubscribe ( )
85- }
8667 }
87- } , [ isInitialized ] )
68+ } , [ ] )
8869
89- // Only provide PostHog context if it's initialized
9070 return (
9171 < OriginalPostHogProvider client = { posthog } >
92- { isInitialized && (
93- < Suspense fallback = { null } >
94- < PageViewTracker />
95- </ Suspense >
96- ) }
72+ < Suspense fallback = { null } >
73+ < PageViewTracker />
74+ </ Suspense >
9775 { children }
9876 </ OriginalPostHogProvider >
9977 )
0 commit comments