Comments on: Event Tracking with Data Attributes in GTM https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/ APIs, Sheets, and Analytics Thu, 23 Feb 2023 05:35:18 +0000 hourly 1 https://wordpress.org/?v=6.5.5 By: Ana https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-21073 Tue, 08 Dec 2020 10:12:54 +0000 https://mixedanalytics.com/?p=8047#comment-21073 In reply to Ludovic.

Sorry, I haven't seen a whole JSON object stored in a data attribute before. You can use gtm.element.dataset.gtmAutopromo in a datalayer variable to grab the whole object, but I'm not sure how you'd break out the specific attributes. I think you'd need to use the JSON.parse() method in a custom JS var to parse it.

]]>
By: Ludovic https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-21054 Mon, 07 Dec 2020 16:07:03 +0000 https://mixedanalytics.com/?p=8047#comment-21054 Also I have a question.
How catch attributes when data attributes look like this :

data-gtm-autopromo="{"emplacement":"menu2","brand":"coca","campaign":"trade"}" class=nav

Thank you 🙂

]]>
By: Ludovic https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-21043 Mon, 07 Dec 2020 10:15:58 +0000 https://mixedanalytics.com/?p=8047#comment-21043 Hey,
Very clear explanation and uses case.

Thks

]]>
By: Ana https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-18954 Wed, 02 Sep 2020 14:55:59 +0000 https://mixedanalytics.com/?p=8047#comment-18954 In reply to Chris McCreery.

You should be able to use a custom JS variable to get the value of an attribute, like this:
function() {
var e = {{Click Element}};
var closestDocType = e.closest('[docType]');
return closestDocType.getAttribute("docType");
}

Check if that works. I've written a few posts about this technique, you can find them categorized under "DOM Scraping": https://mixedanalytics.com/blog/category/dom-scraping/

]]>
By: Chris McCreery https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-18951 Wed, 02 Sep 2020 14:29:35 +0000 https://mixedanalytics.com/?p=8047#comment-18951 In reply to Chris McCreery.

Looks like it stripped my code out.

Any ways the a tag has the follow attributes I would like to capture when the link is clicked:
doctype="Terms of Service" mfg="Company Name" class="download-link"

]]>
By: Chris McCreery https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-18949 Wed, 02 Sep 2020 13:56:45 +0000 https://mixedanalytics.com/?p=8047#comment-18949 Hi Ana, I'm trying to grab an attribute from the element that was clicked but it doesn't use the dataset standard. I want to grab this "doctype" and pass it in a GA tag in GTM as an event and use that as a custom dimension. I tried using a DOM variable with CSS selector but that just grabs the first instance of class.
Thanks

]]>
By: Ana https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-18626 Tue, 18 Aug 2020 14:58:40 +0000 https://mixedanalytics.com/?p=8047#comment-18626 In reply to Manu.

Hey Manu, this is most likely because your data attribute is not on the exact element that gets clicked.
If so, it may help to do a link-click trigger instead of all clicks. If that doesn't work either, you will probably need to use the "Advanced Case" script to crawl up the DOM and return the data attribute with the closest function.

]]>
By: Manu https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-18625 Tue, 18 Aug 2020 13:40:29 +0000 https://mixedanalytics.com/?p=8047#comment-18625 Hi, I configured my data layer variable, as you stated, with gtm.element.dataset.event.title (for receiving the value of "data-title="value"). But when I configure a click-event and trying to use this variable as a tag-label, the result is "undefined".
Any idea how to fix that?
Best wishes, and thanks for your tutorial so far!
Manu

]]>
By: Ana https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-17955 Thu, 09 Jul 2020 02:38:56 +0000 https://mixedanalytics.com/?p=8047#comment-17955 In reply to Martin Marko.

Hey Martin, yep, they work the same as any other DOM element. So, if your data attribute is nested within a "product_title" class, with JS you could select it like this:
document.querySelector('.product_title [data-tile-position]');

Similarly, with GTM you could set your trigger like:
Click Element matches CSS Selector .product_title [data-tile-position]

Please let me know if that answers your question.

]]>
By: Martin Marko https://mixedanalytics.com/blog/value-data-attributes-javascript-gtm/#comment-17951 Wed, 08 Jul 2020 18:59:22 +0000 https://mixedanalytics.com/?p=8047#comment-17951 Hi, is possible to combine attributes with the classes they are nested in?

Thank you

]]>