From 03e90b617828e0991c324d70a6eeea3acb9f3cae Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Mon, 10 Aug 2026 17:00:00 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(frontend)=20fix=20double-counted?= =?UTF-8?q?=20pageviews=20in=20PostHog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../src/features/analytics/hooks/useAnalytics.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/frontend/src/features/analytics/hooks/useAnalytics.ts b/src/frontend/src/features/analytics/hooks/useAnalytics.ts index feb6d6e1..3d46ac4f 100644 --- a/src/frontend/src/features/analytics/hooks/useAnalytics.ts +++ b/src/frontend/src/features/analytics/hooks/useAnalytics.ts @@ -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 }