Skip to content

Commit 206d738

Browse files
authored
Fix incorrect initial posthog capture settings (#236)
1 parent 4fd1209 commit 206d738

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/components/hooks/usePosthog.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ function setAllowTracking(on?: boolean) {
99
// opt_in_capturing() or opt_out_capturing() without having run
1010
// posthog.init() first. This generally only occurs when running locally.
1111
if (window && process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
12-
if (on) {
12+
if (on && !posthog.has_opted_in_capturing()) {
1313
posthog.opt_in_capturing()
14-
} else {
14+
// Make sure to capture initial page view after opting in
15+
posthog.capture('$pageview')
16+
} else if (!on && !posthog.has_opted_out_capturing()) {
1517
posthog.opt_out_capturing()
1618
}
1719
}
@@ -26,8 +28,7 @@ export function usePosthog() {
2628
api_host:
2729
process.env.NEXT_PUBLIC_POSTHOG_API_HOST ||
2830
'https://posthog.plural.sh',
29-
opt_out_capturing_by_default:
30-
window.Cookiebot?.consent?.statistics ?? false,
31+
opt_out_capturing_by_default: !window.Cookiebot?.consent?.statistics,
3132
loaded: () => {
3233
setAllowTracking(window.Cookiebot?.consent?.statistics)
3334
},

0 commit comments

Comments
 (0)