'Variables in Matomo Tag Manager events

I have set up Matomo Tag Manager on-premise. My goal is to have events that trigger user clicks but with a variable event action or name. This variable comes from an attribute of the clicked object.

For example, the clicked object is <div eventId="test"></div>. I want the event name to be the eventId.

This is very useful because we have hundreds of events, and I don't want to create them by hand.

Any thoughts on this?



Solution 1:[1]

I think you can make use of the HTML5 data-attributes and access them through the Matomo Tag Manager Javascript API.

Step 1: Add your attribute to the clickable element (e.g: data-eventId = "test")

Step 2: Create a custom JS variable in the tag manager like this:

  function () {
  if (TagManager.dataLayer.get('mtm.clickElement')) {
      return TagManager.dataLayer.get('mtm.clickElement').getAttribute('data-eventId');
  } else { 
      return 'false';
  }

step 3: Use this variable to build your triggers and tags.


Resources to help you

https://www.mikkopiippo.com/matomo-analytics-event-tracking-using-data-attributes/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 imightmax