Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-tracker-core",
"comment": "Resume sessions when disabling anonymous tracking that was already tracking session state",
"type": "none"
}
],
"packageName": "@snowplow/browser-tracker-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/javascript-tracker",
"comment": "Resume sessions when disabling anonymous tracking that was already tracking session state",
"type": "none"
}
],
"packageName": "@snowplow/javascript-tracker"
}
13 changes: 13 additions & 0 deletions libraries/browser-tracker-core/src/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1379,10 +1379,23 @@ export function Tracker(
},

disableAnonymousTracking: function (configuration?: DisableAnonymousTrackingConfiguration) {
/*
Flag for if the state storage strategy has changed and we are currently storing session state (anonymously or not).
Because the strategy changes, the below call to initializeIdsAndCookies will fail to see that a session is currently in progress and start a new one.
Detect this state, and once we've updated settings, bump the session cookie with the new strategy to ensure it exists before loading the ID cookie, which will resume the session.
*/
const shouldResumeSession =
configuration?.stateStorageStrategy && // a new strategy was defined (otherwise will already resume)
configuration.stateStorageStrategy !== configStateStorageStrategy && // new strategy is different to old one (otherwise will already resume)
(!configAnonymousTracking || configAnonymousSessionTracking) && // we were previously tracking session IDs (otherwise nothing to resume)
getSnowplowCookieValue('ses'); // and we currently have a session in progress (otherwise we want a new session anyway)

trackerConfiguration.anonymousTracking = false;

toggleAnonymousTracking(configuration);

if (shouldResumeSession) setSessionCookie(); // ensure session cookie exists with new strategy

initializeIdsAndCookies();

outQueue.executeQueue(); // There might be some events in the queue we've been unable to send in anonymous mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,20 @@ describe('Sessions', () => {
log
)
)
).toBe(3);
).toBe(4);
expect(
F.size(
F.uniqBy(
F.get('event.domain_sessionid'),
F.filter(
(e) =>
F.get('event.name_tracker', e) === 'anonymousSessionTracker' &&
F.get('event.app_id', e) === 'session-integration-' + testIdentifier,
log
)
)
)
).toBe(2);
});

it('should only increment domain_sessionidx outside of session timeout (cookie storage)', () => {
Expand Down
10 changes: 7 additions & 3 deletions trackers/javascript-tracker/test/pages/session-integration.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,25 @@

window.snowplow('newTracker', 'anonymousSessionTracker', collector_endpoint, {
appId: 'session-integration-' + testIdentifier,
cookieName: testIdentifier,
cookieName: testIdentifier + 'anon',
sessionCookieTimeout: 1,
stateStorageStrategy: 'localStorage',
anonymousTracking: { withSessionTracking: true },
});

const currentUrl = window.location.href;
const url = new URL(currentUrl);
if (url.searchParams.has("delayed")) {
if (url.searchParams.has('delayed')) {
setTimeout(function () {
window.snowplow('trackPageView:cookieSessionTracker');

window.snowplow('trackPageView:localStorageSessionTracker');

window.snowplow('trackPageView:anonymousSessionTracker');
window.snowplow('disableAnonymousTracking:anonymousSessionTracker', {
stateStorageStrategy: 'cookieAndLocalStorage',
});
window.snowplow('trackPageView:anonymousSessionTracker');
}, 0);
} else {
setTimeout(function () {
Expand All @@ -81,7 +85,7 @@
}, 0);

setTimeout(function () {
url.searchParams.set("delayed", "1");
url.searchParams.set('delayed', '1');
window.location.href = url.toString();
}, 3000);
}
Expand Down
Loading