Using Cookiebot Consent Manager (CMP) with Matomo
Cookiebot by Usercentrics is an easy to use consent management platform that supports bulk domain consent, multiple languages with automatic detection, geo-targeting and monthly cookie audits.
In this step-by-step guide, you will learn how to set up Cookiebot and Matomo to work together. Cookiebot can request and track visitor consent, with Matomo enabling or disabling analytics tracking based on the consent status provided by Cookiebot.
Prerequisites
Before following the instructions in this guide, you should already have created a Cookiebot account and completed the necessary steps to add the consent manager code to your website.
Configure Matomo to apply consent for cookies
Step 1: Insert code for Cookiebot consent
For the Matomo JavaScript tracker to always use the visitor consent status provided by Cookiebot, add the following code to the header of each website page and insert below the Cookiebot code:
<script>
var waitForTrackerCount = 0;
function matomoWaitForTracker() {
if (typeof _paq === 'undefined' || typeof Cookiebot === 'undefined') {
if (waitForTrackerCount < 40) {
setTimeout(matomoWaitForTracker, 250);
waitForTrackerCount++;
return;
}
} else {
window.addEventListener('CookiebotOnAccept', function (e) {
consentSet();
});
window.addEventListener('CookiebotOnDecline', function (e) {
consentSet();
})
}
}
function consentSet() {
if (Cookiebot.consent.statistics) {
_paq.push(['rememberCookieConsentGiven']);
_paq.push(['setConsentGiven']);
} else {
_paq.push(['forgetCookieConsentGiven']);
_paq.push(['deleteCookies']);
}
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker);
</script>
Matomo will only track visitors if they have given consent for Cookiebot Statistics. If a visitor revokes consent, the cookies are deleted.
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.
Step 2: Insert Matomo tracking code
- Go to Administration > Measurables > Tracking Code in your Matomo dashboard and make sure the correct website is selected.
- Copy the tracking code shown in the Matomo dashboard and paste it into a text editor.
-
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" data-cookieconsent="statistics" type="text/plain"></script> <!-- End Matomo Code -->
-
This code must be added to all pages on your website before the
</head>
tag. If you already have the Matomo tracking code on the page, replace it with this updated version.
Find out more about personal data processing in Matomo.
Configure Matomo to apply consent for tracking without cookies
Step 1: Insert code for Cookiebot consent
To setup Matomo to track visitor metrics without using cookies, add the following code to the header of each website page and insert below the Cookiebot code:
<script>
var waitForTrackerCount = 0;
function matomoWaitForTracker() {
if (typeof _paq === 'undefined' || typeof Cookiebot === 'undefined') {
if (waitForTrackerCount < 40) {
setTimeout(matomoWaitForTracker, 250);
waitForTrackerCount++;
return;
}
} else {
window.addEventListener('CookiebotOnAccept', function (e) {
consentSet();
});
window.addEventListener('CookiebotOnDecline', function (e) {
consentSet();
})
}
}
function consentSet() {
if (Cookiebot.consent.statistics) {
_paq.push(['setConsentGiven']);
} else {
_paq.push(['forgetConsentGiven']);
}
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker);
</script>
If consent for Statistics cookies is not given in Cookiebot, then Matomo will not perform any tracking at all.
Step 2: Insert Matomo tracking code
- Go to Administration > Measurables > Tracking Code in your Matomo dashboard and make sure the correct website is selected.
- Copy the tracking code shown in the Matomo dashboard and paste it into a text editor.
-
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 must be added to all pages on your website before the
</head>
tag. If you already have the Matomo tracking code on the page, replace it with this updated version.
Conclusion
If you successfully followed the steps in this guide, then your website will have both Cookiebot and Matomo working together. Cookiebot will manage all visitor consent and the Matomo JavaScript tracker will only track visitors who have given consent.