Skip to content

Instantly share code, notes, and snippets.

@fblundun
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save fblundun/d6f3b1d147503db3c6a1 to your computer and use it in GitHub Desktop.

Select an option

Save fblundun/d6f3b1d147503db3c6a1 to your computer and use it in GitHub Desktop.
Extracting the user ID from the first-party Snowplow cookie
/*
* Function to extract the Snowplow user ID from the first-party cookie set by the Snowplow JavaScript Tracker
*
* @param string cookieName (optional) The value used for "cookieName" in the tracker constructor argmap
* (leave blank if you did not set a custom cookie name)
*
* @return string or bool The ID string if the cookie exists or false if the cookie has not been set yet
*/
function getSnowplowDuid(cookieName) {
cookieName = cookieName || '_sp_';
var c = document.cookie.split(';');
for (var i = 0; i < c.length; i++) {
if (c[i].substr(0, 6) === cookieName + 'id') {
return c[i].split('=')[1].split('.')[0];
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment