How to use the Clicked Data Attribute variable
In Matomo Tag Manager, the Clicked Data Attribute variable allows you to capture the value of a specific data attribute from an HTML element that users interact with on your website. It retrieves the value of a defined data-*
attribute (a custom attribute in HTML) associated with the clicked element.
This guide explains how to track clicks on buttons with a custom data attribute.
For example, track clicks on buttons with the data attribute data-resources
to indicate the type of resource (e.g., “budgeting-guide”, “retirement-planning-guide”).
<button data-resource="budgeting-guide" id="download-budgeting-guide">
<button data-resource="retirement-planning-guide" id="view-retirement-guide">
Configure the Clicked Data Attribute variable
- In Tag Manager, navigate to Variables and click Create New Variable.
- Select the Clicked Data Attribute Variable and provide a descriptive name.
- In the Clicked Data Attribute Name field, specify the attribute name you want to capture. Using the example above, enter
resource
as the attribute name (for attribute-suffix, only add the suffix:resource
). - Save the new variable. It can now be used to fire a trigger and execute a tag.
Set up a Click Trigger
- Navigate to Triggers and click Create New Trigger.
- Select the All Elements Click Trigger and provide a descriptive name.
- Add a condition to fire the trigger only when elements with the
data-resource
attribute are clicked. - Choose the Click Data Attribute created previously and specify the condition as matches the regular expression
^(?!\s*$).+
- Save the new trigger.
Set up the Matomo Analytics Tag
- Navigate to Tags and click Create New Tag.
- Select the Matomo Analytics Tag and provide a descriptive name.
- Choose the Event Tracking Type and (using the example above), define the event as you want it to appear in your reports:
Event Category: Resources
Event Action: Clicked
Event Name: select the variable created previously{{Clicked Data Attribute - Resource}}
.
- Link the All Elements Click Trigger.
- Save the new tag.
- Use Preview and Debug mode to test your setup before publishing.
- Once successfully configured, run a test event and check it is recorded in the Matomo [Real-time report].