Google Analytics & jQuery Mobile & jQuery 1.9

前端 未结 4 1198
太阳男子
太阳男子 2021-01-03 17:08

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

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 17:38

    The code Google gives on the Tracking Info page you will look something like this, depending on what features you've chosen:

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
      _gaq.push(['_setDomainName', 'foo.bar']);
      _gaq.push(['_trackPageview']);
    
      (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);
      })();
    

    And you're told to put that immediately after the opening . You should keep it in the same place with jQuery Mobile (tested on v1.3) by removing the _trackPageview line and adding a simple snippet to the end of that code block, giving:

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
      _gaq.push(['_setDomainName', 'foo.bar']);
    
      (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);
      })();
    
        $(document).on('pageshow', ':jqmData(role="page")', function(evt, data) {
            var url = $(this).jqmData('absUrl');
            try {
                if (url) {
                    url = $.mobile.path.parseUrl(url)
                    _gaq.push(['_trackPageview', url.pathname + url.search + url.hash]);
                }
                else {
                    _gaq.push(['_trackPageview']);
                }
            }
            catch(e) {}
        });
    

提交回复
热议问题