。在Google标签管理器中,但在控制台中工作

发布于 2025-01-23 19:13:07 字数 1061 浏览 0 评论 0原文

在开发人员的帮助下,我们将数据属性附加到了我们想要跟踪的所有元素,但是使用以下代码,请点击AN AN-TAG上,但我想从P-TAG中获取数据归因。

 <main id="content" class="page__content">  
  <div class="app-container page__content__wrapper">
   <div class="page_content_block">
    <p data-mng="mian-forgot-primary">
     <a href="https://homepage.com">Text about the homepage</a>
   </p>
  </div>
 </div>
</main>

以下功能在控制台(勇敢的浏览器和铬)中起作用。我正在单击链接时,我正在模拟单击网站上的点击。 .closest()函数查找p-tag的DOM,并从那里提取属性数据MNG。在控制台中,我获得了我想要的“ mian-forgot-primary”值的输出。

$0.addEventListener('click', (e) => {
 e.preventDefault();
const a = e.target.closest('p');
const mng = a.getAttribute('data-mng');
console.log(mng);
});

然后,我尝试将其作为一个具有以下功能的自定义JavaScript变量作为google标签管理器中的函数实现。在Google标签管理器中预览并单击链接(以前的仿真)时,我只是获得“未定义”的值,而我不明白为什么它对我不起作用?

查看文档,我认为.closest()函数“遍历元素及其父母,直到找到与提供的选择器字符串匹配的节点”

function(){
 var gtmMng = event.target.closest('p').getAttribute('data-mng');
 return gtmMng;
}

With the help from our developer we have attached data-attributes to all elements we want to track, but with the following code the click lands on the a-tag but I want to grab the data-attribute from the p-tag.

 <main id="content" class="page__content">  
  <div class="app-container page__content__wrapper">
   <div class="page_content_block">
    <p data-mng="mian-forgot-primary">
     <a href="https://homepage.com">Text about the homepage</a>
   </p>
  </div>
 </div>
</main>

The following function works in console (Brave browser and Chrome). I'm simulating a click like a user would do on the site, when clicking the link. The .closest() function looks up the DOM for the p-tag and from there should extract the attribute data-mng. In console i get the output i want the "mian-forgot-primary" value.

$0.addEventListener('click', (e) => {
 e.preventDefault();
const a = e.target.closest('p');
const mng = a.getAttribute('data-mng');
console.log(mng);
});

I have then tried to implement it as a function in Google Tag Manager as a custom javascript variable with the following function. When previewing in Google Tag Manager and clicking on the link (the simulation from before) i just get the value 'undefined' and i don't see why it isn't working for me?

Looking at the documentation i thought that the .closest() function "traverses the element and its parents until it finds a node that matches the provided selector string"?

function(){
 var gtmMng = event.target.closest('p').getAttribute('data-mng');
 return gtmMng;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2025-01-30 19:13:07

GTM不会让您典型地接触您在正常事件回调中获得的事件。取而代之的是,您必须转到内置的GTM变量,并启用点击元素var。然后,您可以按照event.target进行使用,即单击触发器。

另外,请使用GTM预览变量调试器来确保您不会在计时问题中运行。但是,它们不太可能与单击规则相关,因此在启用点击元素var之后,下面的代码应起作用。

function(){
 return {{Click Element}}.closest('p').dataset.mng;
}

GTM doesn't give you the typical exposure to the event that you get in your normal event callbacks. Instead, you have to go to your built-in GTM variables and enable the Click Element var. Then you can use it as you would use the event.target in the case of click trigger.

Also use the GTM preview variables debugger to make sure you're not running in timing issues. They're unlikely with the click rules though, so the code below should work after you enable the Click Element var.

function(){
 return {{Click Element}}.closest('p').dataset.mng;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文