I'm having trouble using a Data Element to set the value of an item that may exist multiple times on a page but have different data. Basically I'm trying to know which item someone clicked on when there are a number of items on the page. Within the custom code section of the Adobe Analytics sections I'm able to execute the kind of custom code shown below to return the value I'm looking for (on our site we have a page full of listings, each one with a button to get to the specific listing, and data about each listing is contained within the <li> tag a few levels up from the actual link).
var listing = $(this).parents('.listing').data(); var s = arguments; s.eVar22 = listing.zoneMapId;
However, I'm not able to put that kind of code within a Data Element. I'm only able to get a Data Element to return the data for the first instance on the page, but the not the data for the instance that I have click on. Am I just missing something in regards to how custom code needs to be written in a Data Element? It seems like a Data Element is not able to be dynamically set based on my action like I can do at the rule level.
Using % will only work for properties of objects (ie. this.id, window.location, event.*, target.*, param.*, etc.). You can chain up to as many properties as you would like (window.location.href, etc.), but as soon as you call a method/function on an object, it will not resolve. The only exception to that is the getAttribute method (which Benjamin is using).
@Benjamin Van Wagner:
You are using this.getAttribute correctly, but you just need to remove the quotes. i.e. %this.getAttribute(data-option-value)%. The reason is because the quotes are being evaluated as part of the attribute name. In other words, it evaluates to this.getAttribute("'data-option-value'").
Thanks Rudi, this is really helpful, I didn't realize it was possible use the "this" scope inside the interface this way. This is working great for attributes at the same level, but once I try navigating up to a parent element the data being returned is just the code I wrapped in %% signs. So something like this is working for me in the console:
But wrapping it in %% and assigning it to an eVar is just returning that as text (which I'm guessing is the return of an error). I've tried building out this kind of data return for parent elements in a few different ways and I'm not having any luck. Any thoughts?
Hey - Did you ever figure this out? I am experiencing the same dilemma with the string ( %this.getAttribute('data-option-value')% ) getting set as the eVar. It works in the console, and when I switch to %this.id% it sets it correctly, so I agree that is must be an error.
I would not use a Data Element for this case. In an event based rule when you have set up the selector to correctly identify the element on the page that you are referencing above, you are then able to use the "this" scope. (this.id, this.href, (this).parents('.listing').data(); , etc)
Note: to use this to assign to an eVar within the DTM interface you must use %% signs. so. %this.id%
[img]Screen Shot 2014-02-21 at 8.23.17 AM.png[/img]