Skip to content
Advertisement

Matomo – Multiple Containers / Tag Managers on Single Site

Setup

  • My team is building a web component, which is integrated on many host sites
  • Each of these host sites is managed by a different team and has its own Matomo Tag Manager with its own id e.g. <script src="https://.../container_ugas78d7sg.js">
  • We want to only visualise our own events in our Dashboard. We don’t care about the analytics of the host sites and the host sites do not care about ours.
  • My idea was to load a second tag manager script e.g. <script src="https://.../container_asd3s99ssd.js"> and send events using this 2nd container. However both scripts want to load the tag manager in window._mtm.
  • Multiple containers on the same site should to be possible, but there is no example in the documentation

Questions:

  • How to load a second container and sent events to it without overwriting _mtm?
  • Is there a better alternative without loading a second container?

Advertisement

Answer

Can you maybe do e.g.:

<!-- Matomo Tag Manager -->
<script type="text/javascript">
    var _mtm1 = window._mtm1 = window._mtm1 || [];
    _mtm1.push({'mtm1.startTime': (new Date().getTime()), 'event': 'mtm1.Start'});

    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src='https://{$MATOMO_URL}/js/container_{$CONTAINER}.js'; s.parentNode.insertBefore(g,s);

    var d1=document, g1=d1.createElement('script'), s1=d1.getElementsByTagName('script')[1];
    g1.type='text/javascript'; g.async=true; g1.src='https://{$MATOMO_URL}/js/container_{$CONTAINER}.js'; s1.parentNode.insertBefore(g1,s1);
</script>
<!-- End Matomo Tag Manager -->

I think you only need one _mtm in the whole window, since it only contains the startTime anyways. The only thing you need to do is to load the second container to the page, and they both will use the same “startTime” variable – I see no reason why those two containers should use different “startTime” variable.

What I’m not really sure is whether you should get the first or second element of the array during the getElementsByTagName.

Hope it works!

Advertisement