When using the JS tracking setReferrerUrl seems to be ignored.
ENV
Matomo 3.14.0
CODE
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
if (document.referrer && document.referrer.search(/bad/ > -1) {
console.log('Unsetting referrer, current:'+document.referrer);
_paq.push(['setReferrerUrl','']);
}
(function() {
var u='//' + piwik_url + '/';
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', siteid]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
}
EXECUTION
We see that code which sets referrer is run
console.log
Unsetting referrer, current:...
nevertheless the current referrer as seen by
document.referrer
https://bad.domain/
is also set in piwik.php?....&urlref
parameter
urlref: https://bad.domain/
A guess from some inspection that I did in the tracking js might be that setReferrerUrl
might have to be executed before trackPageView
In this code that is run (breakpoint in the debugger)
function logPageView(customTitle, customData, callback) {
configIdPageView = generateUniqueId();
var request = getRequest('action_name=' + encodeWrapper(titleFixup(customTitle || configTitle)), customData, 'log');
sendRequest(request, configTrackerPause, callback);
}
and inside getRequest the configReferrerUrl is the detected from document.referrer and not the set one.
Also this code
*/
this.setReferrerUrl = function (url) {
configReferrerUrl = url;
};
runs (breakpoint) after the above.
Maybe this has to do with the method applied first as noted in applyFirst=... .
setReferrerUrl is not in this list.
@basos9 your inspection looks correct. You'll need to call this method before the page view. I suggest you reorder the tracking code to
_paq.push(['enableLinkTracking']);
if (document.referrer && document.referrer.search(/bad/ > -1) {
console.log('Unsetting referrer, current:'+document.referrer);
_paq.push(['setReferrerUrl','']);
}
_paq.push(['trackPageView']);
We wouldn't want to add this to applyFirst
unfortunately as someone might initially want to track the page view with the original referrer URL first.
I'll mention this in the docs though.