diff --git a/src/tracker/index.js b/src/tracker/index.js index b2899e8b..c4c420b6 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); @@ -211,8 +219,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;