Reputation:
I am working on a website for a client that uses Google Analytics Classic. They are not upgrading at this time. They are using Google Tag Manager and want me to get the client ID from Google Analytics Classic through GTM (which I have been able to accomplish) and then send that to analytics using GTM. I have it so that it fires a tag when it detects that ID, but when I go to send that ID using a variable I created it returns undefined 95%.
The Tag that gets fired is my SendToAnalytics. It is Classic Google Analytics tag type. It is triggered by WindowLoadedWithValidID which fires when my variable CheckForClientID code does not return false. The code for variable CheckForClientID is:
function() {
var getClientId = function () {
try {
ga(function (tracker) {
if (tracker != undefined) {
var clientId = tracker.get('clientId');
return clientId;
} else {
return false;
}
});
} catch (e) {
if (e) {
return false;
}
}
}
getClientId();
}
My tag SendToAnalytics is where I am having the issue. It is a page view track type and it creates a custom variable to send to GA. The value of that variable is returning undefined 95% of the time. I have tried numerous ways to get that value. I have used code that is similar to the above, I have used:
function(){
var clientID = ga.getAll()[0].get('clientId');
return clientID;
}
and anything else I can think of. It almost always returns undefined even though it is firing after the other trigger fires which depends on the GA script being loaded.
I know that the GA code is asynchronous, but once this tag fires it is because the GA script has already loaded otherwise it would continue to return false. The variable that returns undefined is confusing me because it shouldn't initialize until after this tag has been fired and therefore the GA script should have already been loaded, or is that wrong? Does GTM initialize the variable as soon it sees that the tag exists whether or not it has been fired yet? If that is the case, how would I get the value of that variable to return the clientID and not undefined?
Upvotes: 0
Views: 5930
Reputation: 1560
As the comments says, not sure why your function is actually working, if the account is really a legacy account the following function must do nothing.
function(){
var clientID = ga.getAll()[0].get('clientId'); return clientID;
}
What you have to do now:
1.- Read the cookie and get the second value
I created the following variable:
2.- create a custom JS, this read the cookie for the first time (the fastest way), them if this is not available read the tracker element , and them if everything fails to create a Google Analytics object and get the client id, replace the UA on that script, this case will happen only on the first page of a new user. This avoids hit without client id.
function(){
var cookie_utma= {{cookie - utma}};
if(typeof cookie_utma != "undefined"){
return cookie_utma.split(".")[1];
}
if(typeof _gat != "undefined")
{
return _gat._getTrackerByName()._visitCode();
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']); //change with you UA to avoid issues
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
return _gat._getTrackerByName()._visitCode();
}
Upvotes: 1
Reputation:
I was able to get this working by using the following code for my variable that the tag is using to send to Google Analytics.
function(){
window.getCookie = function (cname) {
if (document.cookie.length > 0) {
cstart = document.cookie.indexOf(cname + "=");
if (cstart != -1) {
cstart = cstart + cname.length + 1;
cend = document.cookie.indexOf(";", cstart);
if (cend == -1) {
cend = document.cookie.length;
}
return unescape(document.cookie.substring(cstart, cend));
}
}
return 'not-available';
}
var GAuserID = getCookie('__utma');
var userId = GAuserID.split('.');
var clientId = userId[1] + '.' + userId[2];
return clientId;
}
Upvotes: 0