Skip to content

Commit

Permalink
Fix incorrect initial posthog capture settings (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmar authored Aug 16, 2023
1 parent 4fd1209 commit 206d738
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/components/hooks/usePosthog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ function setAllowTracking(on?: boolean) {
// opt_in_capturing() or opt_out_capturing() without having run
// posthog.init() first. This generally only occurs when running locally.
if (window && process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {
if (on) {
if (on && !posthog.has_opted_in_capturing()) {
posthog.opt_in_capturing()
} else {
// Make sure to capture initial page view after opting in
posthog.capture('$pageview')
} else if (!on && !posthog.has_opted_out_capturing()) {
posthog.opt_out_capturing()
}
}
Expand All @@ -26,8 +28,7 @@ export function usePosthog() {
api_host:
process.env.NEXT_PUBLIC_POSTHOG_API_HOST ||
'https://posthog.plural.sh',
opt_out_capturing_by_default:
window.Cookiebot?.consent?.statistics ?? false,
opt_out_capturing_by_default: !window.Cookiebot?.consent?.statistics,
loaded: () => {
setAllowTracking(window.Cookiebot?.consent?.statistics)
},
Expand Down

0 comments on commit 206d738

Please sign in to comment.