Introduction

Cookiebot by Usercentrics is a easy to use consent management platform which 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 have completed the necessary steps to add the consent manager code to your website.

Step 1) Connect Matomo and Cookiebot

To have the Matomo JavaScript tracker always use the visitor consent status provided by Cookiebot the following code should be added to the header of each website page beneath 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(['setCookieConsentGiven']);
    _paq.push(['setConsentGiven']);
  } else {
    _paq.push(['forgetCookieConsentGiven']);
    _paq.push(['forgetConsentGiven']);
  }
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker());
</script>

Matomo will only track visitors if they have given the Cookiebot ‘Statistics’ consent.

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 Statistics cookies was given in Cookiebot.

“Consent to Track”
If personal data is tracked, such as user identifiers or eCommerce orders, then this mode should be used. If consent for Statistics cookies is not given in Cookiebot 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.

  • 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(['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.

  • 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 Cookiebot and Matomo working together, with Cookiebot managing all visitor consent and the Matomo JavaScript tracker only tracking visitors who have given consent to be tracked.

Previous FAQ: Using Osano Consent Manager with Matomo