争用条件并同步使用Google Analytics异步(_gaq) [英] Race condition and using Google Analytics Asynchronous (_gaq) synchronously

查看:174
本文介绍了争用条件并同步使用Google Analytics异步(_gaq)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站使用Google Analytics新的异步跟踪方法(_gaq)。我碰到的问题是,我想建立一些特定的链接跟踪,并担心我将创建竞争条件。



基本上,它是一个新闻网站所以它有头条新闻链接到各地的故事。一个故事的标题可能出现在一个页面的3个不同的地方,并出现在其他几百个页面上。因此,为了了解我们的受众如何与网站互动,我们必须跟踪每个特定标题栏的使用方式,而不仅仅是目的地。由于这两项规定追踪个别网页,并且追踪引用网页还不够,我们必须追踪个别链接。因此,如果我有链接。 p>

 < a href =http://www.blah.comonclick =_ gaq.push('_ trackEvent','东西')>此处< / A> 

因为_gaq.push()是一个异步调用,所以页面更改不会在Google完成点击跟踪之前发生?如果是的话有什么方法可以防止这种情况发生,或者我对Google Analytics异步的功能( http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html )。

解决方案

你说得对。如果浏览器在为该事件发送GA跟踪信标(gif hit)之前离开该页面,则该事件将不会被记录。但是,这对于异步代码并不陌生,因为发送跟踪信标的过程是异步的;旧的代码在这方面的工作方式相同。如果跟踪真的很重要,可以这样做:

  function track(link){
if( !_gat)返回true;
_gaq.push(['_trackEvent','stuff']);
setTimeout(function(){location.href = link.href'},200);
返回false;
}

...

< a href =example.comonclick =return track(this);>< / a> ;

这会停止浏览器进入下一页,点击链接时,如果GA已经加载已经(最好不要让用户等待那么久)。然后它发送事件并等待200毫秒以将用户发送到他们点击的链接的href。这增加了记录事件的可能性。通过延长超时时间,您可以增加更多的可能性,但这也可能会损害用户在此过程中的体验。这是一个你必须尝试的平衡点。

I have a website which is using Google Analytics newer asynchronous tracking method (_gaq). The problem I've run into is that I want to institute some specific link tracking and am worried that I will be creating a race condition.

Basically, it's a news website so it has headlines which link to stories all over the place. A headline for a story might appear in 3 different places on a page, and appear on hundreds of other pages. Thus, in order to understand how our audience is interacting with the site we have to track how each specific headline block is used, and not just the destination. Because of those two stipulations tracking individual pages, nor tracking referred pages won't be enough, we have to track individual links.

So if I have a link.

<a href="http://www.blah.com" onclick="_gaq.push('_trackEvent','stuff')">Here</a>

Because _gaq.push() is an asynchronous call, isn't it possible that the page change will occur prior to Google's completion of the click tracking? If so is there a way to prevent that, or do I have a misunderstanding about the way that Google Analytics Async functions (http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html).

解决方案

You're right. If the browser leaves the page before it sends the GA tracking beacon (gif hit) for the event, the event will not be recorded. This is not new to the async code however, because the process of sending the tracking beacon is asynchronous; the old code worked the same way in that respect. If tracking is really that important, you could do something like this:

function track(link) {
  if (!_gat) return true;
  _gaq.push(['_trackEvent', 'stuff']);
  setTimeout(function() {location.href=link.href'}, 200);
  return false;
}

...

<a href="example.com" onclick="return track(this);"></a>

This will stop the browser from going to the next page when the link is clicked if GA has been loaded already (it's probably best to not make the user wait that long). Then it sends the event and waits 200 milliseconds to send the user to the href of the link they clicked on. This increases the likelihood that the event will be recorded. You can increase the likelihood even more by making the timeout longer, but that also may be hurting user-experience in the process. It's a balance you'll have to experiment with.

这篇关于争用条件并同步使用Google Analytics异步(_gaq)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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