How to set Events with Dimensions in GTM

Hello dears!
We implement Matomo through GTM. So I set events as custom HTML code in GTM. I want to add Dimensions to events. Do I use my script correctly? Thanks :sunflower:

<script>
 
  var _paq = _paq || []; 
 
_paq.push(['setCustomDimension', customDimensionId = 3, customDimensionValue = '{{application_referrer}}']);
 
_paq.push(['setCustomDimension', customDimensionId = 4, customDimensionValue = '{{application_type}}']);
 
_paq.push(['setCustomDimension', customDimensionId = 5, customDimensionValue = '{{advert_type}}']);
 
_paq.push(['trackEvent', 'send_credit_form', '{{application_id}}', '{{finance_organization_id}};{{application_referrer}};{{application_type}};{{advert_type}}']);
 
</script>

Hi @Julia_A
I don’t know how works GTM.
In order to help you, can you show me how you configure it for tracking on Matomo?

(Also do you know that Matomo provides an equivalent to GTM called Matomo Tag Manager?)

1 Like

Hi @heurteph-ei dear Philippe!
Thank you for your reply.

  1. Yes, I know about MTM, but at that moment it is more convenient to us to use Matomo through GTM.
  2. GTM has simular interface. Probably in MTM I should do the same? And inset the same code?

The advantage of MTM is that dimensions are directly included in the tracking configuration:

In GTM, is there some Matomo Tag, or must you create the whole tag with HTML code? (in MTM, you can define directly some Google Tags…)

1 Like

dear @heurteph-ei Philippe I use this case
and also that one

You can also just set the custom dimensions as part of the event payload, i.e.:

<script>
  _paq.push([
    "trackEvent",
    "{{dataLayer - eventCategory}}",
    "{{dataLayer - eventAction}}",
    "{{dataLayer - eventLabel}}",
    {
      "dimension1": "{{dataLayer - reservationCheckin}}",
      "dimension2": "{{dataLayer - reservationCheckout}}",
      "dimension3": "{{dataLayer - numeroAdulti}}",
      "dimension4": "{{dataLayer - numeroBambini}}",
      "dimension5": "{{dataLayer - numeroNeonati}}",
      "dimension6": encodeURIComponent("{{dataLayer - LocalitĂ }}"),
      "dimension7": encodeURIComponent("{{dataLayer - Struttura}}")
    }   
  ]);
</script>

For reference: JavaScript Tracking Client: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v4

2 Likes

Oh, thank you so much dear @kal !
Good idea!
And what do think abou my code? Is it correct?

It’s not :slight_smile:

Documentation states:

Please note once a Custom Dimension is set, the value will be used for all following tracking requests and may lead to inaccurate results if this is not wanted.

Reference: JavaScript Tracking Client: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v4

With your implementation the event would fail because you’re exceeding the standard parameters:

You shouldn’t need to add the custom dimensions to the subsequent event (from {{application_referrer}} and further), because you’ve set them up in the lines above.

The js library should handle them for every event in the same page after you’ve set them up.

This implementation is in fact usually made with page-level dimensions such as content groupings or other page-level parameters… you set them up before every tag is fired and from there you just fire your tags, such as every tag does have them (from the pageview to all of the events that gets triggered on page).

So ask yourself: are the custom dimensions you want to track valid for every event on page or just that one event?

This should decide between one implementation or the another.

1 Like

Hi @kal !
Thanks a lot for your help!
I`ve read Tracking a Custom Dimension for one specific action only I hope carefully.
Is this one is better for me?

<script>
  _paq.push([
    "trackEvent",
    "{{dataLayer - eventCategory}}",
    "{{dataLayer - eventAction}}",
    "{{dataLayer - eventLabel}}",
    {
      "dimension1": "{{dataLayer - advertType}}",
      "dimension2": "{{dataLayer - applicationType}}",
      "dimension3": "{{dataLayer - applicationReferrer}}",
      "dimension4": "{{dataLayer - financeOrganizationId}}",
      "dimension5": "{{dataLayer - applicationId}}",
    }   
  ]);
</script>

Yup. It may be that some of the custom dimensions values you’re tracking there can be effectively used for Category, Action and Name fields instead, but that’s up to you I guess.

I’d recommend going that way because custom dimensions takes a load with data processing and can burden your database in the long term, so it’s not a good idea let them proliferate if you have built-in dimensions as alternatives.

1 Like

Wow dear @kal !
Thank you for your time and detailed advices :heart_eyes:
Have a good day and many happy clients :pray:

1 Like