Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix(hash): improve URL normalization and handling in tracking functions

Chairil Fauzi Firmansyahc5298d5d45fa
diff --git a/src/tracker/index.js b/src/tracker/index.js
index 76d29a1dd..b05d90859 100644
--- a/src/tracker/index.js
+++ b/src/tracker/index.js
@@ -38,6 +38,18 @@
/* Helper functions */
+ const normalize = raw => {
+ if (!raw) return raw;
+ try {
+ const u = new URL(raw, location.href);
+ if (excludeSearch) u.search = '';
+ if (excludeHash) u.hash = '';
+ return u.toString();
+ } catch (e) {
+ return raw;
+ }
+ };
+
const getPayload = () => ({
website,
screen,
@@ -61,11 +73,7 @@
if (!url) return;
currentRef = currentUrl;
- currentUrl = new URL(url, location.href);
-
- if (excludeSearch) currentUrl.search = '';
- if (excludeHash) currentUrl.hash = '';
- currentUrl = currentUrl.toString();
+ currentUrl = normalize(new URL(url, location.href).toString());
if (currentUrl !== currentRef) {
setTimeout(track, delayDuration);
@@ -210,8 +218,9 @@
};
}
- let currentUrl = href;
- let currentRef = referrer.startsWith(origin) ? '' : referrer;
+ let currentUrl = normalize(href);
+ let currentRef = normalize(referrer.startsWith(origin) ? '' : referrer);
+
let initialized = false;
let disabled = false;
let cache;