Highlighted

Tip: correct way to use "this"'s custom attribute with %...% notation

analytics_union

02-11-2019

Sharing a tip that I hope will make your life better.

When using %this....% with a custom attribute, don’t use quote marks. I.e. use %this.getAttribute(custom-attribute)% without quote marks, not %this.getAttribute("custom-attribute")%.

(BTW this goes against my JavaScript sensibilities.)

How I discovered this:

For a link click, I want to set an eVar to the value of the link’s data-foo attribute. (My link definitely has a data-foo attribute with a valid string value.)

Round 1:

In the rule’s AA - Set Variables action, in the form, I set my eVar’s value to %this.getAttribute("data-foo")%. In the custom code, for sanity sake, I set another eVar to _satellite.getVar("data-foo"), with the appropriate linkTrackVars.

--> Result: my original eVar is set with the string "null", my custom code eVar gets data-foo’s string value.

Round 2:

In the AA - Set Variables form, I use %this.data-foo%.

--> Result: my original eVar is not set at all, my custom code eVar gets data-foo’s string value.

Round 3:

In the AA - Set Variables form, I use %this["data-foo"]%.

--> Result: my original eVar is set with the string "%this[\"data-foo\"]%", my custom code eVar gets data-foo’s string value.

Round 4:

In the AA - Set Variables form, I use %this.getAttribute(data-foo)% without quote marks in the argument.

--> Result: my original eVar gets data-foo’s string value, my custom code eVar gets data-foo’s string value.