什么是“var _gaq = _gaq ||” []; "对于? [英] What is "var _gaq = _gaq || []; " for?

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

问题描述

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

  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 是已经定义我们应该使用它,否则我们应该是一个数组。



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

解决方案

的全局对象。

是的,它确保定义 _gaq ,所以 _gaq.push()永远不会失败。 p>

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


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); 
})(); 

About The first line:

var _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?

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

解决方案

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

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天全站免登陆