allow passing vars to fathom('trackPageview') which override parsed data. see #178

This commit is contained in:
Danny van Kooten 2018-11-02 11:35:50 +01:00
parent fa46af7864
commit dc1573cc56
1 changed files with 14 additions and 12 deletions

View File

@ -105,7 +105,9 @@
return el ? el.src.replace('tracker.js', 'collect') : ''; return el ? el.src.replace('tracker.js', 'collect') : '';
} }
function trackPageview() { function trackPageview(vars) {
vars = vars || {};
// Respect "Do Not Track" requests // Respect "Do Not Track" requests
if('doNotTrack' in navigator && navigator.doNotTrack === "1") { if('doNotTrack' in navigator && navigator.doNotTrack === "1") {
return; return;
@ -118,13 +120,14 @@
// if <body> did not load yet, try again at dom ready event // if <body> did not load yet, try again at dom ready event
if( document.body === null ) { if( document.body === null ) {
document.addEventListener("DOMContentLoaded", trackPageview) document.addEventListener("DOMContentLoaded", () => {
trackPageview(vars);
})
return; return;
} }
// parse request, use canonical if there is one
let req = window.location; let req = window.location;
// parse canonical, if page has one
let canonical = document.querySelector('link[rel="canonical"][href]'); let canonical = document.querySelector('link[rel="canonical"][href]');
if(canonical) { if(canonical) {
let a = document.createElement('a'); let a = document.createElement('a');
@ -134,18 +137,17 @@
req = a; req = a;
} }
// get path and pathname from location or canonical let path = vars.path || ( req.pathname + req.search );
let path = req.pathname + req.search;
let hostname = req.protocol + "//" + req.hostname;
// if parsing path failed, default to main page
if(!path) { if(!path) {
path = '/'; path = '/';
} }
// determine hostname
let hostname = vars.hostname || ( req.protocol + "//" + req.hostname );
// only set referrer if not internal // only set referrer if not internal
let referrer = ''; let referrer = vars.referrer || '';
if(document.referrer.indexOf(location.hostname) < 0) { if(document.referrer.indexOf(hostname) < 0) {
referrer = document.referrer; referrer = document.referrer;
} }