Convert Matomo Tag Manager _mtm to dataLayer

Hello,

we have integrated Matomo Tag Manager on a website. We use the cloud variant.

Also, the cookie tool OneTrust is implemented on the website.
Since OneTrust pushes the events to the “dataLayer”, we have manually adjusted the Matomo Tag Manager script and replaced “_mtm” with “dataLayer”.

Instead of:

<!-- Matomo Tag Manager -->
<script>
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='https://cdn.matomo.cloud/XXXXXX.cloud/container_XXXXXX.js'; s.parentNode.insertBefore(g,s);
</script>
<!-- End Matomo Tag Manager -->

we implemented the following in the website code:

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

The page load and automatic events with build-in triggers work and are pushed by Matomo Tag Manager, so the data can be seen in real-time in Analytics. But custom events do not work.

For example, we have integrated the following dataLayer push, which should trigger when a contact tab is clicked:

<script> 
window.dataLayer = window.dataLayer || []; 
window.dataLayer.push({'event': 'contactTab' 
}); 
</script>

The event is also pushed to the DataLayer. The corresponding tag with the correct trigger is also created in the Matomo Tag Manager, however the Tag Manager does not respond.

What could be the reason for this? Thanks for help.

I am not sure it is a good idea to change the initial push: {'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'}.

For your issue, I think you should start by:

var _mtm = window._mtm = window._mtm || [];
var dataLayer = window.dataLayer || _mtm;
...
1 Like

Matomo is perfectly able to read dataLayer variables and events, modifying the container tag shouldn’t be needed.

1 Like

I have no doubt about that.

However from my experience I can see that an classic dataLayer.push event is translated into Matomo after pageview

image

So tio me it looks like it’s better to do _mtm.push instead of dataLayer.push if it mattes when the event will be consumed.

1 Like