竞争条件和同步使用

竞争条件和同步使用

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

问题描述

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

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.

基本上,它是一个新闻网站,所以它的标题链接到各地的故事.一个故事的标题可能会出现在一个页面上的 3 个不同位置,并出现在数百个其他页面上.因此,为了了解我们的受众如何与网站互动,我们必须跟踪每个特定标题块的使用方式,而不仅仅是目的地.由于这两个规定跟踪单个页面,也不跟踪引用页面是不够的,我们必须跟踪单个链接.

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.

如果我有链接.

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

因为 _gaq.push() 是一个异步调用,所以页面更改不会在 Google 完成点击跟踪之前发生吗?如果是这样,有没有办法防止这种情况发生,或者我对 Google Analytics Async 的运行方式有误解(http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html).

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).

推荐答案

你说得对.如果浏览器在发送事件的 GA 跟踪信标(gif 命中)之前离开页面,则不会记录该事件.然而,这对于异步代码来说并不新鲜,因为发送跟踪信标的过程是异步的;旧代码在这方面的工作方式相同.如果跟踪真的那么重要,你可以这样做:

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>

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

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 Asynchronous (_gaq)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:15