OneTrust Consent Manager (CMP) is a user-friendly consent management platform that helps you comply with global privacy regulations by managing user consent for data collection and cookies on your website.

By integrating OneTrust CMP, you can efficiently manage visitor consent, with Matomo Tag Manager automatically adjusting tracking based on the consent status provided by the CMP.

This guide explains how to configure Matomo Tag Manager with OneTrust and we recommend reading it together with the Matomo Tag Manager and Consent Managers (CMP) Integration Guide.

Prerequisites

Check the following is set up before starting the integration process:

  • You have a OneTrust account and your cookie consent banner is customised.
  • The Matomo Tag Manager script is added to your website code.
  • The basics requirements are completed for the Tag Manager Configuration.

Configure the OneTrust Tag

If you are an existing OneTrust user, remove the OneTrust script from your website code when implementing the OneTrust Tag.

  1. Go to Matomo > Tag Manager and open the container to edit.

  2. Navigate to Triggers and click Create New Trigger.

  3. Choose the Pageview trigger type and click Create New Trigger to save.

  4. Navigate to Tags and click Create a New Tag.

  5. Choose the OneTrust Tag and provide a name for the tag, which will be publicly visible in the source code when previewing and debugging the container.
    onetrust tag type matomo tag manager

  6. Add your Data domain script found in your OneTrust account > Scripts.

  7. In the section, Configure when the tag should do this, choose the Pageview trigger.
    onetrust tag configuration matomo tag manager

  8. Click Create New Tag to save.

Next, you will need to create a Custom HTML tag to include the Matomo script that manages tracking based on consent updates from OneTrust. If the user gives cookie consent, Matomo will track using cookies and if the user does not give cookie consent, Matomo will track without cookies.

  1. Go to Matomo > Tag Manager and open the container to edit.

  2. Navigate to Variables and make sure your Matomo Configuration Variable has the Require Cookie Consent option enabled. All Matomo tags in your container should reference this Matomo Configuration Variable.

  3. Go to Triggers and create a DOM Ready trigger.

  4. Navigate to Tags and link the Pageview trigger to your Matomo Analytics tag.

  5. Click Create a New Tag to add the cookie handling script.

  6. Choose the Custom HTML tag type, and provide a name for the tag, which will be publicly visible in the source code when previewing and debugging the container.

  7. Paste the following script into the Custom HTML field.

  8. Link the DOM Ready trigger and set the Position to Head Start to define where the code gets inserted into the website’s code.

<script>
  var waitForTrackerCount = 0;
  function matomoWaitForTracker() {
    if (typeof _paq === 'undefined' || typeof OnetrustActiveGroups === 'undefined') {
      if (waitForTrackerCount < 40) {
        setTimeout(matomoWaitForTracker, 250);
        waitForTrackerCount++;
        return;
      }
    } else {
      window.addEventListener('OneTrustGroupsUpdated', function (e) {
          consentSet();
      });
    }
  }
  function consentSet() {
    if (OnetrustActiveGroups.includes("C0004")) {  //replace with your specific OneTrust identifier for statistics cookies
      _paq.push(['rememberCookieConsentGiven']);
      _paq.push(['setConsentGiven']);      
    } else {
      _paq.push(['forgetCookieConsentGiven']); 
      _paq.push(['deleteCookies']);   
    }
  }
matomoWaitForTracker();
</script>

If the user gives tracking consent, Matomo will track (without cookies) and if the user does not give consent, Matomo will cease all tracking.

  1. Go to Matomo > Tag Manager and open the container to edit.

  2. Navigate to Variables and make sure your Matomo Configuration Variable has the Require Tracking Consent option enabled. All Matomo tags in your container should reference this Matomo Configuration Variable.

  3. Go to Triggers and create a DOM Ready trigger.

  4. Navigate to Tags and link the Pageview trigger to your Matomo Analytics tag.

  5. Click Create a New Tag to add the consent handling script.

  6. Choose the Custom HTML tag type, and provide a name for the tag, which will be publicly visible in the source code when previewing and debugging the container.

  7. Paste the following script into the Custom HTML field.

  8. Link the DOM Ready trigger and set the Position to Head Start to define where the code gets inserted into the website’s code.

<script>
  var waitForTrackerCount = 0;
    function matomoWaitForTracker() {
      if (typeof _paq === 'undefined' || typeof OnetrustActiveGroups === 'undefined') {
        if (waitForTrackerCount < 40) {
          setTimeout(matomoWaitForTracker, 250);
          waitForTrackerCount++;
          return;
        }
      } else {
        window.addEventListener('OneTrustGroupsUpdated', function (e) {
            consentSet();
        });
      }
    }
    function consentSet() {
      if (OnetrustActiveGroups.includes("C0004")) {  //replace with your specific OneTrust identifier for statistics cookies
        _paq.push(['setConsentGiven']);      
      } else {
        _paq.push(['forgetConsentGiven']);   
      }
    }
  matomoWaitForTracker();
</script>

Test and Publish

Once you have integrated your consent manager with Matomo Tag Manager, it is recommended to test accepting, rejecting and changing consent preferences to ensure that Matomo cookies are appropriately loaded. Learn more about our testing and validation tips.

Disclaimer: The use of any third-party tools (plugins, extensions, platforms, APIs, widgets, etc.) is at your own risk. Matomo does not own, control, maintain or support any third-party tools that integrate with our product. We recommend checking your privacy setup is correctly configured across your environment when using any third party tools.

Previous FAQ: Using Cookiebot Consent Manager CMP with Matomo Tag Manager