Osano is a popular consent manager platform which supports third party blocking, multi-language and automatically handles consent requirements for different countries based on visitor geo-location.

In this step-by-step guide, you will learn how to set up Osano and Matomo to work together. Osano can request and track visitor consent, with Matomo enabling or disabling analytics tracking based on the consent status provided by Osano.

Prerequisites

Before following the instructions in this guide, you should already have created an Osano account and have 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 Osano, add the following code to the header of each website page and insert below the Osano code:

<script>
var waitForTrackerCount = 0;
function matomoWaitForTracker() {
  if (typeof _paq === 'undefined') {
    if (waitForTrackerCount < 40) {
      setTimeout(matomoWaitForTracker, 250);
      waitForTrackerCount++;
      return;
    }
  } else {
    Osano.cm.addEventListener('osano-cm-initialized', 
      (change) => { consentSet(window.Osano.cm.getConsent()); });
    Osano.cm.addEventListener('osano-cm-consent-changed', 
      (change) => { consentSet(change); });
  }
}
function consentSet(change) {
  if (change.hasOwnProperty('ANALYTICS') && change.ANALYTICS === 'ACCEPT') {
    _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 Osano code:

<script>
var waitForTrackerCount = 0;
function matomoWaitForTracker() {
  if (typeof _paq === 'undefined') {
    if (waitForTrackerCount < 40) {
      setTimeout(matomoWaitForTracker, 250);
      waitForTrackerCount++;
      return;
    }
  } else {
    Osano.cm.addEventListener('osano-cm-initialized', 
      (change) => { consentSet(window.Osano.cm.getConsent()); });
    Osano.cm.addEventListener('osano-cm-consent-changed', 
      (change) => { consentSet(change); });
  }
}
function consentSet(change) {
  if (change.hasOwnProperty('ANALYTICS') && change.ANALYTICS === 'ACCEPT') {
    _paq.push(['setConsentGiven']);    
  } else {
    _paq.push(['forgetConsentGiven']);               
  }
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker());
</script>

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