Skip to content

Commit

Permalink
Add JS tracker parameters 'canonicalUrl' and 'referer'
Browse files Browse the repository at this point in the history
remp/remp#1297
  • Loading branch information
miroc committed Sep 22, 2023
1 parent 4e0b23b commit 6ce0e9e
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions resources/assets/js/remplib.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class Tracker {

maxPageProgressAchieved = 0;

canonicalUrl = null;

referer = null;

init(config) {
Tracker.configure(config)
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -681,19 +703,23 @@ 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"] = {
"id": remplib.getUserId(),
"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,
Expand Down

0 comments on commit 6ce0e9e

Please sign in to comment.