From a57ed871f10995cba0de5bf768f5631cc00edc22 Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Fri, 13 Mar 2026 01:09:45 +0100 Subject: [PATCH] fix: screen size shadowing, popstate double pageview, custom event self-referrals --- CHANGELOG.md | 2 ++ public/script.js | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2ae437..f29aca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - **More accurate pageview counts.** Refreshing a page no longer inflates your pageview numbers. The tracking script now detects when the same page is loaded again within a few seconds and skips the duplicate, so metrics like total pageviews, pages per session, and visit duration reflect real navigation instead of reload habits. - **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. ## [0.15.0-alpha] - 2026-03-13 diff --git a/public/script.js b/public/script.js index f0b01c4..4db5dec 100644 --- a/public/script.js +++ b/public/script.js @@ -282,16 +282,16 @@ referrer = rawReferrer; } } - const screen = { - width: window.innerWidth || screen.width, - height: window.innerHeight || screen.height, + const screenSize = { + width: window.innerWidth || window.screen.width, + height: window.innerHeight || window.screen.height, }; const payload = { domain: domain, path: path, referrer: referrer, - screen: screen, + screen: screenSize, session_id: getSessionId(), }; @@ -354,6 +354,9 @@ // * Track popstate (browser back/forward) window.addEventListener('popstate', function() { + var url = location.href; + if (url === lastUrl) return; + lastUrl = url; trackPageview(); setTimeout(check404, 100); if (trackScroll) scrollFired = {}; @@ -373,7 +376,15 @@ return; } var path = window.location.pathname + window.location.search; - var referrer = document.referrer || ''; + var rawRef = document.referrer || ''; + var referrer = ''; + if (rawRef) { + try { + var rh = new URL(rawRef).hostname.replace(/^www\./, ''); + var sh = domain.replace(/^www\./, ''); + if (rh !== sh) referrer = rawRef; + } catch (e) { referrer = rawRef; } + } var screenSize = { width: window.innerWidth || 0, height: window.innerHeight || 0 }; var payload = { domain: domain,