Laura Vecchio
Laura Vecchio

Reputation: 65

How do you get the value of a data attribute within an event-based rule in Adobe Dynamic Tag Manager (DTM)?

I have an event-based rule configured to fire on click of an element with a specific class. On click, I would like to capture the value of a data attribute that exists. The DTM documentation says you can capture attribute values using this syntax:

%this.data-event%

or

%this.id%

Example HTML:

On click of links with the class "test", I would like to store the value of "event name" within an eVar. When I used the above syntax however, the syntax is converted to a string and in the Adobe server call as:

v25:%this.data-event%

What is the best way to dynamically grab the value of an attribute of an HTML element on click within DTM?

Upvotes: 0

Views: 1813

Answers (3)

Laura Vecchio
Laura Vecchio

Reputation: 65

I found a solution. The best way to grab the value of an attribute on click is to use this syntax:

%this.getAttribute(data-title)%

The key is to not use quotes around the attribute name AND ensure the attribute has a value.

If the attribute is missing, the expression is not replaced by an empty string as one would normally expect from experience in other platforms, but instead will display the raw un-interpolated code.

Upvotes: 1

Michael Spencer
Michael Spencer

Reputation: 47

The easist way to capture the values of a data attribute against an eVar or prop on the element clicked using DTM is to set the input as the following:

%this.getAttribute(data-attributename)%

For example, if there was a data attribute on an element of data-social-share-destination='facebook' simply input %this.getAttribute(data-social-share-destination)%. This will then capture the value of 'facebook'

Data Attribute example in DTM

More detail on data attributes can be found at http://www.digitalbalance.com.au/our-blog/event-based-tracking-using-html5-custom-data-attributes/

Upvotes: 1

CrayonViolent
CrayonViolent

Reputation: 32517

DTM documentation says you can do that, but in practice I too have found that it doesn't seem to work as advertised most of the time, and will instead populate it with a literal (un-eval'd) string like that.

So what I do instead is under Conditions > Rule Conditions I create a Custom condition. In the Custom condition, I add the following:

// example to get id
_satellite.setVar('this_id',this.id);
// example to get href
_satellite.setVar('this_href',this.href);
return true;

Basically I create on-the-fly data elements using javascript, and then return true (so the condition doesn't affect the rule from triggering).

Then I use %this_id%, %this_href%, etc. syntax from the data element I created, in the Adobe Analytics section variable fields.

Upvotes: 2

Related Questions