AJAX GET竞争状态? [英] AJAX GET race condition?

查看:159
本文介绍了AJAX GET竞争状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图跟踪事件时,链接会在类似于以下的方法点击我的网站。

I am attempting to track events when links are clicked on my site in a method similar to the following.

<a href="/example" class="track">Example</a>

<script type="text/javascript">
    jQuery(function($) {
        // track clicks on all anchor tags that require it
        $('a.track').live('click', function(e) {
            // send an AJAX request to our event tracking URL for the server to track it
            $.get('/events/track', {
                    url: $(this).attr('href'),
                    text: $(this).text()
            });
        });
    });
</script>

这是我遇到的问题是,一个新的页面加载中断AJAX请求,因此,有时这些事件没有被跟踪。我知道谷歌Analytics(分析)有一个 _trackPageview 功能,可尽管附着的onclick事件,这似乎并不成为一个问题了。我不知道什么是对他们的要求对我的说我看到这种竞争情况不同,GA是没有的。例如:

The problem that I'm having is that a new page load interrupts the AJAX request, and so sometimes these events aren't being tracked. I know Google Analytics has a _trackPageview function that can be attached to onclick events though, and this doesn't seem to be an issue for that. I'm wondering what's different about their call vs. mine that I'm seeing this race condition, and GA isn't. e.g.:

<a href="/example" onclick="javascript:pageTracker._trackPageview('/click/example');">Example</a>

请注意,我不是担心AJAX请求的结果......我只是希望它ping服务器的事实,一个事件发生了。

Note that I'm not worried about the result of the AJAX request...I simply want it to ping the server with the fact that an event happened.

(还有,我希望我会得到至少一个答案,说简单地跟踪从服务器端的新页面加载,而不是客户端,这是不是对这个问题的一个可以接受的答案。我'我在找的东西,像谷歌如何Analytics(分析) trackPageview 锚标签的Click事件函数的工作无论是加载了新的一页。)

(Also, I expect I'll get at least one answer that says to simply track the new page load from the server side, not the client side. This is not an acceptable answer for this question. I'm looking for something like how Google Analytics' trackPageview function works on the click event of anchor tags regardless of a new page being loaded.)

推荐答案

通过像查尔斯代理运行谷歌的 trackPageview 方法表明,调用 trackPageview ()的要求,从谷歌的各种参数服务器像素设置,这是大多数分析软件包如何收场实施这样的功能性函数(Omniture的不一样)。

Running Google's trackPageview method through a proxy like Charles shows that calls to trackPageview( ) request a pixel from Google's servers with various parameters set, which is how most analytics packages wind up implementing such pieces of functionality (Omniture does the same).

基本上得到解决ansynchronous请求没有完成,他们有客户端请求的图像和紧缩在服务器端的请求传递的参数。

Basically, to get around ansynchronous requests not completing, they have the client request an image and crunch the parameters passed in those requests on the server side.

有关你的目的,你需要实现同样的事情:写请求的图像从你的服务器的实用方法,以及你是通过URL参数(像/track.gif?page感兴趣的信息传递= foo.html和放大器;链接=点击%20Me和放大器;巴=巴兹);服务器然后将记录这些参数在数据库中,并发送回GIF。

For your end, you'd need to implement the same thing: write a utility method that requests an image from your server, passing along the information you're interested in via URL parameters (something like /track.gif?page=foo.html&link=Click%20Me&bar=baz); the server would then log those parameters in the database and send back the gif.

在此之后,它只是切片和切块你收集生成报表数据。

After that, it's merely slicing and dicing the data you've collected to generate reports.

这篇关于AJAX GET竞争状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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