解释 Google Analytics 异步跟踪器 [英] Explaining Google Analytics async tracker
问题描述
我对 google 的异步分析跟踪器的工作原理有所了解.以下代码用于初始化命令数组:
I have a as to how google's async analytics tracker works. The following code is used to init a command array:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(
['_setAccount', 'UA-xxxxxxxx-x'],
['_trackPageview']
);
</script>
现在,这是一个标准数组,一旦加载 GA 的代码就会被替换,并用作一种存储点击的队列.
Now, this is a standard array that gets replaced once the GA's code is loaded and is used as a sort of queue that stores your clicks.
我的困惑在于想知道如果用户单击导致重新加载的链接(在加载 GA javascript 之前),这些点击如何可能会持续存在.如果 GA 代码没有捕获到 _gaq 对象上的推送,那么用户单击一个链接并转到一个新页面,这个数组每次都只是重新初始化,不是吗?
My confusion lies in wondering how these clicks could possibly be persisted if a user clicks a link that causes a reload (prior to the GA javascript being loaded). If the GA code hasn't captured that push on the the _gaq object, then the user clicks a link and goes to a new page, this array is just re initialized each time no?
javascript 变量不会在导致刷新的请求中持续存在,这不是真的吗?如果是这种情况,我们是不是就失去了导致页面重新加载的原始点击?
Isn't it true that a javascript variable will not persist across requests that cause a refresh? If this is the case, haven't we then lost that original click that caused the page reload?
非常感谢任何解释.
推荐答案
是的,如果用户在 ga.js 加载并执行 __utm.gif 请求到 Google 的服务器之前点击离开站点,这是正确的,那么它将不会跟踪 _gaq 数组并且该信息将永远消失.但是这个版本的代码仍然比旧的同步代码提供了很多好处.
Yes, you're right that if the user clicks away from the site before ga.js has loaded and has executed the __utm.gif request to Google's servers, then it will not track the _gaq array and that information is gone forever. But this version code still provides many benefits over the older synchronous code.
首先,使用这种方法加载ga.js是不阻塞的.
First, the loading of ga.js using this method is not blocking.
巧妙地,ga.js 的加载是通过 JavaScript 间接注入的,而不是通过硬编码的 标签.根据 Google 代码博客,
Cleverly, the loading of ga.js is injected indirectly via JavaScript, rather than through a hard-coded <script>
tag. As per Google Code Blog,
片段的后半部分提供加载的逻辑跟踪代码与其他并行页面上的脚本.它执行一个动态的匿名函数创建一个元素并设置具有正确协议的源.因此,大多数浏览器将加载跟踪代码并行页面上的其他脚本,因此减少网页加载时间.
The second half of the snippet provides the logic that loads the tracking code in parallel with other scripts on the page. It executes an anonymous function that dynamically creates a element and sets the source with the proper protocol. As a result, most browsers will load the tracking code in parallel with other scripts on the page, thus reducing the web page load time.
这意味着对于大多数现代浏览器,ga.js 的加载以非阻塞方式发生(并且作为一个好处,async="true" 部分,目前在 FF 4+、IE10p2+、Chrome 12+、Safari 5.1+, 正式化了这种异步).这略微减少了加载时间,并略微降低了在 ga.js 加载之前发生点击的可能性.
This means that the loading of ga.js occurs in a non-blocking way for most modern browsers (and as a benefit, the async="true" part, currently supported in FF 4+, IE10p2+, Chrome 12+, Safari 5.1+, formalizes this asynchronization). This mildly reduces load time, and mildly reduces the likelihood that clicks will occur before ga.js has loaded.
提前排队_gaq数组的好处是可以防止竞态;之前,如果您尝试在 ga.js 加载之前进行 GA 调用(例如,事件跟踪视频播放),它会抛出错误并且事件调用将丢失且永远无法恢复.这样,只要 ga.js 最终加载,_gaq 数组就准备好在加载时为其提供所有调用.
The benefit of queuing up the _gaq array in advance is to prevent race conditions; priorly, if you tried to make GA calls before ga.js loaded (say, Event Tracking a video play), it would throw an error and the Event call would be lost and never recoverable. This way, as long as the ga.js eventually loads, the _gaq array is ready to serve it all of the calls at load time.
这篇关于解释 Google Analytics 异步跟踪器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!