trackingId and clientId with Google tag manager

一笑奈何 提交于 2019-12-06 08:13:08

问题


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?


回答1:


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




回答2:


This way works for me:

function getGAClientID()
{
    var trackers = ga.getAll();
    var i, len;
    for (i = 0, len = trackers.length; i < len; i += 1)
    {
        if (trackers[i].get('trackingId') === 'UA-yourcodehere')
        {
            var clientid = trackers[i].get('clientId');
            console.log(clientid);
        }
    }
}


来源:https://stackoverflow.com/questions/27189563/trackingid-and-clientid-with-google-tag-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!