Skip to content

Commit 0e162b1

Browse files
authored
Fix session not continuing when disableAnonymousTracking changes storage strategy (#1433)
1 parent f0ed85f commit 0e162b1

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "Resume sessions when disabling anonymous tracking that was already tracking session state",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/javascript-tracker",
5+
"comment": "Resume sessions when disabling anonymous tracking that was already tracking session state",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/javascript-tracker"
10+
}

libraries/browser-tracker-core/src/tracker/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,10 +1379,23 @@ export function Tracker(
13791379
},
13801380

13811381
disableAnonymousTracking: function (configuration?: DisableAnonymousTrackingConfiguration) {
1382+
/*
1383+
Flag for if the state storage strategy has changed and we are currently storing session state (anonymously or not).
1384+
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.
1385+
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.
1386+
*/
1387+
const shouldResumeSession =
1388+
configuration?.stateStorageStrategy && // a new strategy was defined (otherwise will already resume)
1389+
configuration.stateStorageStrategy !== configStateStorageStrategy && // new strategy is different to old one (otherwise will already resume)
1390+
(!configAnonymousTracking || configAnonymousSessionTracking) && // we were previously tracking session IDs (otherwise nothing to resume)
1391+
getSnowplowCookieValue('ses'); // and we currently have a session in progress (otherwise we want a new session anyway)
1392+
13821393
trackerConfiguration.anonymousTracking = false;
13831394

13841395
toggleAnonymousTracking(configuration);
13851396

1397+
if (shouldResumeSession) setSessionCookie(); // ensure session cookie exists with new strategy
1398+
13861399
initializeIdsAndCookies();
13871400

13881401
outQueue.executeQueue(); // There might be some events in the queue we've been unable to send in anonymous mode

trackers/javascript-tracker/test/integration/sessionStorage.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,20 @@ describe('Sessions', () => {
181181
log
182182
)
183183
)
184-
).toBe(3);
184+
).toBe(4);
185+
expect(
186+
F.size(
187+
F.uniqBy(
188+
F.get('event.domain_sessionid'),
189+
F.filter(
190+
(e) =>
191+
F.get('event.name_tracker', e) === 'anonymousSessionTracker' &&
192+
F.get('event.app_id', e) === 'session-integration-' + testIdentifier,
193+
log
194+
)
195+
)
196+
)
197+
).toBe(2);
185198
});
186199

187200
it('should only increment domain_sessionidx outside of session timeout (cookie storage)', () => {

trackers/javascript-tracker/test/pages/session-integration.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,25 @@
5252

5353
window.snowplow('newTracker', 'anonymousSessionTracker', collector_endpoint, {
5454
appId: 'session-integration-' + testIdentifier,
55-
cookieName: testIdentifier,
55+
cookieName: testIdentifier + 'anon',
5656
sessionCookieTimeout: 1,
5757
stateStorageStrategy: 'localStorage',
5858
anonymousTracking: { withSessionTracking: true },
5959
});
6060

6161
const currentUrl = window.location.href;
6262
const url = new URL(currentUrl);
63-
if (url.searchParams.has("delayed")) {
63+
if (url.searchParams.has('delayed')) {
6464
setTimeout(function () {
6565
window.snowplow('trackPageView:cookieSessionTracker');
6666

6767
window.snowplow('trackPageView:localStorageSessionTracker');
6868

6969
window.snowplow('trackPageView:anonymousSessionTracker');
70+
window.snowplow('disableAnonymousTracking:anonymousSessionTracker', {
71+
stateStorageStrategy: 'cookieAndLocalStorage',
72+
});
73+
window.snowplow('trackPageView:anonymousSessionTracker');
7074
}, 0);
7175
} else {
7276
setTimeout(function () {
@@ -81,7 +85,7 @@
8185
}, 0);
8286

8387
setTimeout(function () {
84-
url.searchParams.set("delayed", "1");
88+
url.searchParams.set('delayed', '1');
8589
window.location.href = url.toString();
8690
}, 3000);
8791
}

0 commit comments

Comments
 (0)