-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathsessionManager.ts
More file actions
264 lines (232 loc) · 9.31 KB
/
sessionManager.ts
File metadata and controls
264 lines (232 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { Observable } from '../../tools/observable'
import type { Context } from '../../tools/serialisation/context'
import { createValueHistory } from '../../tools/valueHistory'
import type { RelativeTime } from '../../tools/utils/timeUtils'
import { clocksOrigin, dateNow, ONE_MINUTE, relativeNow } from '../../tools/utils/timeUtils'
import { addEventListener, addEventListeners, DOM_EVENT } from '../../browser/addEventListener'
import { clearInterval, setInterval } from '../../tools/timer'
import type { Configuration } from '../configuration'
import type { TrackingConsentState } from '../trackingConsent'
import { addTelemetryDebug } from '../telemetry'
import { isSyntheticsTest } from '../synthetics/syntheticsWorkerValues'
import type { CookieStore } from '../../browser/browser.types'
import { getCurrentSite } from '../../browser/cookie'
import { ExperimentalFeature, isExperimentalFeatureEnabled } from '../../tools/experimentalFeatures'
import { findLast } from '../../tools/utils/polyfills'
import { monitorError } from '../../tools/monitor'
import { SESSION_NOT_TRACKED, SESSION_TIME_OUT_DELAY, SessionPersistence } from './sessionConstants'
import { startSessionStore } from './sessionStore'
import type { SessionState } from './sessionState'
import { toSessionState } from './sessionState'
import { retrieveSessionCookie } from './storeStrategies/sessionInCookie'
import { SESSION_STORE_KEY } from './storeStrategies/sessionStoreStrategy'
import { retrieveSessionFromLocalStorage } from './storeStrategies/sessionInLocalStorage'
export interface SessionManager<TrackingType extends string> {
findSession: (
startTime?: RelativeTime,
options?: { returnInactive: boolean }
) => SessionContext<TrackingType> | undefined
renewObservable: Observable<void>
expireObservable: Observable<void>
sessionStateUpdateObservable: Observable<{ previousState: SessionState; newState: SessionState }>
expire: () => void
updateSessionState: (state: Partial<SessionState>) => void
}
export interface SessionContext<TrackingType extends string> extends Context {
id: string
trackingType: TrackingType
isReplayForced: boolean
anonymousId: string | undefined
}
export const VISIBILITY_CHECK_DELAY = ONE_MINUTE
const SESSION_CONTEXT_TIMEOUT_DELAY = SESSION_TIME_OUT_DELAY
let stopCallbacks: Array<() => void> = []
export function startSessionManager<TrackingType extends string>(
configuration: Configuration,
productKey: string,
computeTrackingType: (rawTrackingType?: string) => TrackingType,
trackingConsentState: TrackingConsentState,
onReady: (sessionManager: SessionManager<TrackingType>) => void
) {
const renewObservable = new Observable<void>()
const expireObservable = new Observable<void>()
// TODO - Improve configuration type and remove assertion
const sessionStore = startSessionStore(
configuration.sessionStoreStrategyType!,
configuration,
productKey,
computeTrackingType
)
stopCallbacks.push(() => sessionStore.stop())
const sessionContextHistory = createValueHistory<SessionContext<TrackingType>>({
expireDelay: SESSION_CONTEXT_TIMEOUT_DELAY,
})
stopCallbacks.push(() => sessionContextHistory.stop())
// We expand/renew session unconditionally as tracking consent is always granted when the session
// manager is started.
sessionStore.expandOrRenewSession(() => {
sessionStore.renewObservable.subscribe(() => {
sessionContextHistory.add(buildSessionContext(), relativeNow())
renewObservable.notify()
})
sessionStore.expireObservable.subscribe(() => {
expireObservable.notify()
sessionContextHistory.closeActive(relativeNow())
})
sessionContextHistory.add(buildSessionContext(), clocksOrigin().relative)
if (isExperimentalFeatureEnabled(ExperimentalFeature.SHORT_SESSION_INVESTIGATION)) {
const session = sessionStore.getSession()
if (session) {
detectSessionIdChange(configuration, session)
}
}
trackingConsentState.observable.subscribe(() => {
if (trackingConsentState.isGranted()) {
sessionStore.expandOrRenewSession()
} else {
sessionStore.expire(false)
}
})
trackActivity(configuration, () => {
if (trackingConsentState.isGranted()) {
sessionStore.expandOrRenewSession()
}
})
trackVisibility(configuration, () => sessionStore.expandSession())
trackResume(configuration, () => sessionStore.restartSession())
onReady({
findSession: (startTime, options) => sessionContextHistory.find(startTime, options),
renewObservable,
expireObservable,
sessionStateUpdateObservable: sessionStore.sessionStateUpdateObservable,
expire: sessionStore.expire,
updateSessionState: sessionStore.updateSessionState,
})
})
function buildSessionContext() {
const session = sessionStore.getSession()
if (!session) {
reportUnexpectedSessionState(configuration).catch(() => void 0) // Ignore errors
return {
id: 'invalid',
trackingType: SESSION_NOT_TRACKED as TrackingType,
isReplayForced: false,
anonymousId: undefined,
}
}
return {
id: session.id!,
trackingType: session[productKey] as TrackingType,
isReplayForced: !!session.forcedReplay,
anonymousId: session.anonymousId,
}
}
}
export function stopSessionManager() {
stopCallbacks.forEach((e) => e())
stopCallbacks = []
}
function trackActivity(configuration: Configuration, expandOrRenewSession: () => void) {
const { stop } = addEventListeners(
configuration,
window,
[DOM_EVENT.CLICK, DOM_EVENT.TOUCH_START, DOM_EVENT.KEY_DOWN, DOM_EVENT.SCROLL],
expandOrRenewSession,
{ capture: true, passive: true }
)
stopCallbacks.push(stop)
}
function trackVisibility(configuration: Configuration, expandSession: () => void) {
const expandSessionWhenVisible = () => {
if (document.visibilityState === 'visible') {
expandSession()
}
}
const { stop } = addEventListener(configuration, document, DOM_EVENT.VISIBILITY_CHANGE, expandSessionWhenVisible)
stopCallbacks.push(stop)
const visibilityCheckInterval = setInterval(expandSessionWhenVisible, VISIBILITY_CHECK_DELAY)
stopCallbacks.push(() => {
clearInterval(visibilityCheckInterval)
})
}
function trackResume(configuration: Configuration, cb: () => void) {
const { stop } = addEventListener(configuration, window, DOM_EVENT.RESUME, cb, { capture: true })
stopCallbacks.push(stop)
}
async function reportUnexpectedSessionState(configuration: Configuration) {
const sessionStoreStrategyType = configuration.sessionStoreStrategyType
if (!sessionStoreStrategyType) {
return
}
let rawSession
let cookieContext
if (sessionStoreStrategyType.type === SessionPersistence.COOKIE) {
rawSession = retrieveSessionCookie(sessionStoreStrategyType.cookieOptions, configuration)
cookieContext = {
cookie: await getSessionCookies(),
currentDomain: `${window.location.protocol}//${window.location.hostname}`,
}
} else {
rawSession = retrieveSessionFromLocalStorage()
}
// monitor-until: forever, could be handy to troubleshoot issues until session manager rework
addTelemetryDebug('Unexpected session state', {
sessionStoreStrategyType: sessionStoreStrategyType.type,
session: rawSession,
isSyntheticsTest: isSyntheticsTest(),
createdTimestamp: rawSession?.created,
expireTimestamp: rawSession?.expire,
...cookieContext,
})
}
function detectSessionIdChange(configuration: Configuration, initialSessionState: SessionState) {
if (!window.cookieStore || !initialSessionState.created) {
return
}
const sessionCreatedTime = Number(initialSessionState.created)
const sdkInitTime = dateNow()
const { stop } = addEventListener(configuration, cookieStore as CookieStore, DOM_EVENT.CHANGE, listener)
stopCallbacks.push(stop)
function listener(event: CookieChangeEvent) {
const changed = findLast(event.changed, (change): change is CookieListItem => change.name === SESSION_STORE_KEY)
if (!changed) {
return
}
const sessionAge = dateNow() - sessionCreatedTime
if (sessionAge > 14 * ONE_MINUTE) {
// The session might have expired just because it's too old or lack activity
stop()
} else {
const newSessionState = toSessionState(changed.value)
if (newSessionState.id && newSessionState.id !== initialSessionState.id) {
stop()
const time = dateNow() - sdkInitTime
getSessionCookies()
.then((cookie) => {
// monitor-until: 2026-04-01, after RUM-10845 investigation done
addTelemetryDebug('Session cookie changed', {
time,
session_age: sessionAge,
old: initialSessionState,
new: newSessionState,
cookie,
})
})
.catch(monitorError)
}
}
}
}
async function getSessionCookies(): Promise<{ count: number; domain: string }> {
let sessionCookies: string[] | Awaited<ReturnType<CookieStore['getAll']>>
if ('cookieStore' in window) {
sessionCookies = await (window as { cookieStore: CookieStore }).cookieStore.getAll(SESSION_STORE_KEY)
} else {
sessionCookies = document.cookie.split(/\s*;\s*/).filter((cookie) => cookie.startsWith(SESSION_STORE_KEY))
}
return {
count: sessionCookies.length,
domain: getCurrentSite() || 'undefined',
...sessionCookies,
}
}