How to set up MTM with consent manager

I’d like to switch to using the MTM, but I’m struggling to understand how to properly do that in combination with a consent manager (I’m using klaro.js).

First, I need to install the MTM container:

<!-- Matomo Tag Manager -->
var _mtm = window._mtm = window._mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=''; s.parentNode.insertBefore(g,s);
<!-- End Matomo Tag Manager -->

I did this, but I’m wondering:

  1. Where does the tracking code come from? When I’m in the dashboard and click: Tags > Create new tag > Matomo Analytics will this add the well known tracking code:
<!-- Matomo -->
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  (function() {
    var u="";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '2']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
<!-- End Matomo Code -->
  1. If the above adds the tracking code, how do I add _paq.push(['requireCookieConsent']); before the _paq.push(['trackPageView']); line?
  2. How do I make sure that _paq.push(['requireCookieConsent']); will always come before _paq.push(['trackPageView']);?

Thanks for any clarifications.


Did I ask an absolute basic question that no one want to answer or is it so hard that no one can answer? :face_with_peeking_eye:

@putzwasser You just need to enable the require cookie consent in the tag manager config.

1 Like

Hi @putzwasser

MTM already embeds the tracking code. You have to use first a page view tag or an initialization tag (in case of page view not needed)…

This can be configured in the container configuration variable, as Vui Doan mentioned (and will be executed in the good order)