谷歌分析 &jQuery Mobile &jQuery 1.9 [英] Google Analytics & jQuery Mobile & jQuery 1.9

查看:17
本文介绍了谷歌分析 &jQuery Mobile &jQuery 1.9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前有一个关于在 jquery Mobile 中使用 Google Analytics 的问题,并在 此处提供了成功的答案.但是,这是针对 1.9 之前的 jQuery 版本.

There has been a previous Question about using Google Analytics with jquery Mobile with a successful answer provided here. However this was for versions of jQuery prior to 1.9.

请有人帮助更改代码,以便将折旧的 .live 事件替换为当前的 .on 事件.我试过了,但没有成功.

Please can someone help alter the code so that the depreciated .live event is replaced with the current .on event. I have tried but with no success.

这是 JavaScript 代码:

Here is the JavaScript code:

var _gaq = _gaq || [];

$(document).ready(function(e) {
    (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + 
          '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
}); 

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

        if ($.mobile.activePage.attr("data-url")) {
            _gaq.push(['_trackPageview', $.mobile.activePage.attr("data-url")]);
        } else {
            _gaq.push(['_trackPageview']);
        }
    } catch(err) {}

});

推荐答案

除了将 .live 替换为 .on 之外,作为 Universal Analytics 更改的一部分,Google 正在用 analytics.js 替换 ga.js.

In addition to .live being replaced by .on, Google is replacing ga.js with analytics.js as part of the change to Universal Analytics.

我使用 Walf 发布的代码的变体来跟踪基于 jQuery Mobile 的站点上的各个页面,但需要对其进行更改以使用新的 Universal Analytics 配置.我在 body 标记之后有以下脚本:

I was using a variation of the code Walf posted to track individual pages on a jQuery Mobile based site, but needed to changed it around to work with the new Universal Analytics configuration. I have the following script just after the body tag:

(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
    (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date(); a = s.createElement(o),
    m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

ga('create', 'UA-XXXXXXXX-X', 'site_name.com');
ga('send', 'pageview');

$(function () {
    $('[data-role=page]').on('pageshow', function (event, ui) {
        try {
            if (location.hash) {
                ga('send', 'pageview', location.hash);
            }
            else {
                ga('send', 'pageview');
            }
        }
        catch (error) {
        }
    });
});

在 Google Analytics 中按位置哈希(例如 #about)列出的页面为我提供了我想了解的有关访问者如何与网站互动的信息.

Pages being listed by the location hash (e.g. #about) in Google Analytics gives me the information I want to see about how visitors are interacting with the site.

这篇关于谷歌分析 &jQuery Mobile &jQuery 1.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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