Skip to content

Commit 8033316

Browse files
authored
πŸ› [RUM-11850] Remove anonymous Id when consent is not granted (#3883)
* πŸ› Remove anonymous Id when consent is not granted * Improve code * Improve code * Fix: rebase conflicts
1 parent 2c62a9c commit 8033316

File tree

8 files changed

+62
-15
lines changed

8 files changed

+62
-15
lines changed

β€Žpackages/core/src/domain/session/sessionManager.spec.tsβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,16 @@ describe('startSessionManager', () => {
631631
expectSessionIdToBeDefined(sessionManager)
632632
expect(sessionManager.findSession()!.id).not.toBe(initialSessionId)
633633
})
634+
635+
it('Remove anonymousId when tracking consent is withdrawn', () => {
636+
const trackingConsentState = createTrackingConsentState(TrackingConsent.GRANTED)
637+
const sessionManager = startSessionManagerWithDefaults({ trackingConsentState })
638+
const session = sessionManager.findSession()!
639+
640+
trackingConsentState.update(TrackingConsent.NOT_GRANTED)
641+
642+
expect(session.anonymousId).toBeUndefined()
643+
})
634644
})
635645

636646
describe('session state update', () => {

β€Žpackages/core/src/domain/session/sessionManager.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function startSessionManager<TrackingType extends string>(
9292
if (trackingConsentState.isGranted()) {
9393
sessionStore.expandOrRenewSession()
9494
} else {
95-
sessionStore.expire()
95+
sessionStore.expire(false)
9696
}
9797
})
9898

β€Žpackages/core/src/domain/session/sessionState.tsβ€Ž

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { isEmptyObject } from '../../tools/utils/objectUtils'
22
import { objectEntries } from '../../tools/utils/polyfills'
33
import { dateNow } from '../../tools/utils/timeUtils'
4-
import { generateUUID } from '../../tools/utils/stringUtils'
54
import type { Configuration } from '../configuration'
65
import { SESSION_EXPIRATION_DELAY, SESSION_TIME_OUT_DELAY } from './sessionConstants'
76
import { isValidSessionString, SESSION_ENTRY_REGEXP, SESSION_ENTRY_SEPARATOR } from './sessionStateValidation'
@@ -23,12 +22,8 @@ export function getExpiredSessionState(
2322
const expiredSessionState: SessionState = {
2423
isExpired: EXPIRED,
2524
}
26-
if (configuration.trackAnonymousUser) {
27-
if (previousSessionState?.anonymousId) {
28-
expiredSessionState.anonymousId = previousSessionState?.anonymousId
29-
} else {
30-
expiredSessionState.anonymousId = generateUUID()
31-
}
25+
if (configuration.trackAnonymousUser && previousSessionState?.anonymousId) {
26+
expiredSessionState.anonymousId = previousSessionState?.anonymousId
3227
}
3328
return expiredSessionState
3429
}

β€Žpackages/core/src/domain/session/sessionStore.spec.tsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ describe('session store', () => {
249249
expect(sessionStoreManager.getSession().isExpired).toBeUndefined()
250250
expect(sessionStoreManager.getSession()[PRODUCT_KEY]).toBeDefined()
251251
})
252+
253+
it('should generate an anonymousId if not present', () => {
254+
setupSessionStore()
255+
expect(sessionStoreManager.getSession().anonymousId).toBeDefined()
256+
})
252257
})
253258

254259
describe('expand or renew session', () => {
@@ -574,6 +579,12 @@ describe('session store', () => {
574579
expect(sessionStoreManager.getSession().id).toBe(FIRST_ID)
575580
expect(sessionStoreManager.getSession().isExpired).toBeUndefined()
576581
})
582+
583+
it('restart session should generate an anonymousId if not present', () => {
584+
setupSessionStore()
585+
sessionStoreManager.restartSession()
586+
expect(sessionStoreManager.getSession().anonymousId).toBeDefined()
587+
})
577588
})
578589
})
579590

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface SessionStore {
2626
renewObservable: Observable<void>
2727
expireObservable: Observable<void>
2828
sessionStateUpdateObservable: Observable<{ previousState: SessionState; newState: SessionState }>
29-
expire: () => void
29+
expire: (hasConsent?: boolean) => void
3030
stop: () => void
3131
updateSessionState: (state: Partial<SessionState>) => void
3232
}
@@ -170,6 +170,7 @@ export function startSessionStore<TrackingType extends string>(
170170
{
171171
process: (sessionState) => {
172172
if (isSessionInNotStartedState(sessionState)) {
173+
sessionState.anonymousId = generateUUID()
173174
return getExpiredSessionState(sessionState, configuration)
174175
}
175176
},
@@ -231,8 +232,11 @@ export function startSessionStore<TrackingType extends string>(
231232
expireObservable,
232233
sessionStateUpdateObservable,
233234
restartSession: startSession,
234-
expire: () => {
235+
expire: (hasConsent?: boolean) => {
235236
cancelExpandOrRenewSession()
237+
if (hasConsent === false && sessionCache) {
238+
delete sessionCache.anonymousId
239+
}
236240
sessionStoreStrategy.expireSession(sessionCache)
237241
synchronizeSession(getExpiredSessionState(sessionCache, configuration))
238242
},

β€Žpackages/core/src/domain/session/storeStrategies/sessionInCookie.spec.tsβ€Ž

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ describe('session in cookie strategy', () => {
3535
expect(getCookie(SESSION_STORE_KEY)).toBe('id=123&created=0')
3636
})
3737

38-
it('should set `isExpired=1` and `aid` to the cookie holding the session', () => {
38+
it('should set `isExpired=1` to the cookie holding the session', () => {
3939
const cookieStorageStrategy = setupCookieStrategy()
4040
spyOn(Math, 'random').and.callFake(() => 0)
4141
cookieStorageStrategy.persistSession(sessionState)
4242
cookieStorageStrategy.expireSession(sessionState)
4343
const session = cookieStorageStrategy.retrieveSession()
44-
expect(session).toEqual({ isExpired: '1', anonymousId: jasmine.any(String) })
45-
expect(getSessionState(SESSION_STORE_KEY)).toEqual({ isExpired: '1', anonymousId: jasmine.any(String) })
44+
expect(session).toEqual({ isExpired: '1' })
45+
expect(getSessionState(SESSION_STORE_KEY)).toEqual({ isExpired: '1' })
46+
})
47+
48+
it('should not generate an anonymousId if not present', () => {
49+
const cookieStorageStrategy = setupCookieStrategy()
50+
cookieStorageStrategy.persistSession(sessionState)
51+
const session = cookieStorageStrategy.retrieveSession()
52+
expect(session).toEqual({ id: '123', created: '0' })
53+
expect(getSessionState(SESSION_STORE_KEY)).toEqual({ id: '123', created: '0' })
4654
})
4755

4856
it('should return an empty object if session string is invalid', () => {

β€Žpackages/core/src/domain/session/storeStrategies/sessionInLocalStorage.spec.tsβ€Ž

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ describe('session in local storage strategy', () => {
4343
localStorageStrategy.persistSession(sessionState)
4444
localStorageStrategy.expireSession(sessionState)
4545
const session = localStorageStrategy?.retrieveSession()
46-
expect(session).toEqual({ isExpired: '1', anonymousId: jasmine.any(String) })
46+
expect(session).toEqual({ isExpired: '1' })
4747
expect(getSessionStateFromLocalStorage(SESSION_STORE_KEY)).toEqual({
4848
isExpired: '1',
49-
anonymousId: jasmine.any(String),
5049
})
5150
})
5251

52+
it('should not generate an anonymousId if not present', () => {
53+
const localStorageStrategy = initLocalStorageStrategy(DEFAULT_INIT_CONFIGURATION)
54+
localStorageStrategy.persistSession(sessionState)
55+
const session = localStorageStrategy.retrieveSession()
56+
expect(session).toEqual({ id: '123', created: '0' })
57+
expect(getSessionStateFromLocalStorage(SESSION_STORE_KEY)).toEqual({ id: '123', created: '0' })
58+
})
59+
5360
it('should return an empty object if session string is invalid', () => {
5461
const localStorageStrategy = initLocalStorageStrategy(DEFAULT_INIT_CONFIGURATION)
5562
window.localStorage.setItem(SESSION_STORE_KEY, '{test:42}')

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ test.describe('rum sessions', () => {
8181

8282
expect(true).toBeTruthy()
8383
})
84+
85+
createTest('removes anonymous id when tracking consent is withdrawn')
86+
.withRum()
87+
.run(async ({ browserContext, page }) => {
88+
expect((await findSessionCookie(browserContext))?.aid).toBeDefined()
89+
90+
await page.evaluate(() => {
91+
window.DD_RUM!.setTrackingConsent('not-granted')
92+
})
93+
94+
expect((await findSessionCookie(browserContext))?.aid).toBeUndefined()
95+
})
8496
})
8597

8698
test.describe('manual session expiration', () => {

0 commit comments

Comments
Β (0)