From 6ce0e9e61b7204a1f0cff37b9da1a922d811da36 Mon Sep 17 00:00:00 2001 From: Miroslav Svitok Date: Fri, 22 Sep 2023 11:16:56 +0000 Subject: [PATCH] Add JS tracker parameters 'canonicalUrl' and 'referer' remp/remp#1297 --- resources/assets/js/remplib.js | 36 +++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/resources/assets/js/remplib.js b/resources/assets/js/remplib.js index a018939..d0c736c 100644 --- a/resources/assets/js/remplib.js +++ b/resources/assets/js/remplib.js @@ -52,6 +52,10 @@ class Tracker { maxPageProgressAchieved = 0; + canonicalUrl = null; + + referer = null; + init(config) { Tracker.configure(config) } @@ -162,6 +166,24 @@ class Tracker { } } + if (config.tracker.canonicalUrl) { + try { + new URL(config.tracker.canonicalUrl); + this.canonicalUrl = config.tracker.canonicalUrl; + } catch (error) { + console.warn("Tracker - invalid value of 'canonicalUrl' attribute. Make sure the value is valid URL, including schema."); + } + } + + if (config.tracker.referer) { + try { + new URL(config.tracker.referer); + this.referer = config.tracker.referer; + } catch (error) { + console.warn("Tracker - invalid value of 'referer' attribute. Make sure the value is valid URL, including schema."); + } + } + if (Tracker.initialized) { this._reset(); } @@ -681,9 +703,13 @@ class Tracker { } let canonicalUrl = null; - let canonicalSelector = document.querySelector("link[rel='canonical']"); - if (canonicalSelector) { - canonicalUrl = canonicalSelector.href; + if (this.canonicalUrl) { + canonicalUrl = this.canonicalUrl; + } else { + let canonicalSelector = document.querySelector("link[rel='canonical']"); + if (canonicalSelector) { + canonicalUrl = canonicalSelector.href; + } } params["user"] = { @@ -691,9 +717,9 @@ class Tracker { "browser_id": remplib.getBrowserId(), "subscriber": remplib.isUserSubscriber(), "subscription_ids": remplib.getSubscriptionIds(), - "url": window.location.href, + "url": this.canonicalUrl || window.location.href, "canonical_url": canonicalUrl, - "referer": document.referrer, + "referer": this.referer || document.referrer, "user_agent": window.navigator.userAgent, "adblock": remplib.usingAdblock, "window_height": window.outerHeight || document.documentElement.clientHeight,