Google标签管理器的递送属性
我想为GTM标签设置defer
属性,并且效果很好。但是,我的GTM包含另外10个脚本。 j.defer = true
不影响这些脚本。如何将这些脚本属性作为defer
而不是async
? (可以参考此图像)
<script>
(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
j.defer = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'XXX-XXXXXXX');
</script>
I want to set the defer
attribute for the GTM tag and it works well. But, my GTM contains another 10 scripts. The j.defer = true
does not affect those scripts. How can I make those script attribute as defer
instead of async
? (can refer to this image)
Script for GTM
<script>
(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
j.defer = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'XXX-XXXXXXX');
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GTM通过标签中的用户定义逻辑添加脚本。您可以访问您的GTM帐户,查找加载脚本的标签并对他们做完全相同的标签。但这是没有道理的,因为GTM已经推迟了,因此即使不明确推迟推迟,它添加到DOM中的任何脚本都会有效地推迟。
另一方面,除非您的GTM容器包含逻辑山,否则延迟应以任何可测量的方式与异步有所不同,但可能会损害GTM收集的数据。
说,如果页面加载更长,并且用户不想等待而退出,则由于GTM被推迟,因此您将不知道该弹跳的用户。我敢肯定,如果您使用DataLayer,或者GTM依赖于在页面加载之前可能会发射的其他事件侦听器,则事件计时中还有更多的边缘情况。
GTM adds scripts through user-defined logic in tags. You could get access to your GTM account, find tags that load scripts and do exactly the same to them. But it makes no sense since GTM is already deferred, so any scripts it adds to the DOM will effectively be deferred even if not explicitly deferred.
On the other hand, Unless your GTM container contains mountains of logic, defer should not differ from async in any measurable way, but it may harm the data that GTM collects.
Say, in case the page loads longer, and the user doesn't wanna wait and just quit, you will not know about that bounced user since GTM is deferred. I'm sure there are more edge cases in events timing in case you use datalayer or if GTM relies on other event listeners that could fire before the page is done loading.