admin管理员组文章数量:1326461
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.
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.
Share Improve this question asked Oct 27, 2017 at 5:08 StefWillStefWill 3631 gold badge3 silver badges13 bronze badges2 Answers
Reset to default 7You 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)
2) Create trigger, which will fire when you clicking on your links
CSS selector on the screenshot is .item-set .list a
3) Create JS variable
Code is: function() {
return $({{Click Element}}.closest('.item-set')).find('header').text();
}
3) Create a tag, which will send data to GA
Here you can use your variable form step 3 {{Click List Header}}
Maybe you are not capturing/storing the return $(this).closest('.item-set').find('header').text();
from the function?
When you for example immediately invoke your function and click a div, the text of the <header>
will be logged to the console.
(function() {
$('section.products div.list').click(function(e) {
e.preventDefault(); // to prevent the default action of the click
console.log($(this).closest('.item-set').find('header').text());
return $(this).closest('.item-set').find('header').text();
});
})();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
本文标签: google tag managerGet element text through GTM Custom JavaScript VariableStack Overflow
版权声明:本文标题:google tag manager - Get element text through GTM Custom JavaScript Variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742208559a2433273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论