11import type { ContextValue } from '@datadog/browser-core'
22import {
3+ toServerDuration ,
34 DISCARDED ,
45 HookNames ,
56 SKIPPED ,
@@ -12,6 +13,7 @@ import {
1213} from '@datadog/browser-core'
1314import type { RumSessionManager } from '../rumSessionManager'
1415import { SessionReplayState , SessionType } from '../rumSessionManager'
16+ import type { PageStateServerEntry } from '../../rawRumEvent.types'
1517import { RumEventType } from '../../rawRumEvent.types'
1618import type { RecorderApi } from '../../boot/rumPublicApi'
1719import type { DefaultRumEventAttributes , DefaultTelemetryEventAttributes , Hooks } from '../hooks'
@@ -38,6 +40,7 @@ export function startSessionContext(
3840 const eventDuration = elapsed ( startTime , relativeNow ( ) )
3941 const wasInFrozenState = pageStateHistory . wasInPageStateDuringPeriod ( PageState . FROZEN , startTime , eventDuration )
4042 if ( isSessionExpired || ( wasInFrozenState && eventType !== RumEventType . VIEW ) ) {
43+ const pageStatesForPeriod = pageStateHistory . findPageStatesForPeriod ( startTime , eventDuration ) !
4144 // monitor-until: 2026-01-01
4245 addTelemetryDebug ( 'Event sent after session expiration or frozen page state' , {
4346 debug : {
@@ -49,14 +52,26 @@ export function startSessionContext(
4952 ? Number ( session . expire ) - relativeToClocks ( startTime ) . timeStamp
5053 : undefined ,
5154 wasInFrozenState,
52- pageStateDuringEventDuration : pageStateHistory . findPageStatesForPeriod (
53- startTime ,
54- eventDuration
55- ) as ContextValue ,
55+ pageStateDuringEventDuration : pageStatesForPeriod as ContextValue ,
56+ sumFrozenDuration : wasInFrozenState ? computeSumFrozenDuration ( pageStatesForPeriod ) : undefined ,
5657 } ,
5758 } )
5859 }
5960
61+ /**
62+ * Compute a frozen period duration by looking at the frozen entry start time and the next entry start time
63+ */
64+ function computeSumFrozenDuration ( pageStates : PageStateServerEntry [ ] ) {
65+ let sum = 0
66+ for ( let i = 0 ; i < pageStates . length ; i ++ ) {
67+ if ( pageStates [ i ] . state === PageState . FROZEN ) {
68+ const nextStateStart = pageStates [ i + 1 ] ? pageStates [ i + 1 ] . start : toServerDuration ( relativeNow ( ) )
69+ sum += nextStateStart - pageStates [ i ] . start
70+ }
71+ }
72+ return sum
73+ }
74+
6075 let hasReplay
6176 let sampledForReplay
6277 let isActive
0 commit comments