In Google Analytics, there is a cookie that stores a user's information that helps Google distinguish new users from existing visitors (__utma). If I were to get that information from a user's cookie, is there any way to use that information to view that particular user's interactions with my site? At this point I'm considering just using a custom variable to store that cookie's value, and go from there, but since that cookie already exists, and Google obviously uses it, do they share any of that data to help re-create a user's session (to some limited extent)?
1 Answer
Yes you can, this is the code I use, it may not be the neatest, so someone maybe able to clean it up. I have used a custom variable and believe this is the best solution.
It will only track this information after the first pageview as the cookie will not be set until then. There are workarounds such as triggering blank events so that the tracking works with all users, however this was enough for me, as if someone only visits one page, then you probably won't need to segment their data.
//Set Custom Var 1 - Visitor ID
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
var utma=getCookie("__utma");
if (utma == null) {var utma="000.999";}
var visitorID=utma.split(".");
//alert("visitor ID = " + visitorID[1]);
//GA Legacy Code
var _gaq = _gaq || [];
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'YOUR UA NUMBER']);
_gaq.push(['_setDomainName', 'YOUR DOMAIN']);
_gaq.push(['_setAllowLinker', true]);
if (visitorID[1] != "999") {_gaq.push(['_setCustomVar',1,'VisitorID',visitorID[1],1]);}//Send Custom Var 1
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
addLinkerEvents();
})();
It is important you don't import personally identifiable information as this would breach Google Analytics TOS.
-
Getting the user id from the cookie isn't the problem, but using that information on the GA reporting side, to find that person is. I see that you're using the custom var, and that's probably how you're getting anything useful back from GA. The question was is it possible without the custom var...– Mike_KAug 14, 2013 at 17:02
-
1Apologise for the confusion, if your wondering if you can do this without custom data; No, Google won't allow you to segment on that granularity without using a custom variable or similar. If you are looking for an alternative, potentially you could append a query parameter to the track page view or by using events, either way it would still be through custom data and therefore wouldn't be vastly different. If you're wondering how you can breakdown the data in GA, advanced segments or adding a secondary dimension should be the best option. Hopefully this answers the question a little better :) Aug 14, 2013 at 18:03
-
It is. That answers the question. I think some non personally identifiable index as a custom var is the way to get useful debugging info about one person's interaction with your site. GA will not give you that by default, even though they have it. +1's– Mike_KAug 16, 2013 at 14:57