有没有一种方法可以将data- *作为脚本属性包含在自定义Google跟踪代码管理器中 [英] Is there a way to include data-* as a script attribute in custom Google Tag Manager

查看:64
本文介绍了有没有一种方法可以将data- *作为脚本属性包含在自定义Google跟踪代码管理器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GTM中包含一个自定义脚本,该脚本可调用外部资源.脚本看起来像这样

I'm including a Custom script in GTM that makes a call to an external resource. The script looks like this

<script type="text/javascript" id="some-key" data-key="xxxxxxxxxxxxxxxxxx" src="link-to-external-resource"></script>

我已将其触发设置为当前触发所有页面",但是问题是,脚本呈现的DOM中没有 data-key 属性.它在DOM中的呈现如下所示

I've set its triggering to All pages which currently triggers fine But the problem is, the script renders without the data-key attribute in the DOM. It's rendering in the DOM looks like this

<script type="text/javascript" id="some-key" src="link-to-external-resource"></script>

我错过了什么吗?有没有办法在Google跟踪代码管理器中包含data- *属性?

Am I missing something? Is there a way to include data-* attributes in Google Tag Manager?

推荐答案

我可以确认,由于未知原因,GTM从附加的 script 元素中删除了 data 属性.其他元素,例如 img 似乎保留了数据属性.我已经测试了一些保留 data 属性的变通办法,但是有必要测试以这种方式添加时所添加脚本的行为是否正确:

I can confirm that for unknown reasons GTM strips the data attributes from appended script elements. Other elements like img seems to keep data attributes. I've tested some workaround that preserves the data attributes but it's necessary to test if the appended script behaves properly when added in this way:

<script>
  var script = document.createElement('script');
  script.id = 'Gdf234ds'
  script.dataset.key = 'xxxxxxxxxxxxxxxxxx'
  script.src = "https://link-to-external-resource.js"

  // use another target than head if necessary
  document.getElementsByTagName('head')[0].appendChild(script) 
</script>

这篇关于有没有一种方法可以将data- *作为脚本属性包含在自定义Google跟踪代码管理器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆