Repository navigation
feat(rum): Send a view update on every page exit - #5360
Draft
matthewnewman43 wants to merge 10 commits into
Draft
matthewnewman43 wants to merge 10 commits into
matthewnewman43 wants to merge 10 commits into
Conversation
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
Bundles Sizes Evolution
|
|
With the view_update_on_page_exit experimental feature, the view is updated in the urgent flush for every page exit reason (hidden, frozen, page terminated), not only before_unload. The last page state then reaches the intake before the page can be closed, discarded or killed without another chance to send.
The page exit listener fires before the page state listener, so the view update sent on hide or freeze ended with the previous state. With view_update_on_page_exit, the page state history now records hidden or frozen on the urgent flush first.
The urgent flush subscriber now also records terminated, so the view update sent on page termination no longer depends on capture listeners running first at the target. New tests check that a subscriber added later in the same flush, as trackViews is, sees the exit state as the last page state.
The page exit view update often left out the page state it was sent for. The exit state is recorded with performance.now(), but the view duration came from Date.now() in whole milliseconds, so the page states assemble hook (entry.startTime <= start + duration) could drop the newest entry. With view_update_on_page_exit, the view duration is now the longer of the system clock and monotonic clock durations, for active and ended views.
When a tab closes or reloads, Chrome fires visibilitychange after pagehide, so hidden was appended after terminated. When the page enters the back/forward cache, the same visibilitychange could append hidden after frozen, and the page then ended hidden instead of frozen. With view_update_on_page_exit, nothing is recorded after terminated, and a visibilitychange to hidden is not recorded after frozen. A resume or pageshow still records the state, so a page that runs again is tracked.
With view_update_on_page_exit, the last view update of a page that exits must end its page states with terminated, and nothing may follow terminated. Unit tests mock Date.now() and performance.now() from one source, so only an e2e test runs this with real clocks. Chromium only: Playwright turns off its back/forward cache there, so the flush navigation terminates the page.
When a tab closes or reloads, Chrome fires visibilitychange after pagehide. With view_update_on_page_exit, that sent a second view update, in its own beacon, after the terminated one. Since the page states keep terminated as the last state, that update added nothing. Once a view sent its terminated update, later page exits skip their update. A close or reload now adds one view update instead of two. The beforeunload update is unchanged.
The page state specs dispatched page events with the browser's own timeStamp, but read the page states from clock.relative(0). On Safari the real event time was earlier than clock.relative(0), so older entries ended before the view started and were left out. The events now carry a timeStamp from the mocked clock. They are dispatched on document, so the pageStateHistory capture listener on window still sees them, but other window listeners are not told that the page exits.
matthewnewman43
force-pushed
the
matthew.newman/view-update-on-page-exit
branch
from
December 3, 2026 21:34
3b2559a to
f9a7ce3
Compare
matthewnewman43
commented
Dec 3, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When a page goes away, the browser SDK records the new page state (
hidden,frozen) but sends a view update only onbefore_unload. A page that is then closed, discarded or killed ends with an older state, or with no update at all. The backend cannot tell how the page ended, and the hide can reach the intake after an operation summary is already written.This is the lightest version of two ideas: send the last page state before the tab closes (
page_terminated), and send a hide or freeze when it happens.Changes
view_update_on_page_exit.trackViews.ts, thePREPARE_URGENT_FLUSHsubscription callstriggerViewUpdate()for every page exit reason (visibility_hidden,page_frozen,page_terminated) when the feature is on, not only forbefore_unload. The update is added before the exit flush, so it is sent in the same beacon.pageStateHistory.ts, an urgent-flush subscriber recordshidden,frozenorterminatedfor the exit reason beforetrackViewsbuilds the update, because the DOM listener can run too late: the page exit observable registers itsvisibilitychangeandfreezelisteners first, and forpagehidethe order would rely on capture listeners running first at the target.hidden,frozenandterminatedare existing_dd.page_statesvalues.hiddenis often a short period in which the browser limits the page, which keeps sending keep-alive view updates. The update records when that period started, and it is the last update the SDK can count on if the page is later discarded or killed.Points to review
visibilitychangehidden,beforeunload,pagehidefire in quick order). This is why it is behind a flag; the plan is to measure view updates per view before and after.freezewhen a page enters the back/forward cache, so a cached page sends afrozenupdate. Safari fires nofreeze.is_activestaystrue.resume) and keep running; the update still saysfrozen, so a consumer must not readfrozenas final.Test instructions
pageStateHistory.spec.tschecks, for each exit reason, that a subscriber added later in the same urgent flush (astrackViewsis) assembles view page states that end with the exit state.Manual check with the sandbox (
yarn dev,http://localhost:8080), withview_update_on_page_exitadded toenableExperimentalFeaturesinsandbox/index.html:yarn dev-server intake clear.yarn dev-server intake rum-viewswhile still away: a new view update ends with page statehidden.terminated.Validation TODO
Sandbox (
yarn dev-server intake rum-views, feature on insandbox/index.html):hidden.terminated, and no update comes after it.frozen, and the restored page keeps sending updates.web-ui with the dev bundle (
?enable-rum=true, Browser SDK panel open, Redirect override):DD_RUM.versionisdev, andDD_RUM.getInitConfiguration().enableExperimentalFeaturescontainsview_update_on_page_exit.Staging (
@type:view version:dev-local @usr.id:<your user id>):@view.is_active:false: close the tab and wait more than 20 minutes. The@operation.failure_reason:abandonedstep comes about 20 minutes after the exit, not after an older keep-alive update.view_replaced). The abandoned step is unchanged with the feature on and off.Checklist