Using CookieYes Consent Manager with Matomo
Introduction
CookieYes is a popular consent management platform which supports cookie auto-blocking, multi-language translation, geo-targeting and privacy policy generation.
In this step-by-step guide you will learn how to set up CookieYes and Matomo to work together. CookieYes can request and track visitor consent, with Matomo enabling or disabling analytics tracking based on the consent status provided by CookieYes.
Prerequisites
Before following the instructions in this guide you should already have created a CookieYes and have completed the necessary steps to add the consent manager code to your website.
Step 1) Connect Matomo and CookieYes
To have the Matomo JavaScript tracker always use the visitor consent status provided by CookieYes the following code should be added to the header of each website page beneath the CookieYes code.
<script>
var waitForTrackerCount = 0;
function matomoWaitForTracker() {
if (typeof _paq === 'undefined') {
if (waitForTrackerCount < 40) {
setTimeout(matomoWaitForTracker, 250);
waitForTrackerCount++;
return;
}
} else {
document.addEventListener("cookieyes_consent_update", function (eventData) {
const data = eventData.detail;
consentSet(data);
});
}
}
function consentSet(data) {
if (data.accepted.includes("analytics")) {
_paq.push(['rememberCookieConsentGiven']);
_paq.push(['setConsentGiven']);
} else {
_paq.push(['forgetCookieConsentGiven']);
}
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker());
</script>
Step 2) Configure the Matomo Tracker consent mode
Matomo can apply consent in two different ways:
“Consent to Cookie”
This mode can be used when personal data is not being tracked. If consent to use cookies is not given then Matomo will still track visitors without using cookies and provide a full range of metrics, however the accuracy of some metrics may be reduced. Cookies will only be used if consent for Analytics cookies was given in CookieYes.
“Consent to Track”
If personal data is tracked, such as user identifiers or eCommerce orders, then this mode should be used. If consent for Analytics cookies is not given in CookieYes then Matomo will not perform any tracking at all.
Find out more about personal data processing in Matomo
Depending on which Matomo consent mode you choose, follow step 2a or step 2b.
Step 2a) Consent to Cookie
- Go to Administration (the gear icon) > Measurables > Tracking Code in your Matomo dashboard, make sure the correct website is selected.
-
Copy the tracking code shown in the Matomo dashboard and paste it into a text editor. It should look similar to this:
<!-- Matomo --> <!-- SAMPLE CODE - DO NOT COPY THIS --> <!-- COPY THE TRACKING CODE FROM YOUR MATOMO DASHBOARD --> <script> var _paq = window._paq = window._paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//matomo/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); })(); </script> <script src="//matomo/matomo.js"></script> <!-- End Matomo Code -->
-
Add the line
_paq.push(['requireCookieConsent']);
just before the first line starting with_paq.push
. -
The resulting code should now look similar to this:
<!-- Matomo --> <!-- SAMPLE CODE - DO NOT COPY THIS --> <!-- COPY THE TRACKING CODE FROM YOUR MATOMO DASHBOARD --> <script> var _paq = window._paq = window._paq || []; _paq.push(['requireCookieConsent']); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//matomo/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); })(); </script> <script src="//matomo/matomo.js"></script> <!-- End Matomo Code -->
-
This code should now be added to all pages on your website directly before the tag, if you already have Matomo tracking code on the page then it should be replaced with this updated version.
Step 2b) Consent to Tracking
- Go to Administration > Measurables > Tracking Code in your Matomo dashboard, make sure the correct website is selected.
-
Copy the tracking code shown in the Matomo dashboard and paste it into a text editor. It should look similar to this:
<!-- Matomo --> <!-- SAMPLE CODE - DO NOT COPY THIS --> <!-- COPY THE TRACKING CODE FROM YOUR MATOMO DASHBOARD --> <script> var _paq = window._paq = window._paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//matomo/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); })(); </script> <script src="//matomo/matomo.js"></script> <!-- End Matomo Code -->
-
Add the line
_paq.push(['requireConsent']);
just before the first line starting with_paq.push
. -
The resulting code should now look similar to this:
<!-- Matomo --> <!-- SAMPLE CODE - DO NOT COPY THIS --> <!-- COPY THE TRACKING CODE FROM YOUR MATOMO DASHBOARD --> <script> var _paq = window._paq = window._paq || []; _paq.push(['requireConsent']); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//matomo/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); })(); </script> <script src="//matomo/matomo.js"></script> <!-- End Matomo Code -->
-
This code should now be added to all pages on your website directly before the tag, if you already have Matomo tracking code on the page then it should be replaced with this updated version.
Conclusion
If you’ve successfully followed the steps in this guide then your website will now have both CookieYes and Matomo working together, with CookieYes managing all visitor consent and the Matomo JavaScript tracker only tracking visitors who have given consent to be tracked.