0

I need to get the tracking code (UA-XXXXX...) in my code after i init google analytics.

            (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');

After i init the Google Analytics how to retrieve the UA from the ga object ?

Thomas Bolander
  • 3,892
  • 3
  • 22
  • 30
  • Sorry, can you explain a bit further? Is the tracking code in the page and you want to get it after the page loads? Or are you saying you want to insert the tracking code after the page loads? – theatlasroom Feb 06 '15 at 10:52
  • Sorry - I don't mean to be rude, but could you please explain further what you're trying to achieve? :) – Tim Feb 06 '15 at 11:11
  • Its okay. I want to retrieve the UA after i setting it. – Thomas Bolander Feb 06 '15 at 11:58
  • Its because i made a script which people are able to insert into their page. My script need to get the UA to perform some actions. @Tim – Thomas Bolander Feb 06 '15 at 12:02

1 Answers1

0

After some research i found this method

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;
}
Thomas Bolander
  • 3,892
  • 3
  • 22
  • 30