使用谷歌Analytics(分析)来跟踪AJAX请求 [英] Using Google Analytics to track AJAX requests

查看:243
本文介绍了使用谷歌Analytics(分析)来跟踪AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我改变了一个网站的一大段使用jQuery地址深层链接AJAX功能。我使用的URI像 mysite.com /#!/第1页/子页/ 等等。

I'm changing a big section of a website to use jQuery Address' deep linking AJAX features. I'm using URIs like mysite.com/#!/page1/subpage/ and so on.

我读过有关跟踪通信与 _gaq.push()功能好一点,但我想知道是否有可能做的多一点传统的时尚...

I've read a good bit about tracking traffic with the _gaq.push() function, but I was wondering if it was possible to do it in a bit more traditional fashion...

每个AJAX请求调用一个PHP函数,建立了一个页面,并返回其在< HTML> 包装,这让我很容易地定义自定义页面标题等。

Each AJAX request calls a PHP function that builds a page and returns it in an <HTML> wrapper, which lets me easily define custom page titles and so on.

如果我把分析code在该网页上,将jQuery的调用该网页触发它来跟踪访问?

If I put the analytics code on that page, will jQuery calling that page trigger it to track the visit?

推荐答案

那么你可以使用jQuery的AJAX事件在全球范围内监听AJAX请求,然后按索引到 _gaq 阵列(这似乎是最易维护的方法):

Well you can use jQuery's AJAX Events to globally listen for AJAX requests and then push an index onto the _gaq array (this seems like the most maintainable approach):

$(document).on('ajaxComplete', function (event, request, settings) {
    _gaq.push(['_trackPageview', settings.url]);
});

注意。对()是在新的jQuery 1.7和相同 .bi​​nd()的这种情况下,

Note that .on() is new in jQuery 1.7 and is the same as .bind() in this case.

另外请注意,我没有测试的顺利通过为全局AJAX事件参数的内容。

Also note that I have not tested the contents of the arguments passed for global AJAX events.

更新

您也可以使用 $ globalEval()来解析Ajax响应体加载脚本:的 http://api.jquery.com/jquery.globalEval/

You can also use $.globalEval() to parse scripts loaded in an AJAX response body: http://api.jquery.com/jquery.globalEval/

success: function(data) {

    var dom = $(data);

    dom.filter('script').each(function(){
        $.globalEval(this.text || this.textContent || this.innerHTML || '');
    });

    $('#mydiv').html(dom.find('#something').html());

}

来源:<一href="http://stackoverflow.com/questions/2699320/jquery-script-tags-in-the-html-are-parsed-out-by-jquery-and-not-executed">jQuery - 在HTML脚本标记被解析由jQuery和不执行

这篇关于使用谷歌Analytics(分析)来跟踪AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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