javascript - 禁用异步 Google Analytics 加载

标签 javascript google-analytics universal-analytics

我的网页中有这段代码用 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/

相关文章:

google-analytics - 移动 Google Analytics(分析)虚拟屏幕 View

google-analytics - Universal Analytics - 在服务器端跟踪人口统计数据

javascript - 为什么这个事件跟踪器不起作用?

JavaScript click() 方法仅在 Chrome 扩展程序中有效一次

javascript - 我无法修复 "Uncaught SyntaxError: Cannot use import statement outside a module"的问题

iOS 谷歌分析日落

javascript - Visual Studio Code 无法识别 EJS

google-analytics - 如何通过 YQL 或 Yahoo 管道进行 Google 分析数据?

google-analytics - Google Analytics - gtag 用户 ID 问题

google-analytics - Universal Analytics 跟踪代码应该放在页面中的什么位置?