Google Analytics & jQuery Mobile & jQuery 1.9

前端 未结 4 1196
太阳男子
太阳男子 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:25

    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.

提交回复
热议问题