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.

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

  1. Go to Administration > Measurables > Tracking Code in your Matomo dashboard and make sure the correct website is selected.
  2. Copy the tracking code shown in the Matomo dashboard and paste it into a text editor.
  3. 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 -->
    
  4. 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.

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

  1. Go to Administration > Measurables > Tracking Code in your Matomo dashboard and make sure the correct website is selected.
  2. Copy the tracking code shown in the Matomo dashboard and paste it into a text editor.
  3. 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 -->
    
  4. 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.

Previous FAQ: Using Osano Consent Manager CMP with Matomo