🐛(frontend) fix double-counted pageviews in PostHog

Pageviews were being counted twice in PostHog. Refactor the way
pageviews are computed to follow PostHog's documented recommended
pattern.

Verified locally by connecting PostHog to localhost and confirming
that only a single pageview event is emitted per navigation.
This commit is contained in:
lebaudantoine
2026-08-10 17:00:00 +02:00
committed by aleb_the_flash
parent c53a2f8af4
commit 03e90b6178
@@ -1,5 +1,4 @@
import { useEffect } from 'react'
import { useLocation } from 'wouter'
import { type ApiUser } from '@/features/auth/api/ApiUser'
import { useUser } from '@/features/auth/api/useUser'
import { getPosthog } from '../utils'
@@ -31,7 +30,6 @@ export const useAnalytics = ({
flags_api_host,
isDisabled,
}: useAnalyticsProps) => {
const [location] = useLocation()
const { user } = useUser()
useEffect(() => {
@@ -42,6 +40,8 @@ export const useAnalytics = ({
api_host: host,
flags_api_host: flags_api_host,
person_profiles: 'always',
capture_pageview: 'history_change',
capture_pageleave: true,
})
})
}, [id, host, flags_api_host, isDisabled])
@@ -51,12 +51,5 @@ export const useAnalytics = ({
startAnalyticsSession(user)
}, [user])
// From PostHog tutorial on PageView tracking in a Single Page Application (SPA) context.
useEffect(() => {
getPosthog().then((ph) => {
ph.capture('$pageview')
})
}, [location])
return null
}