demarches-normaliennes/app/javascript/shared/track/matomo.js
Pierre de La Morinerie 07230870d3 js: don't track external links
This fixes an issue where, by default, links to private attachments are
reported to Matomo.

This is benign: attachments URLs can be filtered out server-side, and
expire after one hour anyway. But we don't want to ship an insecure
configuration by default.
2021-12-23 09:50:21 +01:00

35 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { key, enabled } = gon.matomo || {};
if (enabled) {
window._paq = window._paq || [];
const url = '//stats.data.gouv.fr/';
const trackerUrl = `${url}piwik.php`;
const jsUrl = `${url}piwik.js`;
//
// Configure Matomo analytics
//
window._paq.push(['setCookieDomain', '*.www.demarches-simplifiees.fr']);
window._paq.push(['setDomains', ['*.www.demarches-simplifiees.fr']]);
// Dont store any cookies or send any tracking request when the "Do Not Track" browser setting is enabled.
window._paq.push(['setDoNotTrack', true]);
// When enabling external link tracking, consider that it will also report links to attachments.
// Youll want to exclude links to attachments from being tracked for instance using Matomo's
// `setCustomRequestProcessing` callback.
// window._paq.push(['enableLinkTracking']);
window._paq.push(['trackPageView']);
// Load script from Matomo
window._paq.push(['setTrackerUrl', trackerUrl]);
window._paq.push(['setSiteId', key]);
const script = document.createElement('script');
const firstScript = document.getElementsByTagName('script')[0];
script.type = 'text/javascript';
script.id = 'matomo-js';
script.async = true;
script.src = jsUrl;
firstScript.parentNode.insertBefore(script, firstScript);
}