Using Osano Consent Manager with Matomo
Introduction
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.
Step 1) Connect Matomo and Osano
To have the Matomo JavaScript tracker always use the visitor consent status provided by Osano the following code should be added to the header of each website page beneath 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']);
}
}
document.addEventListener('DOMContentLoaded', matomoWaitForTracker());
</script>
Step 2) Configure the Matomo Tracker consent mode
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 Analytics was given in Osano.
“Consent to Track”
If personal data is tracked, such as user identifiers or eCommerce orders, then this mode should be used. If consent for Analytics is not given in Osano 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.
Step 2a) Consent to Cookie
- Go to Administration > Measurables > Tracking Code in your Matomo dashboard, make sure the correct website is selected.
-
Copy the tracking code shown in your 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.
Step 2b) Consent to Tracking
- Go to Administration > Measurables > Tracking Code in your Matomo dashboard, make sure the correct website is selected.
-
Copy the tracking code shown in your 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 Osano and Matomo working together, with Osano managing all visitor consent and the Matomo JavaScript tracker only tracking visitors who have given consent to be tracked.