|
/** |
|
* Snippet to deal with two Snowplow cookies on the same page but with different paths. |
|
* First, load the contents of the local cookie and delete it. |
|
* Then load the contents of the root cookie. |
|
* Combine the two sets of information, for example by adding together the session counts. |
|
* Save the resulting information in the root cookie. |
|
* |
|
* WARNING! This is experimental and untested and should not be run lightly! |
|
* It may not work if the cookies do not both exist or are not set on the expected domain. |
|
*/ |
|
|
|
// The cookie creation and extraction function the JS Tracker uses |
|
/* |
|
* @version 0.3.1 |
|
* @date 2014-02-25 |
|
* @stability 2 - Unstable |
|
* @author Lauri Rooden <[email protected]> |
|
* @license MIT License |
|
*/ |
|
this.cookie = function(name, value, ttl, path, domain, secure) { |
|
|
|
if (arguments.length > 1) { |
|
return document.cookie = name + "=" + escape(value) + |
|
(ttl ? "; expires=" + new Date(+new Date()+(ttl*1000)).toUTCString() : "") + |
|
(path ? "; path=" + path : "") + |
|
(domain ? "; domain=" + domain : "") + |
|
(secure ? "; secure" : "") |
|
} |
|
|
|
return unescape((("; "+document.cookie).split("; "+name+"=")[1]||"").split(";")[0]) |
|
} |
|
|
|
// Substitute your cookie name here - you can find it with `console.log('document.cookie');` |
|
var snowplowCookieName = '_sp_id.4209'; |
|
|
|
// Substitute your root domain here |
|
var myDomain = 'snowplowanalytics.com'; |
|
|
|
// Load the local cookie |
|
var localCookie = cookie(snowplowCookieName).split('.'); |
|
|
|
if (localCookie) { |
|
|
|
// Delete the local cookie |
|
cookie(snowplowCookieName, 'anything', -1000); |
|
|
|
// Load the root cookie |
|
var rootCookie = cookie(snowplowCookieName).split('.'); |
|
|
|
if (rootCookie) { |
|
|
|
cookie(snowplowCookieName, [ |
|
|
|
// The domain user ID |
|
localCookie[0], |
|
|
|
// The time of the first visit |
|
Math.min(localCookie[1], rootCookie[1]), |
|
|
|
// Sum of visits from the two cookies |
|
localCookie[2] + rootCookie[2], |
|
|
|
// The time of the current visit |
|
Math.max(localCookie[3], rootCookie[3]), |
|
|
|
// Last visit |
|
Math.max(localCookie[4], rootCookie[4]) |
|
].join('.'), |
|
|
|
// Expire in 2 years |
|
63072000, |
|
|
|
// The root path |
|
'/', |
|
|
|
// Domain of your choice |
|
myDomain); |
|
|
|
} else { |
|
|
|
cookie(snowplowCookieName, localCookie.join('.'), |
|
|
|
// Expire in 2 years |
|
63072000, |
|
|
|
// The root path |
|
'/', |
|
|
|
// Domain of your choice |
|
myDomain); |
|
} |
|
|
|
} |