如何将Google Analytics集成到jQueryMobile网站中

Wit*_*tek 12 html javascript google-analytics jquery-mobile

jQueryMobile会像每个站点一样加载其第一页.通常的Google Analytics集成工作 - 跟踪请求.但是后续页面是异步加载的,并且不会跟踪用户点击.

如何将Google Analytics集成到jQueryMobile网站中,以便跟踪所有页面点击?

Fil*_*lip 15

Jon Gales写了一篇很棒的文章.

http://www.jongales.com/blog/2011/01/10/google-analytics-and-jquery-mobile/

这是他推荐使用的代码:

$('[data-role=page]').live('pageshow', function (event, ui) {
    try {
        _gaq.push(['_setAccount', 'YOUR_GA_ID']);

        hash = location.hash;

        if (hash) {
            _gaq.push(['_trackPageview', hash.substr(1)]);
        } else {
            _gaq.push(['_trackPageview']);
        }
    } catch(err) {

    }

});
Run Code Online (Sandbox Code Playgroud)

更新

由于live现在已弃用on,因此如果您使用的是jQuery 1.7+,则应使用该事件.http://api.jquery.com/on/

  • 您不需要_setAccount调用.如果您已经在第一页上有它.您可以在_setAccount之后调用多次_trackPageview (3认同)
  • jQuery Mobile 1.0 Final与jQuery Core 1.6.4打包在一起,因为升级到1.7+可能会导致问题(我看过Blackberry和1.7的白屏问题).我提出了这个问题`.on()`对于jQuery Mobile 1.0 Final来说有点太新了,但你可以在同一个庄园中使用`.delegate()`. (2认同)