解释Google Analytics异步跟踪器 [英] Explaining Google Analytics async tracker

查看:116
本文介绍了解释Google Analytics异步跟踪器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个谷歌的异步分析跟踪器如何工作。以下代码用于初始化命令数组:

 < script type =text / javascript> 
var _gaq = _gaq || [];
_gaq.push(
['_setAccount','UA-xxxxxxxx-x'],
['_trackPageview']
);
< / script>

现在,这是一个标准数组,一旦GA代码加载并被用作我的困惑在于想知道如果用户点击导致重新加载的链接(在GA之前,这些点击可能会持续存在) JavaScript正在加载)。如果GA代码没有捕获到_gaq对象的推送,那么用户点击一个链接并进入一个新页面,这个数组每次都会被重新初始化。

JavaScript变量不会在导致刷新的请求间持续存在吗?如果是这样,那么我们是不是失去了导致页面重新加载的原始点击?



任何解释都非常感谢。


是的,你说得对,如果用户在ga.js加载之前点击远离网站并向Google的服务器执行了__utm.gif请求,那么它将不会跟踪_gaq数组,并且该信息将永远消失。但是这个版本代码仍然比旧的同步代码提供了许多好处。



首先,使用这种方法加载ga.js不是阻塞。

巧妙地,ga.js的加载是通过JavaScript间接注入的,而不是通过硬编码的< script> 标签。根据 Google代码博客


片段
的后半部分提供了将
跟踪代码与其他
脚本并行加载的逻辑这一页。它执行一个
匿名函数,它动态地
创建一个元素,并使用适当的协议将
设置为源。
因此,大多数浏览器会在页面上加载
跟踪代码并行加载
其他脚本,因此
可以减少网页加载时间。

这意味着对于大多数现代浏览器来说,ga.js的加载是以非阻塞的方式进行的(作为一个好处, async =true部分,目前支持 FF 4+,IE10p2 + Chrome 12+,Safari 5.1+,将这种异步形式化)。这可以轻松减少加载时间,并轻微降低ga.js加载之前发生点击的可能性。

提前排队_gaq数组的好处是可以防止竞争条件;事先,如果您在加载ga.js(例如,事件跟踪视频播放)之前尝试进行GA呼叫,它会引发错误,并且Event调用将会丢失并且永远无法恢复。这样,只要ga.js最终加载,_gaq数组就可以在加载时为所有调用提供服务。

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>

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.

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?

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?

Any explanation is greatly appreciated.

解决方案

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.

First, the loading of ga.js using this method is not blocking.

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.

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.

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屋!

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