CookieYes is a popular consent management platform that 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 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 CookieYes, add the following code to the header of each website page and insert below 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']);  
       _paq.push(['deleteCookies']);         
   }
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker());
</script>

Matomo will only track visitors if they have given consent. 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"></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 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(['setConsentGiven']);
   } else {
       _paq.push(['forgetConsentGiven']);                      
   }
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker());
</script>

If consent is not given in CookieYes, 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 CookieYes and Matomo working together. CookieYes will manage all visitor consent and the Matomo JavaScript tracker will only track visitors who have given consent.

Previous FAQ: Using Cookiebot Consent Manager CMP with Matomo