什么是“var _gaq = _gaq ||"[];"为了? [英] What is "var _gaq = _gaq || []; " for?

查看:24
本文介绍了什么是“var _gaq = _gaq ||"[];"为了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Analytics 中的异步跟踪代码如下所示:

The Async Tracking code in Google Analytics looks like this:

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-XXXXX-X']); 
_gaq.push(['_trackPageview']); 

(function() { 
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
})(); 

关于第一行:

var _gaq = _gaq || []; 

我认为它确保如果 _gaq 已经定义,我们应该使用它,否则我们应该使用数组.

I think it ensures that if _gaq is already defined we should use it otherwise we should an array.

谁能解释一下这是做什么用的?

Can anybody explain what this is for?

另外,如果_gaq 被重命名有关系吗?换句话说,Google Analytics 是否依赖名为 _gaq 的全局对象?

Also, does it matter if _gaq gets renamed? In other words, does Google Analytics rely on a global object named _gaq?

推荐答案

是的,它确保定义了 _gaq,这样 _gaq.push() 永远不会失败.

Yes, it ensures that _gaq is defined, so that _gaq.push() never fails.

我不会弄乱 GA 代码中变量的名称...你有什么理由这样做吗?它是否与您的任何变量冲突?(然后我会改变我的...)

I would not mess with the name of the variables in GA's code... do you have any reason to? Does it conflict with any of your variables? (Then I would change mine...)

这篇关于什么是“var _gaq = _gaq ||"[];"为了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆