Skip to content

Instantly share code, notes, and snippets.

@takatama
Last active Dec 15, 2015
Embed
What would you like to do?
Cohort Anarisis in Google Analytics
try {
var f = function (d) {
return d.getFullYear() + ("0" + (d.getMonth() + 1)).slice(-2) + ("0" + d.getDate()).slice(-2);
};
var c = document.cookie;
var i = c.indexOf('__utma');
var s = null;
if (i < 0) {
s = f(new Date());
} else {
var utma = c.substring(i, c.indexOf(';', i));
if (utma) {
var v = utma.split('.');
if (v[2]) {
var date = new Date();
date.setTime(v[2] * 1000);
s = f(date);
}
}
}
if(s) {
_gaq.push(['_setCustomVar', 1, 'FirstVisit', s, 1]);
}
} catch (e) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment