我只是出于学习原因而玩一些谷歌代码。 全世界都在使用以下谷歌代码进行分析。 如果我看这一行:
i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments) }
这对我来说一定是错误的。
i[r] 代表 i['ga']
如果我不向调用提供参数“ga”,我将在下一行中收到错误。这对我来说意味着 ||function(.. 永远不会被调用。
(function(i,s,o,g,r,a,m){ // r is the 'ga' argument
i['GoogleAnalyticsObject']=r;
i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments) }, //for what is this line?
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','//www.google-analytics.com/analytics.js','ga');
我不敢相信这是谷歌提出的问题。有什么我不知道的吗?
最佳答案
对我来说,它有助于“简化”一些事情,所以如果你替换参数值,这
i[r]=i[r] || function(){
(i[r].q=i[r].q||[]).push(arguments)
},
变成这样
window.ga = window.ga || function(){
(window.ga.q = window.ga.q || []).push(arguments)
},
如果你替换所有的缩小技巧,你会得到这个:
// check whether the Analytics object is defined
if (!('ga' in window)){
// Analytics object not defined, so define it
window.ga = function(){
// Add tasks to the queue
window.ga.q.push(arguments);
};
希望这有帮助。
关于javascript - 不明白谷歌要求分析(一行是错误的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25242662/