🐛 Track GA4 page_view on client-side route changes - #6776
Conversation
The gtag('config', ...) call in GoogleAnalytics only ran once, on the
first full page load. Every subsequent in-app navigation via next/link
is a client-side transition that never re-executes that script, so
GA4 only ever recorded the entry page of each session — multi-page
visits were massively under-reported.
Adds a RouteChangeTracker that watches pathname + query string and
fires an explicit page_view event on every route change, matching
Google's documented SPA tracking pattern. Guards against double-
counting the landing page and against window.gtag being unavailable.
Signed-off-by: kubestellar-hive[bot] <223556219+Copilot@users.noreply.github.com>
|
Important Held for human sign-off on the direction, not on the code. This PR's only tracked rationale is #6775, which the hive filed itself — issue #6775 was filed by kubestellar-hive[bot] and no human has acknowledged it. An agent-filed issue does not, on its own, establish that anyone agreed to the direction (hivecommons/hive#5117). The change may well be right; nothing here is a review of it. To release the hold, acknowledge the direction on that issue — comment on it, assign yourself, or add the |
✅ Deploy Preview for kubestellar-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Hi @kubestellar-hive[bot]. Thanks for your PR. I'm waiting for a kubestellar member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
Thank you for your contribution! Your PR has been merged. Check out what's new:
Stay connected: Slack #kubestellar-dev | Multi-Cluster Survey |
Fixes #6775
Problem
src/components/GoogleAnalytics.tsxfiresgtag('config', ...)(withsend_page_view: true) exactly once, inside an inline<Script strategy="afterInteractive">in the root layout. The docs site uses the Next.js App Router withnext/linkfor internal navigation (seeDocsNavbar), so client-side route transitions never re-run that script. Only the first page a visitor lands on was ever reported to GA4 — every subsequent page in a session was invisible.Fix
Adds
RouteChangeTracker, a small client component rendered insideGoogleAnalytics(wrapped inSuspense, as required foruseSearchParamsin the App Router). It watchesusePathname()+useSearchParams()and fires an explicitgtag('event', 'page_view', { page_path, page_location, page_title })on every route change, per Google's documented SPA tracking pattern. The first render is skipped so the landing page (already covered by the inline config script) isn't double-counted.Scope / guardrails
isProductionDeploy()unchanged).Testing
src/__tests__/RouteChangeTracker.test.tsx(4 tests): no page_view on mount, page_view fires on pathname change, query string is included, no throw whenwindow.gtagis unavailable.GoogleAnalytics.deploy-gate.test.tsx,gtagEvent.test.ts, andEditPageLink.gtag.test.tsxall still pass (16/16 total across the four files).npx eslintandnpx tsc --noEmitare clean on the changed files.— hive: agent=telemetry backend=copilot model=claude-sonnet-4-6