@@ -5,21 +5,25 @@ import { PostHogProvider as PHProvider } from 'posthog-js/react'
55import { usePathname , useSearchParams } from "next/navigation"
66import { Suspense , useEffect } from "react"
77import { env } from '@/env.mjs'
8+ import { useSession } from 'next-auth/react'
9+ import { captureEvent } from '@/hooks/useCaptureEvent'
810
11+ // @see : https://posthog.com/docs/libraries/next-js#capturing-pageviews
912function PostHogPageView ( ) {
1013 const pathname = usePathname ( )
1114 const searchParams = useSearchParams ( )
1215 const posthog = usePostHog ( )
1316
14- // Track pageviews
1517 useEffect ( ( ) => {
1618 if ( pathname && posthog ) {
1719 let url = window . origin + pathname
1820 if ( searchParams . toString ( ) ) {
1921 url = url + `?${ searchParams . toString ( ) } `
2022 }
2123
22- posthog . capture ( '$pageview' , { '$current_url' : url } )
24+ captureEvent ( '$pageview' , {
25+ $current_url : url ,
26+ } ) ;
2327 }
2428 } , [ pathname , searchParams , posthog ] )
2529
@@ -32,34 +36,55 @@ interface PostHogProviderProps {
3236}
3337
3438export function PostHogProvider ( { children, disabled } : PostHogProviderProps ) {
39+ const { data : session } = useSession ( ) ;
40+
3541 useEffect ( ( ) => {
3642 if ( ! disabled && env . NEXT_PUBLIC_POSTHOG_PAPIK ) {
43+ console . debug ( `PostHog telemetry enabled. Cloud environment: ${ env . NEXT_PUBLIC_SOURCEBOT_CLOUD_ENVIRONMENT } ` ) ;
3744 posthog . init ( env . NEXT_PUBLIC_POSTHOG_PAPIK , {
3845 // @see next.config.mjs for path rewrites to the "/ingest" route.
3946 api_host : "/ingest" ,
4047 person_profiles : 'identified_only' ,
4148 capture_pageview : false ,
4249 autocapture : false ,
43- // eslint-disable-next-line @typescript-eslint/no-explicit-any
44- sanitize_properties : ( properties : Record < string , any > , _event : string ) => {
45- // https://posthog.com/docs/libraries/js#config
46- if ( properties [ '$current_url' ] ) {
47- properties [ '$current_url' ] = null ;
48- }
49- if ( properties [ '$ip' ] ) {
50- properties [ '$ip' ] = null ;
51- }
52-
53- return properties ;
54- }
50+ // In self-hosted mode, we don't want to capture the following
51+ // default properties.
52+ // @see : https://posthog.com/docs/data/events#default-properties
53+ property_denylist : env . NEXT_PUBLIC_SOURCEBOT_CLOUD_ENVIRONMENT === undefined ? [
54+ '$current_url' ,
55+ '$pathname' ,
56+ '$session_entry_url' ,
57+ '$session_entry_host' ,
58+ '$session_entry_pathname' ,
59+ '$session_entry_referrer' ,
60+ '$session_entry_referring_domain' ,
61+ '$referrer' ,
62+ '$referring_domain' ,
63+ '$ip' ,
64+ ] : [ ]
5565 } ) ;
5666 } else {
57- console . log ( "PostHog telemetry disabled" ) ;
67+ console . debug ( "PostHog telemetry disabled" ) ;
68+ }
69+ } , [ disabled ] ) ;
70+
71+ useEffect ( ( ) => {
72+ if ( ! session ) {
73+ return ;
74+ }
75+
76+ // Only identify the user if we are running in a cloud environment.
77+ if ( env . NEXT_PUBLIC_SOURCEBOT_CLOUD_ENVIRONMENT !== undefined ) {
78+ posthog . identify ( session . user . id , {
79+ email : session . user . email ,
80+ name : session . user . name ,
81+ } ) ;
5882 }
59- } , [ disabled ] )
83+ } , [ session ] ) ;
6084
6185 return (
6286 < PHProvider client = { posthog } >
87+ { /* @see : https://github.com/vercel/next.js/issues/51581 */ }
6388 < Suspense fallback = { null } >
6489 < PostHogPageView />
6590 </ Suspense >
0 commit comments