我的网页中有这段代码用 Analytics 的 cookie 装饰网址。
var tracker = window[window.GoogleAnalyticsObject].getAll()[0];
new window.gaplugins.Linker(tracker).decorate("http://www.toto.com");
出现错误:
window[window.GoogleAnalyticsObject].getAll is not a function
window.gaplugins is undefined
就像 Google Analytics 异步加载其插件一样,我想 getAll()
和 gaplugins.linker
函数尚未声明。
我等不及 DOM 准备好了,所以我想强制同步 GA 插件加载。
感谢您的帮助
最佳答案
最好的选择是使用回调函数来执行代码,而不是强制同步执行。
像这样:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-1', 'auto');
ga('send', 'pageview');
ga(function(){
// Code in here will only run after ga is loaded.
window.tracker = window[window.GoogleAnalyticsObject].getAll()[0];
new window.gaplugins.Linker(tracker).decorate("http://www.toto.com");
})
关于javascript - 禁用异步 Google Analytics 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44417616/