From 6edd5ac0b64c567ba3c3b80113ce70eb698a096e Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Fri, 13 Mar 2026 01:17:12 +0100 Subject: [PATCH] fix: skip pageview tracking for prerendered pages --- CHANGELOG.md | 1 + public/script.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f29aca0..290dc5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. - **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. +- **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 diff --git a/public/script.js b/public/script.js index 4db5dec..7517b22 100644 --- a/public/script.js +++ b/public/script.js @@ -329,8 +329,14 @@ }); } - // * Track initial pageview - trackPageview(); + // * Track initial pageview (skip if page is being speculatively prerendered) + if (document.prerendering) { + document.addEventListener('prerenderingchange', function() { + trackPageview(); + }, { once: true }); + } else { + trackPageview(); + } // * Track SPA navigation: MutationObserver (DOM updates) and history.pushState/replaceState // * (some SPAs change the URL without a DOM mutation we observe)