jQuery Mobile Google Universal Analytics

Lui*_*igi 2 google-analytics jquery-mobile universal-analytics

我很高兴使用这个最佳实践http://roughlybrilliant.com/jquery_mobile_best_practices#7将ga.js与jQuery Mobile集成.我计划使用新的Universal Analytics跟踪代码升级到analytics.js.我想知道使用ga.js跟踪代码的最佳实践是否可以使用下面这样的内容.

<script>
$(document).ready(function (e) {
    (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');
});

$(document).on('pageshow', '[data-role=page], [data-role=dialog]', function (event, ui) {
    try {
        ga('create', 'UA-XXXXXXXX-X', 'domain.com');
        if ($.mobile.activePage.attr("data-url")) {
            ga('send', 'pageview', '$.mobile.activePage.attr("data-url")');
        } else {
            ga('send', 'pageview');
        }
    } catch (err) {}
});
</script>
Run Code Online (Sandbox Code Playgroud)

Tom*_*tes 11

一切都应该工作,保存'$.mobile...'部分在引号.他们的一些建议虽然有点过时.折射如下

<script>
// domReady is unnecessary here and can slow down perceived performance
// $(document).ready(function (e) { 
    (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', 'domain.com'); // move this here!
// });

$(document).on('pageshow', '[data-role=page], [data-role=dialog]', function (event, ui) {
    try {
        if ($.mobile.activePage.attr("data-url")) {
            ga('send', 'pageview', $.mobile.activePage.attr("data-url")); // remove quotes
        } else {
            ga('send', 'pageview');
        }
    } catch (err) {}
});
</script>
Run Code Online (Sandbox Code Playgroud)