Skip to content

Commit

Permalink
chore: disable posthog default $pageview capture (#2645)
Browse files Browse the repository at this point in the history
chore: disable default page view capture
  • Loading branch information
prestonbourne authored Dec 10, 2024
1 parent 04e040d commit e2efda3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
38 changes: 35 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
"openapi-types": "^12.1.3",
"p-map": "^4.0.0",
"pkg-dir": "^5.0.0",
"posthog-js": "^1.194.6",
"probe-image-size": "^7.2.3",
"promise-polyfill": "^8.2.1",
"query-string": "^7.1.1",
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/use-posthog-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

import { useEffect } from 'react'
import { useRouter } from 'next/router'
import { type PostHog } from 'posthog-js'

declare global {
/*
Loaded via a posthog script tag in GTM based on datagrail consent
*/
interface Window {
posthog?: $TSFixMe
posthog?: PostHog
}
}

Expand All @@ -17,7 +21,7 @@ function onRouteChangeComplete() {
* PostHog automatically captures a `pageview` for initial page loads.
* Subsequent client-side navigation events have to be captured manually,
* which is why we have to set up this `onRouteChangeComplete` event.
*
*
* PostHog documentation for capturing pageviews in SPA with the JS web installation:
* https://posthog.com/docs/libraries/js#single-page-apps-and-pageviews
*/
Expand All @@ -33,7 +37,9 @@ export default function usePostHogPageAnalytics(): void {

useEffect(() => {
// Ensures code only runs if PostHog has been initialized
if (!window?.posthog?.capture) return
if (!window?.posthog) return

window.posthog.config.capture_pageview = false

// Record a pageview when route changes
router.events.on('routeChangeComplete', onRouteChangeComplete)
Expand Down

0 comments on commit e2efda3

Please sign in to comment.