Skip to content

Commit ef99cc4

Browse files
committed
Improve code
1 parent de468a8 commit ef99cc4

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

packages/core/src/domain/session/sessionState.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@ export function getExpiredSessionState(
2323
const expiredSessionState: SessionState = {
2424
isExpired: EXPIRED,
2525
}
26-
if (!hasConsent) {
27-
return expiredSessionState
28-
}
29-
if (configuration.trackAnonymousUser) {
30-
if (previousSessionState?.anonymousId) {
31-
expiredSessionState.anonymousId = previousSessionState?.anonymousId
32-
}
26+
if (hasConsent && configuration.trackAnonymousUser && previousSessionState?.anonymousId) {
27+
expiredSessionState.anonymousId = previousSessionState?.anonymousId
3328
}
3429
return expiredSessionState
3530
}

packages/core/src/domain/session/sessionStore.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export function startSessionStore<TrackingType extends string>(
156156
}
157157
if (hasSessionInCache()) {
158158
if (isSessionInCacheOutdated(sessionState)) {
159-
expireSessionInCache(sessionState)
159+
expireSessionInCache()
160160
} else {
161161
sessionStateUpdateObservable.notify({ previousState: sessionCache, newState: sessionState })
162162
sessionCache = sessionState
@@ -170,9 +170,7 @@ export function startSessionStore<TrackingType extends string>(
170170
{
171171
process: (sessionState) => {
172172
if (isSessionInNotStartedState(sessionState)) {
173-
if (!sessionState.anonymousId) {
174-
sessionState.anonymousId = generateUUID()
175-
}
173+
sessionState.anonymousId = generateUUID()
176174
return getExpiredSessionState(sessionState, configuration)
177175
}
178176
},
@@ -206,8 +204,8 @@ export function startSessionStore<TrackingType extends string>(
206204
return sessionCache.id !== sessionState.id || sessionCache[productKey] !== sessionState[productKey]
207205
}
208206

209-
function expireSessionInCache(sessionState: SessionState) {
210-
sessionCache = getExpiredSessionState(sessionState, configuration)
207+
function expireSessionInCache() {
208+
sessionCache = getExpiredSessionState(sessionCache, configuration)
211209
expireObservable.notify()
212210
}
213211

@@ -236,9 +234,11 @@ export function startSessionStore<TrackingType extends string>(
236234
restartSession: startSession,
237235
expire: (hasConsent?: boolean) => {
238236
cancelExpandOrRenewSession()
239-
const expiredSessionState = getExpiredSessionState(sessionCache, configuration, hasConsent)
240-
sessionStoreStrategy.expireSession(expiredSessionState)
241-
synchronizeSession(expiredSessionState)
237+
if (hasConsent === false && sessionCache) {
238+
delete sessionCache.anonymousId
239+
}
240+
sessionStoreStrategy.expireSession(sessionCache)
241+
synchronizeSession(getExpiredSessionState(sessionCache, configuration))
242242
},
243243
stop: () => {
244244
clearInterval(watchSessionTimeoutId)

packages/logs/src/domain/logsSessionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function startLogsSessionManager(
3535
return session && session.trackingType === LoggerTrackingType.TRACKED
3636
? {
3737
id: session.id,
38-
anonymousId: trackingConsentState.isGranted() ? session.anonymousId : undefined,
38+
anonymousId: session.anonymousId,
3939
}
4040
: undefined
4141
},

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)