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

问题描述

我曾见过很多帖子解释如何将Google分析与您的JQM网站集成,但似乎并没有为我工作。
我使用的是JQM 1.2版本,并且有一个包含此代码的外部JavaScript文件:

你能发现这不起作用吗?
我是否需要在每个页面上内联加载代码,而不是在外部文件中加载代码?

解决方案

您必须触发页面浏览到pageshow事件。



请参阅:



试试这个:

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

});

如果您将上面的代码放在外部文件中,请确保代码将在正确的时间(在JQM和页面被加载之后)...更好地将这个代码附加到主HTML文件的末尾。


I've seen lots of posts that explain how to integrate Google analytics with your JQM site, but it doesn't seem to be working for me.I'm using JQM version 1.2 and have an external javascript file containing this code:

Can you spot why this isn't working?Do I need to load the code inline on each page rather than have it in an external file?

解决方案

You have to trigger page views to the "pageshow" event.

See: http://roughlybrilliant.com/jquery_mobile_best_practices#7

Try this:

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

    });

If you put the above code in an external file, be sure that the code will be run in the right time (after JQM and the page is loaded)... better attach this code to the end of your main html file.

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

08-12 05:20