Sunday, May 19, 2024
177
rated 0 times [  178] [ 1]  / answers: 1 / hits: 6689  / 7 Years ago, fri, october 27, 2017, 12:00:00

I have two groups of links all with the same class names, the only difference is the text in the .
I need to get the text for the clicked link and pass it to GA through GTM.



<div class=item-set>
<header>Section Title One</header>
<section class=products>
<div class=list>
<a href=/Product/60216935><img src=/ProductImages1.jpg></a>
</div>
<div class=list>
<a href=/Product/6021693x><img src=/ProductImages2.jpg></a>
</div>
<div class=list>
<a href=/Product/6021693y><img src=/ProductImages3.jpg></a>
</div>
</section>
</div>
<div class=item-set>
<header>Section Title Two</header>
<section class=products>
<div class=list>
<a href=/Product/60216935><img src=/ProductImages1.jpg></a>
</div>
<div class=list>
<a href=/Product/6021693x><img src=/ProductImages2.jpg></a>
</div>
<div class=list>
<a href=/Product/6021693y><img src=/ProductImages3.jpg></a>
</div>
</section>
</div>


I have created a custom javascript variable



function() {
$('section.products div.list').click(function() {
return $(this).closest('.item-set').find('header').text();
});
}


But the bleep thing isn't working as I expect (or at all). It returns undefined.



Any assistance is greatly appreciated.


More From » google-tag-manager

 Answers
2

You shouldn't bind to click event in JS variable. You should use JS variables in GTM only for receiving values



The correct way to achieve your goal is:



1) Enable built-in variable Click Element (if you already have it, you can skip this step)
enter



2) Create trigger, which will fire when you clicking on your links
enter
CSS selector on the screenshot is .item-set .list a



3) Create JS variable
enter
Code is: function() {
return $({{Click Element}}.closest('.item-set')).find('header').text();
}

3) Create a tag, which will send data to GA
enter
Here you can use your variable form step 3 {{Click List Header}}


[#17246] Wednesday, October 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maurodannyr

Total Points: 126
Total Questions: 103
Total Answers: 105

Location: Maldives
Member since Sun, Feb 27, 2022
2 Years ago
;