@@ -13,6 +13,7 @@ import type { CookieStore } from '../../browser/browser.types'
1313import { getCurrentSite } from '../../browser/cookie'
1414import { ExperimentalFeature , isExperimentalFeatureEnabled } from '../../tools/experimentalFeatures'
1515import { findLast } from '../../tools/utils/polyfills'
16+ import { monitorError } from '../../tools/monitor'
1617import { SESSION_NOT_TRACKED , SESSION_TIME_OUT_DELAY } from './sessionConstants'
1718import { startSessionStore } from './sessionStore'
1819import type { SessionState } from './sessionState'
@@ -173,24 +174,12 @@ function trackResume(configuration: Configuration, cb: () => void) {
173174
174175async function reportUnexpectedSessionState ( ) {
175176 const rawSession = retrieveSessionCookie ( )
176- let sessionCookies : string [ ] | Awaited < ReturnType < CookieStore [ 'getAll' ] > > = [ ]
177-
178- if ( 'cookieStore' in window ) {
179- sessionCookies = await ( window as { cookieStore : CookieStore } ) . cookieStore . getAll ( SESSION_STORE_KEY )
180- } else {
181- sessionCookies = document . cookie . split ( / \s * ; \s * / ) . filter ( ( cookie ) => cookie . startsWith ( SESSION_STORE_KEY ) )
182- }
183-
184177 addTelemetryDebug ( 'Unexpected session state' , {
185178 session : rawSession ,
186179 isSyntheticsTest : isSyntheticsTest ( ) ,
187180 createdTimestamp : rawSession ?. created ,
188181 expireTimestamp : rawSession ?. expire ,
189- cookie : {
190- count : sessionCookies . length ,
191- domain : getCurrentSite ( ) ,
192- ...sessionCookies ,
193- } ,
182+ cookie : await getSessionCookies ( ) ,
194183 currentDomain : `${ window . location . protocol } //${ window . location . hostname } ` ,
195184 } )
196185}
@@ -218,15 +207,36 @@ function detectSessionIdChange(configuration: Configuration, initialSessionState
218207 stop ( )
219208 } else {
220209 const newSessionState = toSessionState ( changed . value )
221- if ( newSessionState . id !== initialSessionState . id ) {
222- addTelemetryDebug ( 'Session cookie changed' , {
223- time : dateNow ( ) - sdkInitTime ,
224- session_age : sessionAge ,
225- old : initialSessionState ,
226- new : newSessionState ,
227- } )
210+ if ( newSessionState . id && newSessionState . id !== initialSessionState . id ) {
228211 stop ( )
212+ const time = dateNow ( ) - sdkInitTime
213+ getSessionCookies ( )
214+ . then ( ( cookie ) => {
215+ addTelemetryDebug ( 'Session cookie changed' , {
216+ time,
217+ session_age : sessionAge ,
218+ old : initialSessionState ,
219+ new : newSessionState ,
220+ cookie,
221+ } )
222+ } )
223+ . catch ( monitorError )
229224 }
230225 }
231226 }
232227}
228+
229+ async function getSessionCookies ( ) : Promise < { count : number ; domain : string } > {
230+ let sessionCookies : string [ ] | Awaited < ReturnType < CookieStore [ 'getAll' ] > >
231+ if ( 'cookieStore' in window ) {
232+ sessionCookies = await ( window as { cookieStore : CookieStore } ) . cookieStore . getAll ( SESSION_STORE_KEY )
233+ } else {
234+ sessionCookies = document . cookie . split ( / \s * ; \s * / ) . filter ( ( cookie ) => cookie . startsWith ( SESSION_STORE_KEY ) )
235+ }
236+
237+ return {
238+ count : sessionCookies . length ,
239+ domain : getCurrentSite ( ) ,
240+ ...sessionCookies ,
241+ }
242+ }
0 commit comments