From fe822e0a4568b04ae85c7d4c756939fb8c117651 Mon Sep 17 00:00:00 2001 From: Najib Boutaib <104867447+N-Boutaib@users.noreply.github.com> Date: Thu, 5 Sep 2024 11:26:42 +0200 Subject: [PATCH] :sparkles: Discard loading time when page is hidden (#2965) --- .../domain/view/viewMetrics/trackLoadingTime.spec.ts | 10 +++++++++- .../src/domain/view/viewMetrics/trackLoadingTime.ts | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.spec.ts b/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.spec.ts index 7baf8f22b8..0c71a38f50 100644 --- a/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.spec.ts +++ b/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.spec.ts @@ -1,7 +1,7 @@ import type { RelativeTime, Duration } from '@datadog/browser-core' import { addDuration, clocksOrigin, Observable } from '@datadog/browser-core' import type { Clock } from '@datadog/browser-core/test' -import { mockClock } from '@datadog/browser-core/test' +import { mockClock, setPageVisibility, restorePageVisibility } from '@datadog/browser-core/test' import { ViewLoadingType } from '../../../rawRumEvent.types' import { createPerformanceEntry } from '../../../../test' import { PAGE_ACTIVITY_END_DELAY, PAGE_ACTIVITY_VALIDATION_DELAY } from '../../waitPageActivityEnd' @@ -47,6 +47,7 @@ describe('trackLoadingTime', () => { afterEach(() => { stopLoadingTimeTracking() + restorePageVisibility() clock.cleanup() }) @@ -125,4 +126,11 @@ describe('trackLoadingTime', () => { expect(loadingTimeCallback).toHaveBeenCalledOnceWith(addDuration(BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY, CLOCK_GAP)) }) + + it('should discard loading time if page is hidden before activity', () => { + setPageVisibility('hidden') + startLoadingTimeTracking() + + expect(loadingTimeCallback).not.toHaveBeenCalled() + }) }) diff --git a/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.ts b/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.ts index d34ed787eb..60de33a4d8 100644 --- a/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.ts +++ b/packages/rum-core/src/domain/view/viewMetrics/trackLoadingTime.ts @@ -4,6 +4,7 @@ import { waitPageActivityEnd } from '../../waitPageActivityEnd' import type { RumConfiguration } from '../../configuration' import type { LifeCycle } from '../../lifeCycle' import { ViewLoadingType } from '../../../rawRumEvent.types' +import { trackFirstHidden } from './trackFirstHidden' export function trackLoadingTime( lifeCycle: LifeCycle, @@ -16,10 +17,14 @@ export function trackLoadingTime( let isWaitingForLoadEvent = loadType === ViewLoadingType.INITIAL_LOAD let isWaitingForActivityLoadingTime = true const loadingTimeCandidates: Duration[] = [] + const firstHidden = trackFirstHidden(configuration) function invokeCallbackIfAllCandidatesAreReceived() { if (!isWaitingForActivityLoadingTime && !isWaitingForLoadEvent && loadingTimeCandidates.length > 0) { - callback(Math.max(...loadingTimeCandidates) as Duration) + const loadingTime = Math.max(...loadingTimeCandidates) + if (loadingTime < firstHidden.timeStamp) { + callback(loadingTime as Duration) + } } } @@ -34,7 +39,10 @@ export function trackLoadingTime( }) return { - stop, + stop: () => { + stop() + firstHidden.stop() + }, setLoadEvent: (loadEvent: Duration) => { if (isWaitingForLoadEvent) { isWaitingForLoadEvent = false