javascript 检查 Google Analytics _gaq 对象是否已加载并可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13414440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checking if the Google Analytics _gaq object is loaded and available
提问by crmpicco
I have some Google Analytics Tracking Code (GATC) on my site which triggers calls to the _gaq.push
method in Google's code.
我的网站上有一些 Google Analytics 跟踪代码 (GATC),它会触发对_gaq.push
Google 代码中方法的调用。
In the scenario that GA is not available, or _gaq
has not loaded, I want to ensure that I do not have any JavaScript errors on the page. By checking that _gaq
is not identical to 'undefined' - will this suffice to check if it's available and is this x-browser? I've had a look at Google's documentation, but it doesn't mention anything about this.
在 GA 不可用或_gaq
尚未加载的情况下,我想确保页面上没有任何 JavaScript 错误。通过检查它_gaq
是否与“未定义”不同 - 这是否足以检查它是否可用以及是否是 x 浏览器?我看过谷歌的文档,但它没有提到任何关于这一点的内容。
I'm aware of checking if the object is null
, but i'm not sure if this is necessary.
我知道检查对象是否是null
,但我不确定这是否有必要。
if (typeof(_gaq) !== 'undefined') {
_gaq.push(['_trackEvent', 'Downloaded Video', 'Yes']);
_gaq.push(['rollup._trackEvent', 'Downloaded Video', 'Yes']);
}
回答by Mike
If you're using Universal Analytics (analytics.js
) then switch this:
如果您使用的是 Universal Analytics ( analytics.js
),请切换:
_gaq.push(['_trackEvent', 'Downloaded Video', 'Yes']);
to this:
对此:
ga('send', 'event', 'Downloaded Video', 'Yes');
回答by David Hellsing
In the recommended javascript code you get from analytics, it includes the following row:
在您从分析中获得的推荐 javascript 代码中,它包含以下行:
var _gaq = _gaq || [];
So, the array should always be available if you keep this line in your code. If you are adding the analytics code later, just add the line above before your main scripts and it will work.
因此,如果您在代码中保留这一行,该数组应该始终可用。如果您稍后添加分析代码,只需在主脚本之前添加上面的行,它就会起作用。
Note that this snippet is harmless even if you defined _gaq
before, since it only defines it as a new Array it if it is previously undefined.
请注意,即使您_gaq
之前定义过此代码段也是无害的,因为它仅在之前未定义的情况下才将其定义为新的 Array it。
This is a great way to use asynchronous scripts, the array is defined first locally, and you can push commands to this array whenever you need. When the analytics script is loaded, it can use those commands when it wants. So no need for checking if the array is undefined or anything like that.
这是使用异步脚本的好方法,数组首先在本地定义,您可以随时将命令推送到此数组。加载分析脚本后,它可以在需要时使用这些命令。所以不需要检查数组是否未定义或类似的东西。
回答by Andy Brudtkuhl
You generally don't need to check if it's defined because the Google Analytics code snippet does this for you. However if you are doing something like event tracking in other sections of your stie as the question suggests it's sometimes good to check if the script is loaded since it's asynchronous.
您通常不需要检查它是否已定义,因为 Google Analytics 代码段会为您执行此操作。但是,如果您在 stie 的其他部分执行诸如事件跟踪之类的操作,正如问题所暗示的那样,有时最好检查脚本是否已加载,因为它是异步的。
I wrote a jQuery plugin wrapper for GA and wanted to check in the code whether or not GA was in fact installed before I tried to push any data at it. This was to cover the case that someone tried to use the plugin without GA installed or before GA was initialized.
我为 GA 编写了一个 jQuery 插件包装器,并想在尝试向其推送任何数据之前检查代码中是否确实安装了 GA。这是为了涵盖有人试图在没有安装 GA 的情况下或在 GA 初始化之前使用插件的情况。
if (typeof(_gaq) == 'undefined') {
console.error("Google Analytics is not installed");
}
回答by Open SEO
When ga.js
is loaded, it defines a window._gat
(Mind the T instead of Q) global object, you just need to test if it is defined or not (but beware it is loaded asynchronously, then you may delay your test).
当ga.js
被加载时,它定义了一个window._gat
(注意 T 而不是 Q)全局对象,你只需要测试它是否被定义(但要注意它是异步加载的,那么你可能会延迟你的测试)。
回答by mike
As @Davidsaid, as long as you have the var _gaq = _gaq || [];
snippet defined earlier, you're fine and there's no need to check for _gaq
being undefined.
正如@David所说,只要您之前var _gaq = _gaq || [];
定义了代码段,就可以了,无需检查_gaq
是否未定义。
The reason for _gaq
being defined as an empty array if it was previously undefined is so that it has a push
method. This allows your analytics code to execute before the GA code is loaded, or in some case when it's not available.
_gaq
如果之前未定义,则将其定义为空数组的原因是它有一个push
方法。这允许您的分析代码在加载 GA 代码之前执行,或者在某些情况下当它不可用时执行。
When GA loads, _gaq
will be redefined as an object with a push
method, and any existing contents of the old _gaq array will be executed.
当 GA 加载时,_gaq
将被重新定义为一个带有push
方法的对象,并且将执行旧 _gaq 数组的任何现有内容。
回答by Duncanmoo
You may want to add event tracking somewhere in js, but the CMS only adds Google Analytics for non-logged-in visitors, or certain pages. The following check for the function will work:
您可能希望在 js 中的某处添加事件跟踪,但 CMS 只会为未登录的访问者或某些页面添加 Google Analytics。该函数的以下检查将起作用:
if (typeof(ga) === 'function') {
ga('send', 'event', 'what', 'has', 'happened');
}