Avoid saving same-domain referrers and resolve path-only referrers correctly

This commit is contained in:
Mike Cao committed 2026-08-03 23:03:52 -07:00
1 parent c5d34d10ad
commit 8efd5d7279
4 files changed
+131 -6

No files matched your search

+4 -2
View File
@@ -19,8 +19,10 @@ export type TrackedProperties = {
/**
* Page referrer
*
* @description extracted from `document.referrer`
* @example 'https://analytics.umami.is/docs/getting-started'
* @description extracted from `document.referrer` on page load, or the previous
* in-app URL on client-side navigation. Same-origin referrers are sent as
* root-relative paths so the site's own domain is never saved as a referrer.
* @example 'https://analytics.umami.is/docs/getting-started', '/docs/getting-started'
*/
referrer?: string;
/**
+7 -2
View File
@@ -274,6 +274,11 @@ type MetricEntry = PerformanceEntry & {
}
};
// Strip the origin from same-origin referrers so the referrer domain
// is never saved when it matches the current hostname
const stripOrigin = (url: string): string =>
url === origin || url?.startsWith(origin + '/') ? url.slice(origin.length) : url;
const getPayload = () => ({
website,
screen,
@@ -281,7 +286,7 @@ type MetricEntry = PerformanceEntry & {
title: document.title,
hostname,
url: currentUrl,
referrer: currentRef,
referrer: stripOrigin(currentRef),
tag,
id: identity ? identity : undefined,
});
@@ -631,7 +636,7 @@ type MetricEntry = PerformanceEntry & {
}
let currentUrl = normalize(href);
let currentRef = normalize(referrer.startsWith(origin) ? '' : referrer);
let currentRef = normalize(referrer);
let initialized = false;
let disabled = false;