jquery-mobile - jQuery Mobile 谷歌通用分析

标签 jquery-mobile google-analytics 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>

最佳答案

除了 '$.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>

关于jquery-mobile - jQuery Mobile 谷歌通用分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872243/

相关文章:

javascript - Jquery Mobile 复制自动分隔符

jquery-mobile - 在 jQuery Mobile 中将固定位置设置为 DIV [不是页脚或页眉] 无法按预期工作

jquery - 如何自定义水平 jQuery-mobile 单选按钮

javascript - 如何防止谷歌分析从本地主机获取流量?

google-analytics - Google Analytics - 收集限制和抽样

css - 覆盖 jquery.mobile-1.0b3.min.css 中的样式

ios - 跟踪每个应用版本的用户百分比

javascript - 使用 Google Analytics 目标跟踪基于 AJAX 的表单

google-analytics - Google 跟踪代码管理器 - 通用分析

google-analytics - 离线应用程序的分析+统计?