Hi - I just installed the Piwik Pro Analytics script on my page and am now trying to figure out how to set it up correctly.
As my page is a multi-page site (not a single-page app) I use pre-fetching and pre-rendering (in supported browsers) to speed up the page. However, when I visit the Tracker debugger it seems like a lot of page-visits are filtered out.
In the debugger log they are marked like:
Excluded event
Page URL: https://page-url.com
Exclusion reason: Prefetch header found
How would I set the tracking script correctly to included actual page-visits that was prefetched? The challenge is, that some events should be excluded as the prefetch will not always result in an actual page visit (the prefetch happens on hover of the link).
Is there any way to turn off the filtering of page views when a pre-fetch header is present? I would prefer to filter things clientside (read on).
My approach is to change the clientside inline tracking script for analytics a bit (thus only registering a pageview once the user interacts with the page).
This would hopefully make it work for both pre-fetching and pre-rendering (I use the speculation rules supported in Chromium browsers).
<script type="text/javascript">
var _paq = _paq || [];
let isTracked = false;
function trackPageView() {
if (isTracked) return;
_paq.push(['trackPageView']);
isTracked = true;
['mousemove', 'scroll', 'click', 'keydown', 'touchstart'].forEach(
(event) => document.removeEventListener(event, trackPageView)
);
}
['mousemove', 'scroll', 'click', 'keydown', 'touchstart'].forEach(
(event) =>
document.addEventListener(event, trackPageView, { once: true })
);
(function () {
var u = 'https://your-account-name.piwik.pro/';
_paq.push(['setTrackerUrl', u + 'ppms.php']);
_paq.push(['setSiteId', 'XXX-XXX-XXX-XXX-XXX']);
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 + 'ppms.js';
s.parentNode.insertBefore(g, s);
})();
</script>
Hopefully, I will get all page views where the user somehow interacted with the page this way.
Probably not perfect: But, IMO a lot better than what I have now. More than half of page visits include a prefetch header when I test it out (and is thus counted as an excluded event).