Fix SPA double-counted pageviews
Fix pageviews counted twice on single-page apps: duplicate router calls, the script included twice, tag manager history triggers, and checking with debug mode.
If pageviews are roughly double the visits you'd expect, or each page shows up twice in the Live stream, two things are sending pageviews for each navigation.
Common causes#
- Both automatic SPA tracking and a manual call. You call
privatus.pageview()in a router hook (router.afterEach,useEffecton the location,onRouteChange) while the tracker also followshistory.pushState,popstateand hash changes. Remove your call, or switch the tracker todata-spa="off"and keep yours. - The script is included twice: in the layout and in a page, or by a
plugin and by hand. Search the rendered HTML for
pa.js. There should be exactly one. A framework integration plus a manual snippet is a common pair. - A tag manager re-fires the tag on "History change" triggers. Fire it only on page load: the tracker handles history changes itself.
- Zaraz SPA support or similar tools re-injecting scripts on navigation.
What the tracker already prevents#
It won't send two automatic pageviews in a row for the same path, so a
router that pushes the same URL twice isn't double counted. The path is
the pathname, plus the fragment in hash mode (data-spa="hash") and
route-like fragments (#/about, #!/about) in automatic mode.
privatus.pageview() always sends, by design.
The data-spa modes#
| Value | What the tracker follows |
|---|---|
auto (default) |
history.pushState, back and forward, and hash changes |
history |
history.pushState and back and forward only |
hash |
Hash changes only. Every #fragment counts as a page |
off |
Nothing. Only the first page load is sent, plus your own privatus.pageview() calls |
Check with data-debug="true": each navigation should log exactly one
[privatus] pageview.