Google Analytics(分析)& jQuery Mobile& jQuery 1.9 [英] Google Analytics & jQuery Mobile & jQuery 1.9

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

问题描述

以前有关于在jQuery Mobile中使用Google Analytics的问题,并提供了此处提供的成功答案。然而,这是针对1.9以前版本的jQuery的。



请有人帮助修改代码,以便将折旧的.live事件替换为当前的.on事件。



以下是JavaScript代码:

  var _gaq = _gaq || []; 
$ b $(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);
})();
});
$ b $('[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替换外,谷歌正在用analytics.js取代ga.js,作为对Universal Analytics进行更改的一部分。



我使用Walf发布的代码变体来跟踪基于jQuery Mobile的网站,但需要对其进行更改才能使用新的Universal Analytics配置。

 (function(i,s,o,g,r,a, m){
i ['GoogleAnalyticsObject'] = r; i [r] = i [r] || function(){
(i [r] .q = i [r] [])。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'); ('page page',function(event,ui){
try {
$ b $(function(){
$('[data-role = page]')
if(location.hash){
ga('send','pageview',location.hash);
}
else {
ga('send' ,'pageview');
}
}
catch(error){
}
});
});

通过Google Analytics中的位置散列(例如#about)列出的网页为我提供了我想要的信息了解访问者如何与网站互动。

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.

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.

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) {}

});

解决方案

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

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) {
        }
    });
});

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.

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

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