fix: strip self-referrals from tracking script

This commit is contained in:
Usman Baig
2026-03-13 01:06:14 +01:00
parent aae1714b02
commit 765f8ec63e
2 changed files with 13 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
### Fixed
- **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.
## [0.15.0-alpha] - 2026-03-13

View File

@@ -270,7 +270,18 @@
lcpObserved = false;
clsObserved = false;
currentEventId = null;
const referrer = document.referrer || '';
// * Strip self-referrals: don't send referrer if it matches the current site domain
var rawReferrer = document.referrer || '';
var referrer = '';
if (rawReferrer) {
try {
var refHost = new URL(rawReferrer).hostname.replace(/^www\./, '');
var siteHost = domain.replace(/^www\./, '');
if (refHost !== siteHost) referrer = rawReferrer;
} catch (e) {
referrer = rawReferrer;
}
}
const screen = {
width: window.innerWidth || screen.width,
height: window.innerHeight || screen.height,