Skip to content

Commit e6946ad

Browse files
committed
Add a method to start a new session (close snowplow#515)
1 parent 3a29e3e commit e6946ad

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

src/js/tracker.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,57 @@
13221322
};
13231323
}
13241324
}
1325-
1325+
1326+
/**
1327+
* Expires current session and starts a new session.
1328+
*/
1329+
function newSession() {
1330+
// If cookies are enabled, base visit count and session ID on the cookies
1331+
var nowTs = Math.round(new Date().getTime() / 1000),
1332+
ses = getSnowplowCookieValue('ses'),
1333+
id = loadDomainUserIdCookie(),
1334+
cookiesDisabled = id[0],
1335+
_domainUserId = id[1], // We could use the global (domainUserId) but this is better etiquette
1336+
createTs = id[2],
1337+
visitCount = id[3],
1338+
currentVisitTs = id[4],
1339+
lastVisitTs = id[5],
1340+
sessionIdFromCookie = id[6];
1341+
1342+
// When cookies are enabled
1343+
if (cookiesDisabled === '0') {
1344+
memorizedSessionId = sessionIdFromCookie;
1345+
1346+
// When cookie/local storage is enabled - make a new session
1347+
if (configStateStorageStrategy != 'none') {
1348+
// New session (aka new visit)
1349+
visitCount++;
1350+
// Update the last visit timestamp
1351+
lastVisitTs = currentVisitTs;
1352+
// Regenerate the session ID
1353+
memorizedSessionId = uuid.v4();
1354+
}
1355+
1356+
memorizedVisitCount = visitCount;
1357+
1358+
// Create a new session cookie
1359+
setSessionCookie()
1360+
1361+
} else {
1362+
memorizedSessionId = uuid.v4();
1363+
memorizedVisitCount++;
1364+
}
1365+
1366+
// Update cookies
1367+
if (configStateStorageStrategy != 'none') {
1368+
setDomainUserIdCookie(_domainUserId, createTs, memorizedVisitCount, nowTs,
1369+
lastVisitTs, memorizedSessionId);
1370+
setSessionCookie();
1371+
}
1372+
1373+
lastEventTime = new Date().getTime();
1374+
}
1375+
13261376
/**
13271377
* Attempts to create a context using the geolocation API and add it to commonContexts
13281378
*/
@@ -1612,6 +1662,11 @@
16121662
return getPageViewId();
16131663
},
16141664

1665+
/**
1666+
* Expires current session and starts a new session.
1667+
*/
1668+
newSession: newSession,
1669+
16151670
/**
16161671
* Get the cookie name as cookieNamePrefix + basename + . + domain.
16171672
*

0 commit comments

Comments
 (0)