Using google tag manager and setting custom dimensions


(Michael Freeman) #1

Hi all,

First-time poster, short time lurker!

I am integrating matomo for a client and have already done google analytics successfully.

However, they want custom dimensions set when a trigger happens on the site. So I set the custom dimensions as per below when the page is loaded

var _paq = [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['setCustomDimension', customDimensionId = 15, customDimensionValue = 'A VALUE']);
    _paq.push(['setCustomDimension', customDimensionId = 1, customDimensionValue = 'not set']);
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//OURPWIKI";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    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);
  })();

Then as it’s a single page app (no page refresh or hash changes) I trigger this code to push a new custom dimension to be set when a trigger happens (trigger successfully fires this code)

<script>
_paq.push(['setCustomDimension', customDimensionId = 1, customDimensionValue = '{{DataLayer - Property ID}}']);
</script>

Yet, this information isn’t coming through the system, what am I doing wrong?

Any help is VERY appreciated.

Thanks,

Michael


(Ronan Chardonneau) #2

Hi there,
To me you need to send it along with an interaction, like an event or a new pageview.


(Michael Freeman) #3

How do I structure that?


(Lukas Winkler) #4

Hi,

I think the issue is that setCustomDimension doesn’t send any data to Matomo itself but only tells the next trackPageView to also send the customDimension that was changed locally.

See also

So you always need to set custom Dimensions before tracking the page view.