我想根据这些情况延迟加载GTM:
这样,初始页面加载时间将不会变慢,而PageSpeed得分将会上升。
在最初的测试中,我尝试删除脚本标记,然后稍后在控制台中添加它。但是,初始PageView未注册。其他事件都不是。如果在页面仍在加载时添加脚本,它似乎可以工作。
问题是:如何延迟GTM,并在加载GTM时在dataLayer中找到事件并提交主题?
这是我的工作范围:
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
// specifically this line below makes PageView get's properly triggered and fired
script.onload = () => { dataLayer.push({ event: 'gtm.js', 'gtm.start': (new Date()).getTime(), 'gtm.uniqueEventId': 0 }); }
script.src = 'https://www.googletagmanager.com/gtm.js?id=YOUR-GTM-ID-HERE'
document.head.appendChild(script);