fix: skip pageview tracking for prerendered pages

This commit is contained in:
Usman Baig
2026-03-13 01:17:12 +01:00
parent a57ed871f1
commit 6edd5ac0b6
2 changed files with 9 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
- **Self-referrals no longer pollute your traffic sources.** Internal navigation within your own site (e.g. clicking from your homepage to your about page) no longer shows your own domain as a referrer. Only external traffic sources appear in your Referrers panel now. - **Self-referrals no longer pollute your traffic sources.** Internal navigation within your own site (e.g. clicking from your homepage to your about page) no longer shows your own domain as a referrer. Only external traffic sources appear in your Referrers panel now.
- **Screen size fallback now works correctly.** A variable naming issue prevented the fallback screen dimensions from being read when the primary value wasn't available. Screen size data is now reliably captured on all browsers. - **Screen size fallback now works correctly.** A variable naming issue prevented the fallback screen dimensions from being read when the primary value wasn't available. Screen size data is now reliably captured on all browsers.
- **Browser back/forward no longer double-counts pageviews.** Pressing the back or forward button could occasionally register two pageviews instead of one. The tracking script now correctly deduplicates these navigations. - **Browser back/forward no longer double-counts pageviews.** Pressing the back or forward button could occasionally register two pageviews instead of one. The tracking script now correctly deduplicates these navigations.
- **Preloaded pages no longer count as visits.** Modern browsers sometimes preload pages in the background before you actually visit them. These ghost visits no longer inflate your pageview counts — only pages the visitor actually sees are tracked.
## [0.15.0-alpha] - 2026-03-13 ## [0.15.0-alpha] - 2026-03-13

View File

@@ -329,8 +329,14 @@
}); });
} }
// * Track initial pageview // * Track initial pageview (skip if page is being speculatively prerendered)
trackPageview(); if (document.prerendering) {
document.addEventListener('prerenderingchange', function() {
trackPageview();
}, { once: true });
} else {
trackPageview();
}
// * Track SPA navigation: MutationObserver (DOM updates) and history.pushState/replaceState // * Track SPA navigation: MutationObserver (DOM updates) and history.pushState/replaceState
// * (some SPAs change the URL without a DOM mutation we observe) // * (some SPAs change the URL without a DOM mutation we observe)