Skip to content

Commit 37e7fa9

Browse files
šŸ› Regenerate anonymousId when session cookie is altered without preserving aid (#4203)
1 parent 91c1e08 commit 37e7fa9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

ā€Žpackages/core/src/domain/session/sessionStore.tsā€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ export function startSessionStore<TrackingType extends string>(
230230
sessionState.id = generateUUID()
231231
sessionState.created = String(dateNow())
232232
}
233+
if (configuration.trackAnonymousUser && !sessionState.anonymousId) {
234+
sessionState.anonymousId = generateUUID()
235+
}
233236
}
234237

235238
function hasSessionInCache() {

ā€Žtest/e2e/scenario/rum/sessions.scenario.tsā€Ž

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { SESSION_STORE_KEY, SESSION_TIME_OUT_DELAY } from '@datadog/browser-core'
12
import { RecordType } from '@datadog/browser-rum/src/types'
23
import { test, expect } from '@playwright/test'
4+
import { setCookie } from '../../lib/helpers/browser'
35
import { expireSession, findSessionCookie, renewSession } from '../../lib/helpers/session'
46
import { createTest, waitForRequests } from '../../lib/framework'
57

@@ -165,6 +167,39 @@ test.describe('rum sessions', () => {
165167
})
166168
})
167169

170+
test.describe('session cookie alteration', () => {
171+
createTest('after cookie is altered to isExpired, a user interaction starts a new session with a new anonymous id')
172+
.withRum()
173+
.run(async ({ flushEvents, browserContext, page }) => {
174+
const initialCookie = await findSessionCookie(browserContext)
175+
const initialSessionId = initialCookie?.id
176+
const initialAid = initialCookie?.aid
177+
178+
expect(initialSessionId).toBeDefined()
179+
expect(initialAid).toBeDefined()
180+
181+
// Simulate cookie being altered to isExpired=1 without preserving aid
182+
await setCookie(page, SESSION_STORE_KEY, 'isExpired=1', SESSION_TIME_OUT_DELAY)
183+
184+
// Cookies are cached for 1s, wait until the cache expires
185+
await page.waitForTimeout(1100)
186+
187+
await page.locator('html').click()
188+
189+
// The session is not created right away, let's wait until we see a cookie
190+
await page.waitForTimeout(1000)
191+
192+
await flushEvents()
193+
194+
const newCookie = await findSessionCookie(browserContext)
195+
expect(newCookie?.isExpired).not.toEqual('1')
196+
expect(newCookie?.id).toBeDefined()
197+
expect(newCookie?.id).not.toEqual(initialSessionId)
198+
expect(newCookie?.aid).toBeDefined()
199+
expect(newCookie?.aid).not.toEqual(initialAid)
200+
})
201+
})
202+
168203
test.describe('third party cookie clearing', () => {
169204
createTest('after a 3rd party clears the cookies, do not restart a session on user interaction')
170205
.withRum()

0 commit comments

Comments
Ā (0)