|
|
@@ -3,16 +3,15 @@
|
|
|
import { usePathname, useSearchParams } from "next/navigation"
|
|
|
import posthog from "posthog-js"
|
|
|
import { PostHogProvider as OriginalPostHogProvider } from "posthog-js/react"
|
|
|
-import { useEffect, Suspense } from "react"
|
|
|
+import { useEffect, Suspense, useState } from "react"
|
|
|
+import { hasConsent, onConsentChange } from "@/lib/analytics/consent-manager"
|
|
|
|
|
|
-// Create a separate component for analytics tracking that uses useSearchParams
|
|
|
function PageViewTracker() {
|
|
|
const pathname = usePathname()
|
|
|
const searchParams = useSearchParams()
|
|
|
|
|
|
// Track page views
|
|
|
useEffect(() => {
|
|
|
- // Only track page views if PostHog is properly initialized
|
|
|
if (pathname && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
|
|
|
let url = window.location.origin + pathname
|
|
|
if (searchParams && searchParams.toString()) {
|
|
|
@@ -29,8 +28,10 @@ function PageViewTracker() {
|
|
|
}
|
|
|
|
|
|
export function PostHogProvider({ children }: { children: React.ReactNode }) {
|
|
|
+ const [isInitialized, setIsInitialized] = useState(false)
|
|
|
+
|
|
|
useEffect(() => {
|
|
|
- // Initialize PostHog only on the client side
|
|
|
+ // Initialize PostHog only on the client side AND when consent is given
|
|
|
if (typeof window !== "undefined") {
|
|
|
const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY
|
|
|
const posthogHost = process.env.NEXT_PUBLIC_POSTHOG_HOST
|
|
|
@@ -51,27 +52,48 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- posthog.init(posthogKey, {
|
|
|
- api_host: posthogHost || "https://us.i.posthog.com",
|
|
|
- capture_pageview: false, // We'll handle this manually
|
|
|
- loaded: (posthogInstance) => {
|
|
|
- if (process.env.NODE_ENV === "development") {
|
|
|
- // Log to console in development
|
|
|
- posthogInstance.debug()
|
|
|
- }
|
|
|
- },
|
|
|
- respect_dnt: true, // Respect Do Not Track
|
|
|
+ const initializePosthog = () => {
|
|
|
+ if (!isInitialized) {
|
|
|
+ posthog.init(posthogKey, {
|
|
|
+ api_host: posthogHost || "https://us.i.posthog.com",
|
|
|
+ capture_pageview: false,
|
|
|
+ loaded: (posthogInstance) => {
|
|
|
+ if (process.env.NODE_ENV === "development") {
|
|
|
+ posthogInstance.debug()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ respect_dnt: true, // Respect Do Not Track
|
|
|
+ })
|
|
|
+ setIsInitialized(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check initial consent status
|
|
|
+ if (hasConsent()) {
|
|
|
+ initializePosthog()
|
|
|
+ }
|
|
|
+
|
|
|
+ // Listen for consent changes
|
|
|
+ const unsubscribe = onConsentChange((consented) => {
|
|
|
+ if (consented && !isInitialized) {
|
|
|
+ initializePosthog()
|
|
|
+ }
|
|
|
})
|
|
|
- }
|
|
|
|
|
|
- // No explicit cleanup needed for posthog-js v1.231.0
|
|
|
- }, [])
|
|
|
+ return () => {
|
|
|
+ unsubscribe()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, [isInitialized])
|
|
|
|
|
|
+ // Only provide PostHog context if it's initialized
|
|
|
return (
|
|
|
<OriginalPostHogProvider client={posthog}>
|
|
|
- <Suspense fallback={null}>
|
|
|
- <PageViewTracker />
|
|
|
- </Suspense>
|
|
|
+ {isInitialized && (
|
|
|
+ <Suspense fallback={null}>
|
|
|
+ <PageViewTracker />
|
|
|
+ </Suspense>
|
|
|
+ )}
|
|
|
{children}
|
|
|
</OriginalPostHogProvider>
|
|
|
)
|