0

初始化谷歌分析后,我需要在我的代码中获取跟踪代码(UA-XXXXX ...)。

            (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', 'https://www.google-analytics.com/analytics.js', 'ga');
            ga('create', 'UA-XXXXXXX-XX','auto');
            ga('require', 'displayfeatures');
            ga('send', 'pageview');

在我初始化 Google Analytics 之后,如何从 ga 对象中检索 UA?

4

1 回答 1

0

经过一番研究,我发现了这种方法

function getUA(){

    var trackers = ga.getAll();
    var code;

    for (var i=0; i < trackers.length; ++i) {
        var tracker = trackers[i];
        code = tracker.get("trackingId");
    }

    return code;
}
于 2015-02-06T12:18:45.687 回答