我的标题中有google标记管理器代码段,他在我的链接中添加了一些参数。在解决这位经理之后,我不会解析这个链接。 我该怎么做?
目前我只能使用setTimeout
启动我的功能,但事情并不正确。
在这种情况下,管理器根本不工作,控制台中不显示任何内容。 此代码位于标题中 我做错了什么?
console
答案 0 :(得分:1)
我会在GTM中创建一个Custom HTML tag来激发您可以侦听的JavaScript事件。
<script>
window.dispatchEvent(new CustomEvent('gtm:loaded'))
</script>
然后,在源代码中,监听该事件。
window.addEventListener('gtm:loaded', function (event) {
// give the necessary tag a chance to run
setTimeout(function () {
// your code
}, 500)
})
注意,如果您想支持IE,则需要为CustomEvent
使用polyfill。
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
})();
来源:https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent