Implementing Custom Dimensions with Google Tag Manager

Hi I am trying to push Custom Dimensions using Google Tag Manager, but so far nothing is showing up in Piwik.

I have implemented the following javascript on the page where I want to push the customer dimension:

I have set up this dimension as an ‘Action Dimension’ in Piwik.

The ‘{{transactionId}}’ is a data layer variable which I have set up in Google Tag Manager.

Hoping someone can help solve this issue.

Hi!

I’ve got almost the same issue: we’re trying to implement some custom dimension for a client that want’s to use both Google Analytics and Matomo - both implemented via GTM.

We’ve set up Simo Ahava’s excellent solution for tracking clientID and sessionID (Improve Data Collection With Four Custom Dimensions | Simo Ahava’s blog) and want to send those dimensions to Matomo as well.

I’ve managed to get the sessionID to work by implementing the custom dimension push in the custom HTML-tag tag as below:

<script>
  var _paq = _paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['setCustomDimension', 2, '{{randomSessionId}}']);
  _paq.push(['setCustomUrl', '{{getPageViewUrl}}']);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//piwik.clienturl.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '{{getMatomoSiteID}}']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

And it works. But some custom dimensions need to be sent with separate pushes. For them I first created a new custom HTML-tag like this:

<script>
_paq.push(['setCustomDimension', 4, 'dimensionValue']);
</script>

…and that doesn’t work, so I figured I needed to add some more tracking info to get Matomo to understand where to put the data - so I modified the page view tag like this (removed the actual page view pushes to avoid duplicate tracking):

<script>
  var _paq = _paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['setCustomDimension', 4, 'dimensionValue']);
  (function() {
    var u="//piwik.clienturl.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '{{getMatomoSiteID}}']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

But that doesn’t work either and now I’m running out of ideas - does anyone else got any?

1 Like