analytics: send Matomo event when navigating using Turbolinks

This commit is contained in:
Pierre de La Morinerie 2019-01-23 15:23:10 +00:00
parent fa4af38a94
commit b177df477e

View file

@ -1,11 +1,13 @@
:javascript
var _paq = _paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
// Configure Matomo analytics
_paq.push(["setCookieDomain", "*.www.demarches-simplifiees.fr"]);
_paq.push(["setDomains", ["*.www.demarches-simplifiees.fr"]]);
_paq.push(["setDoNotTrack", true]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
// Load script from Matomo
(function() {
var u="//stats.data.gouv.fr/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
@ -13,3 +15,20 @@
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);
})();
// Send Matomo a new event when navigating to a new page using Turbolinks
// (see https://developer.matomo.org/guides/spa-tracking)
(function() {
var previousPageUrl = null;
addEventListener('turbolinks:load', function(event) {
if (previousPageUrl) {
var loadTimeMs = event.data.timing.visitEnd - event.data.timing.visitStart;
_paq.push(['setReferrerUrl', previousPageUrl]);
_paq.push(['setCustomUrl', '/' + window.location.href]);
_paq.push(['setDocumentTitle', document.title]);
_paq.push(['setGenerationTimeMs', loadTimeMs]);
_paq.push(['trackPageView']);
}
previousPageUrl = window.location.href;
});
})();