Tracking can be triggered using the data-event=”social-share” and simply adding the social network into the data-social-share-destination value. You will now be able to track the clicks and capture which social network the user shared the page to.
3 reasons they’re a better way to track website interactions.
- All major browsers support them including the dreaded Internet Explorer 6 and 7 (via getAttribute).
- Web developers can change ids and class names at will without breaking tracking once tracking is changed to trigger off the data attribute.
- Multiple data attributes can be applied to a single element, meaning you can capture as many data values as you like.
Applying data attributes in tag management platforms.
Once you’ve applied custom data elements to your website you can easily set up the tracking in your respective Tag Management Solution (TMS). Below are examples of how to set this up within both Google Tag Manager and Adobe Dynamic Tag Management (DTM) using my previous social-share code example. Upon setting up the configuration, both of the TMS will detect clicks on elements where the data-event value of social share exists and will capture the date-social-share-destination value.
Google Tag Manager (GTM).
Firstly, go to the Variables section in GTM and ensure that “Click Element” has been ticked in the Enabled Built-In Variables.
Next up, still in the Variables section, we need to create two new user-defined variables for the data-event and data-social-share-destination data attributes.
In custom variable type choose “Data Layer Variable” and configure the name as follows “gtm.element.dataset.{attribute name}”. So, for example data-event becomes “gtm.element.dataset.event”.
Note that data attributes containing multiple hyphens will need to be input as lowerCamelCase (for example data-social-share-destination will become gtm.element.dataset.socialShareDestination).
Once the custom variables have been set up you’re ready to create the trigger for the rule. In the Triggers section of GTM choose ‘click’ for both the event and the trigger type. For the Fire On settings, select the data-event variable created and set it to fire when the value equals social-share.
Lastly, to set up the Google Analytics Event Tracking, navigate to the Tag section of GTM and set up the following:
Product: Google Analytics
Tag Type: Universal Analytics
Configure Tag:
- Tracking ID: {Insert your GA Tracking ID}
- Track Type: Event
- Category: {Type the value you wish to set as the category – I’ve set my rule below to the data-event value}
- Action: {Type the value you wish to set as the action – I’ve set my rule below to click}
- Label: Insert your Social Share Destination Custom Variable. This will populate the label with the value in the data-social-share-destination attribute.
Fire On: Click and select the “Data Event equals social-share” trigger created in the last step.
Adobe Dynamic Tag Management (DTM).
Within Adobe DTM we can set up event based rules. We simply set up a rule to trigger when the data attribute is clicked, and assign different attribute values for capturing the data.
Using the social share example again, input “a” as the element tag and tick the Manually assign properties & attributes option. In the property and value fields input ‘data-event’ and ‘social-share‘ respectively.
Next, set the analytics you wish to fire on click. The values of data attributes on the element clicked can easily be captured and sent to your analytics platform using DTM. To capture the value of the data-social-share-destination simply input ‘%this.getAttribute(data-social-share-destination)%’
And there you have it. A simple way to trigger event based tracking within Google Tag Manager and Adobe Dynamic Tag Management using HTML Data Attributes. By using data attributes to trigger your tracking, you will reduce the risk of tracking errors thus increasing your data quality.
Tag management is a favourite topic of ours so make sure you download our one page Guide to Tag Management and have a read of our other articles. If you’re new to tag management Getting familiar with Adobe DTM will walk you through.