Skip to content

Instantly share code, notes, and snippets.

@jcleblanc
Created May 21, 2012 19:46
Show Gist options
  • Save jcleblanc/2764232 to your computer and use it in GitHub Desktop.
Save jcleblanc/2764232 to your computer and use it in GitHub Desktop.
Anonymous User Data Tracking via Local Storage & Cookies
var storeName = "visited";
if (typeof(localStorage) == "undefined" ) {
var cookieVal = readCookie(storeName);
var value = ((cookieVal === null) ? window.location : cookieVal
+ window.location);
var days = 1;
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = storeName + "=" + value + "|"
+ expires + "; path=/";
} else {
//Use Local Storage
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' '){ c = c.substring(1, c.length) };
if (c.indexOf(nameEQ) == 0){
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
var storeName = "visited";
if (typeof(localStorage) == 'undefined' ) {
//Local Storage Not Available
} else {
try {
var sites = localStorage.getItem(storeName);
sites = (sites === null) ? window.location : sites + window.location;
localStorage.setItem(storeName, sites + "|");
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
//quota exceeded
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment