Merge pull request #3319 from betagouv/fix-matomo-analytics

analytics: send Matomo event when navigating using Turbolinks
This commit is contained in:
Pierre de La Morinerie 2019-01-23 16:37:03 +01:00 committed by GitHub
commit 8d875708b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,13 @@
:javascript :javascript
var _paq = _paq || []; var _paq = _paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */ // Configure Matomo analytics
_paq.push(["setCookieDomain", "*.www.demarches-simplifiees.fr"]); _paq.push(["setCookieDomain", "*.www.demarches-simplifiees.fr"]);
_paq.push(["setDomains", ["*.www.demarches-simplifiees.fr"]]); _paq.push(["setDomains", ["*.www.demarches-simplifiees.fr"]]);
_paq.push(["setDoNotTrack", true]); _paq.push(["setDoNotTrack", true]);
_paq.push(['trackPageView']); _paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']); _paq.push(['enableLinkTracking']);
// Load script from Matomo
(function() { (function() {
var u="//stats.data.gouv.fr/"; var u="//stats.data.gouv.fr/";
_paq.push(['setTrackerUrl', u+'piwik.php']); _paq.push(['setTrackerUrl', u+'piwik.php']);
@ -13,3 +15,20 @@
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; 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); 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;
});
})();