避免Google Analytics异步跟踪中的竞争状况 [英] Avoiding race conditions in Google Analytics asynchronous tracking

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

问题描述

我的很多活动(可能是20%)都没有在Google Analytics中记录。这些都是通过在发布表单的图像按钮上进行onclick调用的所有事件。我相信代码运行速度不够快,无法在浏览器移动到下一页之前将事件提交给Google。



我想要做的是检查_gaq队列的状态,并确保在完成我的Javascript之前没有挂起的事件。



_gaq对我来说似乎有点神秘,但它确实有一个公开的属性 - u 。我一直在使用简单命令在Chrome中进行测试:

  console.log(_gaq.u); 

不幸的是,即使在_gaq.push命令之后立即运行,它总是返回0。当然,Chrome似乎总是在邮件发送前将事件发送给Google,所以我不确定我是否可以在Chrome中进行调试,因为我无法复制问题。



有谁知道是否有办法从_gaq获得队列中未决项目的计数? _gaq.u是否合适?我希望做一些更优雅的事情,而不仅仅是在我所有的事件上抛出100或200毫秒的等待。



首先,当它的数组在 ga.js 已经加载,它的一个函数名称或其实际函数的队列。

然后,当 ga时,
$ b < js
载入,它被转换成一个特殊的对象,处理队列(函数被调用)以及所有后续的 _gaq.push()立即执行它们传递的内容。



当它仍然是一个队列时, _gaq 就是一个正常的JavaScript数组。因此,您可以使用 _gaq.length 访问等待处理的项目数量。一旦队列被处理,队列就没有办法了。

onclick 竞争条件问题是常见问题。许多人喜欢通过阻止默认操作,完成事件跟踪,然后设置完成原始操作的短时间段setTimeout来避开它。



我的偏好是不要为了分析而损害用户体验(而且我这样做是为了生活)。所以,我的解决方案是使用 onmousedown onkeydown 作为替代品(我把它们称为 preclick ),它消除了竞争条件,只有极少的误报,这对于网络分析来说已经足够了,是关于近似模型,而不是科学准确性。



以下是jQuery中的一个示例:

  $。fn.preclick = function(param){
if(!param){
return this.trigger(mousedown);
}
return this .one(keydown.preclick mousedown.preclick,function(e){
if(e.type ===mousedown|| e.which === 13){
param.apply (this);
}
});
};


I am having issues with many of my events (maybe 20%) not getting recorded in Google Analytics. These are all events called with onclick on an image button that posts a form. I believe that the code isn't running fast enough to submit the events to Google before the browser moves on to the next page.

What I would like to do is check the state of _gaq's queue and make sure there are no pending events before completing my Javascript.

_gaq seems a little cryptic to me, but it does have an exposed property - u. I've been testing in Chrome with a simple command of:

console.log(_gaq.u);

Unfortunately, this always returns 0, even if run immediately after the _gaq.push command. Of course, Chrome always seems to get the events sent to Google before the post fires, so I'm not sure if I even can debug in Chrome, since I can't replicate the problem.

Does anyone know if there is a way to get the count of pending items in the queue from _gaq? Is _gaq.u the appropriate property? I would like to do something a little more elegant than just throwing a 100 or 200 ms wait on all my events.

解决方案

_gaq has 2 stages.

First, when its an array, before ga.js has loaded, its a queue of either function names with their arguments, or actual functions.

Then, when ga.js loads, it gets converted into a special object, the queue is processed (the functions are called), and all subsequent _gaq.push() calls immediately execute what they're passed.

At the time that it is still a queue, _gaq is just a normal JavaScript array. So, you can access the number of items that are waiting to be processed with _gaq.length. Once the queue is processed, there is no way the queue.

The onclick race condition issue is a common one. Many people like to get around it by preventing the default action, doing their event track, and then setting a short-timespanned setTimeout that completes the original action.

My preference is to not compromise the user experience just for analytics (and I do this for a living.) So, my solution is to use onmousedown and onkeydown as substitutes (together I refer to them as preclick. It eliminates the race condition, with only minimal false positives, which is good enough for web analytics, which is about approximate models, not scientific accuracy.

Here's an example in jQuery:

$.fn.preclick = function(param) {
    if (!param) {
        return this.trigger("mousedown");
    }
    return this.one("keydown.preclick mousedown.preclick", function(e) {
        if (e.type === "mousedown" || e.which === 13) {
            param.apply(this);
        }
    });
};

这篇关于避免Google Analytics异步跟踪中的竞争状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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