68 lines
2.0 KiB
JavaScript
68 lines
2.0 KiB
JavaScript
(function () {
|
||
// --- helpers ---
|
||
function loadScript(src) {
|
||
var s = document.createElement("script");
|
||
s.async = true;
|
||
s.src = src;
|
||
(document.head || document.documentElement).appendChild(s);
|
||
}
|
||
function consentOk() {
|
||
try {
|
||
// Si tu utilises vanilla-cookieconsent v3
|
||
return window.CookieConsent &&
|
||
typeof CookieConsent.acceptedCategory === "function"
|
||
? CookieConsent.acceptedCategory("analytics")
|
||
: true; // pas de CMP → on trace par défaut (change à false si tu veux l’inverse)
|
||
} catch (e) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
// --- Matomo ---
|
||
function initMatomo() {
|
||
if (window.__MATOMO_LOADED__) return;
|
||
window.__MATOMO_LOADED__ = true;
|
||
window._paq = window._paq || [];
|
||
_paq.push(["trackPageView"]);
|
||
_paq.push(["enableLinkTracking"]);
|
||
var u = "https://staging.uslugi.fr/matomo/";
|
||
_paq.push(["setTrackerUrl", u + "matomo.php"]);
|
||
_paq.push(["setSiteId", "1"]);
|
||
loadScript(u + "matomo.js"); // charge le tracker depuis ton staging (autorise ce domaine dans la CSP)
|
||
}
|
||
|
||
// --- Google Analytics (facultatif) ---
|
||
function initGA() {
|
||
if (window.__GA_READY__) return;
|
||
window.__GA_READY__ = true;
|
||
loadScript("https://www.googletagmanager.com/gtag/js?id=G-RXS9TX1JYD");
|
||
window.dataLayer = window.dataLayer || [];
|
||
function gtag() {
|
||
dataLayer.push(arguments);
|
||
}
|
||
window.gtag = gtag;
|
||
gtag("js", new Date());
|
||
gtag("config", "G-RXS9TX1JYD", { anonymize_ip: true });
|
||
}
|
||
|
||
// --- start when allowed ---
|
||
function start() {
|
||
if (!consentOk()) return;
|
||
//initMatomo();
|
||
initGA(); // décommente si tu veux activer GA aussi
|
||
}
|
||
|
||
if (document.readyState === "loading") {
|
||
document.addEventListener("DOMContentLoaded", start);
|
||
} else {
|
||
start();
|
||
}
|
||
|
||
// Si la bannière change le consentement à chaud
|
||
if (window.CookieConsent && typeof CookieConsent.onChange === "function") {
|
||
CookieConsent.onChange(function ({ changedCategories }) {
|
||
if (changedCategories.includes("analytics") && consentOk()) start();
|
||
});
|
||
}
|
||
})();
|