jQuery Mobile Google通用分析 [英] jQuery Mobile Google Universal Analytics

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

问题描述

我一直很高兴地使用此最佳做法 http://roughlybrilliant.com/jquery_mobile_best_practices#7 用于将ga.js与jQuery Mobile集成在一起.我打算使用新的Universal Analytics跟踪代码升级到analytics.js.我不知道下面的这种代码是否可以按照ga.js跟踪代码的最佳做法工作.

I've been happily using this best practice http://roughlybrilliant.com/jquery_mobile_best_practices#7 for integrating ga.js with jQuery Mobile. I'm planning to upgrade to analytics.js using the new Universal Analytics tracking code. I wonder whether something like this below will work following the best practice using the ga.js tracking code.

<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...'部分用引号引起来之外,所有内容都应能正常工作.他们的一些建议有些偏离.在下面折射了

Everything should work above save the '$.mobile...' part being in quotes. A few of their suggestions are a bit off though. Refractored below

<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 Google通用分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆