-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathposthogProvider.tsx
More file actions
110 lines (100 loc) · 3.44 KB
/
posthogProvider.tsx
File metadata and controls
110 lines (100 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use client'
import posthog from 'posthog-js'
import { usePostHog } from 'posthog-js/react'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import { usePathname, useSearchParams } from "next/navigation"
import { Suspense, useEffect } from "react"
import { useSession } from 'next-auth/react'
import { captureEvent } from '@/hooks/useCaptureEvent'
// @see: https://posthog.com/docs/libraries/next-js#capturing-pageviews
function PostHogPageView() {
const pathname = usePathname()
const searchParams = useSearchParams()
const posthog = usePostHog()
useEffect(() => {
if (pathname && posthog) {
let url = window.origin + pathname
if (searchParams.toString()) {
url = url + `?${searchParams.toString()}`
}
captureEvent('$pageview', {
$current_url: url,
});
}
}, [pathname, searchParams, posthog])
return null
}
interface PostHogProviderProps {
children: React.ReactNode
isDisabled: boolean
isPiiEnabled: boolean
posthogApiKey: string
sourcebotVersion: string
sourcebotInstallId: string
}
export function PostHogProvider({
children,
isDisabled,
isPiiEnabled,
posthogApiKey,
sourcebotVersion,
sourcebotInstallId,
}: PostHogProviderProps) {
const { data: session } = useSession();
useEffect(() => {
if (!isDisabled) {
posthog.init(posthogApiKey, {
// @see next.config.mjs for path rewrites to the "/ingest" route.
api_host: "/ingest",
person_profiles: 'identified_only',
capture_pageview: false,
autocapture: false,
// If PII is not enabled, we don't want to capture the following
// default properties that may contain PII.
// @see: https://posthog.com/docs/data/events#default-properties
property_denylist: isPiiEnabled === false ? [
'$current_url',
'$pathname',
'$session_entry_url',
'$session_entry_host',
'$session_entry_pathname',
'$session_entry_referrer',
'$session_entry_referring_domain',
'$referrer',
'$referring_domain',
'$ip',
] : [],
loaded: (posthog) => {
// Include install id & version in all events.
posthog.register({
sourcebot_version: sourcebotVersion,
install_id: sourcebotInstallId,
});
}
});
} else {
console.debug("PostHog telemetry disabled");
}
}, [isDisabled, isPiiEnabled, posthogApiKey, sourcebotInstallId, sourcebotVersion]);
useEffect(() => {
if (!session) {
return;
}
posthog.identify(
session.user.id,
isPiiEnabled === true ? {
email: session.user.email,
name: session.user.name,
} : undefined
);
}, [isPiiEnabled, session]);
return (
<PHProvider client={posthog}>
{/* @see: https://github.com/vercel/next.js/issues/51581 */}
<Suspense fallback={null}>
<PostHogPageView />
</Suspense>
{children}
</PHProvider>
)
}