@@ -5,39 +5,40 @@ import { usePathname, useSearchParams } from "next/navigation"
55import posthog from "posthog-js"
66import { PostHogProvider } from "posthog-js/react"
77
8- if ( typeof window !== "undefined" ) {
9- posthog . init ( process . env . NEXT_PUBLIC_POSTHOG_API_KEY ! , {
10- api_host : "https://app.posthog.com" ,
11- capture_pageview : true ,
12- session_recording : {
13- maskAllInputs : false ,
14- } ,
15- // Enable debug mode in development
16- loaded : ( posthog ) => {
17- if ( process . env . NODE_ENV === "development" ) posthog . debug ( )
18- } ,
19- } )
20- }
8+ import {
9+ capturePostHogEvent ,
10+ initPostHog ,
11+ isPostHogEnabled ,
12+ } from "@/lib/posthog"
13+
14+ initPostHog ( )
2115
2216export function PostHogPageview ( ) : React . ReactNode {
2317 const pathname = usePathname ( )
2418 const searchParams = useSearchParams ( )
2519
2620 useEffect ( ( ) => {
27- if ( pathname ) {
28- let url = window . origin + pathname
29- if ( searchParams && searchParams . toString ( ) ) {
30- url = url + `?${ searchParams . toString ( ) } `
31- }
32- posthog . capture ( "$pageview" , {
33- $current_url : url ,
34- } )
21+ if ( ! isPostHogEnabled || ! pathname ) {
22+ return
23+ }
24+
25+ let url = window . origin + pathname
26+ if ( searchParams && searchParams . toString ( ) ) {
27+ url = url + `?${ searchParams . toString ( ) } `
3528 }
29+
30+ capturePostHogEvent ( "$pageview" , {
31+ $current_url : url ,
32+ } )
3633 } , [ pathname , searchParams ] )
3734
3835 return < > </ >
3936}
4037
4138export function PHProvider ( { children } : { children : React . ReactNode } ) {
39+ if ( ! isPostHogEnabled ) {
40+ return children
41+ }
42+
4243 return < PostHogProvider client = { posthog } > { children } </ PostHogProvider >
4344}
0 commit comments