diff --git a/src/assets/js/cookieConsent.js b/src/assets/js/cookieConsent.js index 9e7bbc6..44fc8a9 100644 --- a/src/assets/js/cookieConsent.js +++ b/src/assets/js/cookieConsent.js @@ -14,11 +14,33 @@ const cookieStorage = { }; const storageType = cookieStorage; -const consentPropertyName = "Audacity_consent"; +const consentPropertyName = "audacity_consent"; const showShowPopup = () => !storageType.getItem(consentPropertyName); -const saveAcceptToStorage = () => storageType.setItem(consentPropertyName, true); -const saveRejectToStorage = () => storageType.setItem(consentPropertyName, false); +const saveAcceptToStorage = () => + storageType.setItem(consentPropertyName, true); +const saveRejectToStorage = () => + storageType.setItem(consentPropertyName, false); +const applyMatomoTrackingCode = () => { + console.log( + "audacity-consent cookie created, running Matomo tracking code..." + ); + var _paq = (window._paq = window._paq || []); + /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ + _paq.push(["trackPageView"]); + _paq.push(["enableLinkTracking"]); + (function () { + var u = "https://matomo.audacityteam.org/"; + _paq.push(["setTrackerUrl", u + "matomo.php"]); + _paq.push(["setSiteId", "19"]); + var d = document, + g = d.createElement("script"), + s = d.getElementsByTagName("script")[0]; + g.async = true; + g.src = u + "matomo.js"; + s.parentNode.insertBefore(g, s); + })(); +}; window.onload = () => { const consentPopup = document.getElementById("consent-popup"); @@ -28,15 +50,16 @@ window.onload = () => { const acceptCookie = (event) => { saveAcceptToStorage(storageType); consentPopup.classList.add("hide"); + applyMatomoTrackingCode(); }; const rejectCookie = (event) => { saveRejectToStorage(storageType); - consentPopup.classList.add("hide") - } + consentPopup.classList.add("hide"); + }; acceptBtn.addEventListener("click", acceptCookie); - rejectBtn.addEventListener("click", rejectCookie) + rejectBtn.addEventListener("click", rejectCookie); if (showShowPopup(storageType)) { setTimeout(() => { diff --git a/src/assets/js/matomoTracking.js b/src/assets/js/matomoTracking.js new file mode 100644 index 0000000..a314828 --- /dev/null +++ b/src/assets/js/matomoTracking.js @@ -0,0 +1,26 @@ +const getCookie = (name) => { + const value = `; ${document.cookie}`; + const parts = value.split(`; ${name}=`); + if (parts.length === 2) return parts.pop().split(";").shift(); + } + + if (getCookie("audacity_consent") === "true") { + console.log( + "audacity_consent cookie found, loading Matomo tracking code..." + ); + var _paq = (window._paq = window._paq || []); + /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ + _paq.push(["trackPageView"]); + _paq.push(["enableLinkTracking"]); + (function () { + var u = "https://matomo.audacityteam.org/"; + _paq.push(["setTrackerUrl", u + "matomo.php"]); + _paq.push(["setSiteId", "19"]); + var d = document, + g = d.createElement("script"), + s = d.getElementsByTagName("script")[0]; + g.async = true; + g.src = u + "matomo.js"; + s.parentNode.insertBefore(g, s); + })(); + } \ No newline at end of file diff --git a/src/components/banner/CookieConsent.jsx b/src/components/banner/CookieConsent.jsx index 7046c6b..b789fa3 100644 --- a/src/components/banner/CookieConsent.jsx +++ b/src/components/banner/CookieConsent.jsx @@ -4,7 +4,7 @@ function CookieConsent() { return (