Skip to content

Commit d775fb3

Browse files
committed
Made analytics tracking respect cookie preferences
1 parent 353f183 commit d775fb3

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed
Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
1-
<!-- Matomo Tag Manager -->
1+
<!-- Matomo -->
22
<script>
3-
var _mtm = window._mtm = window._mtm || [];
4-
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
5-
(function() {
6-
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
7-
g.async=true; g.src='https://cdn.matomo.cloud/opentechfund.matomo.cloud/container_9dG94t2E.js'; s.parentNode.insertBefore(g,s);
8-
})();
3+
4+
// Get user's analytic tracking preference
5+
function getUserTrackingConsent () {
6+
return localStorage.getItem("cookieconsent") === "accept"
7+
}
8+
9+
// Initialize Matomo
10+
function initMatomo() {
11+
// Boilerplate Matomo tracking code
12+
var _paq = window._paq = window._paq || [];
13+
_paq.push(['trackPageView']);
14+
_paq.push(['enableLinkTracking']);
15+
(function() {
16+
var u="https://opentechfund.matomo.cloud/";
17+
_paq.push(['setTrackerUrl', u+'matomo.php']);
18+
_paq.push(['setSiteId', '1']);
19+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
20+
g.async=true; g.src='https://cdn.matomo.cloud/opentechfund.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s);
21+
})();
22+
}
23+
24+
// Set the consent prefs for Matomo based on the `consent` var
25+
function setMatomoConsent() {
26+
if (consent) {
27+
_paq.push(['setConsentGiven']);
28+
} else {
29+
_paq.push(['forgetConsentGiven']);
30+
}
31+
}
32+
33+
// Check if the user's consent setting has updated
34+
function checkConsentUpdates() {
35+
var new_consent = getUserTrackingConsent();
36+
37+
if (new_consent != consent) {
38+
consent = new_consent;
39+
setMatomoConsent();
40+
}
41+
}
42+
43+
var consent = getUserTrackingConsent();
44+
45+
var _paq = window._paq = window._paq || [];
46+
_paq.push(['requireConsent']);
47+
initMatomo();
48+
setMatomoConsent();
49+
50+
// Check for consent updates every 5 seconds
51+
setInterval(checkConsentUpdates, 5000);
952
</script>
10-
<!-- End Matomo Tag Manager -->
53+
<!-- End Matomo -->

0 commit comments

Comments
 (0)