Cookiehub Datalayer integration

Hi,

I would like to use Matomo Tag Manager also for my User/Cookie Consent Service Cookiehub (similar to Cookiefirst). They have an integration guide for Google Tag Manager, which I followed [1] but It does not seem to work with MTM. I think the problem is that Cookiehub writes into window.datalayer and MTM does not pick that up. How can I approach this? I have a dev environment you can check out [2]

thx!

[1] https://support.cookiehub.com/article/77-google-tag-manager-initial-setup
[2] https://p2-docma-dev-frontend.eu.ngrok.io/

There are different options -

  1. You could use the Cookiehub event system to keep track of the current consent settings and “tell” Matomo about these changes - https://support.cookiehub.com/article/87-events

  2. You overwrite the datalayer.push function for GTM to also push to the Matomo dataLayer (_mtm).

Hi Peterbo, thanks for the reply.

Overwriting the datalayer.push function sounds like a valid approach for my case. Do you have a quick snippet, how to do that. I tried this, but it does not seem to work:

    <script type="text/javascript">
      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.type = "text/javascript";
      g.async = true;
      g.src =
        "MYSOURCE.js";
      s.parentNode.insertBefore(g, s);
      
      var dataLayer = [];
      dataLayer.push = _mtm.push
    </script>

I’m not really a JS professional, but this could work. On the other hand, I don’t know, what sideeffects this could cause. Perhaps a JS ninja can help out?

 var _mtm = window._mtm = window._mtm || [];
 dataLayer.push = function() { 
    _mtm.push.apply(_mtm, arguments);
    return Array.prototype.push.apply(this, arguments);
};

If you then push something to the dataLayer dataLayer.push({'event': "test1"});, it is also applied to the Matomo dataLayer _mtm.

1 Like

Hi Peterbo,

worked like a charm, thank you very much for this!

For anyone running into similar issues here is my complete MTM snippet:

    <!-- Matomo Tag Manager -->
    <script type="text/javascript">
      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.type = "text/javascript";
      g.async = true;
      g.src =
        "MYSOURCE.js";
      s.parentNode.insertBefore(g, s);

      var dataLayer = {};
      dataLayer.push = function() {
        _mtm.push.apply(_mtm, arguments);
        return Array.prototype.push.apply(this, arguments);
      };
    </script>
    <!-- End Matomo Tag Manager -->
2 Likes