trackingId and clientId with Google tag manager

silent-box picture silent-box · Nov 28, 2014 · Viewed 8k times · Source

Using analytics.js, i can access to trackingId or clientId with this functions:

ga.getAll()[0].get('trackingId')
ga.getAll()[0].get('clientId')

With Google tag manager, there is no ga object. How i can get that parameters?

Answer

nyuen picture nyuen · Nov 28, 2014

The tracking ID is input into GTM either through a constant string macro (for reusability), or just as a string. The client ID can be fetched through custom Javascript (from the blog I am about to mention):

function() {
    try {
       var tracker = ga.getAll()[0];
       return tracker.get('clientId');
    }
    catch(e) {
       console.log("Error fetching clientId");
       return "n/a";
    }
}

See this blog: http://www.simoahava.com/analytics/macro-magic-google-tag-manager/#7