Skip to content

feat(rum): Send a view update on every page exit - #5360

Draft
matthewnewman43 wants to merge 10 commits into
mainfrom
matthew.newman/view-update-on-page-exit
Draft

matthewnewman43 wants to merge 10 commits into
mainfrom
matthew.newman/view-update-on-page-exit

Conversation

@matthewnewman43

@matthewnewman43 matthewnewman43 commented Nov 30, 2026 •

Copy link
Copy Markdown

Motivation

When a page goes away, the browser SDK records the new page state (hidden, frozen) but sends a view update only on before_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

  • Adds the experimental feature view_update_on_page_exit.
  • In trackViews.ts, the PREPARE_URGENT_FLUSH subscription calls triggerViewUpdate() for every page exit reason (visibility_hidden, page_frozen, page_terminated) when the feature is on, not only for before_unload. The update is added before the exit flush, so it is sent in the same beacon.
  • In pageStateHistory.ts, an urgent-flush subscriber records hidden, frozen or terminated for the exit reason before trackViews builds the update, because the DOM listener can run too late: the page exit observable registers its visibilitychange and freeze listeners first, and for pagehide the order would rely on capture listeners running first at the target.
  • No schema change: hidden, frozen and terminated are existing _dd.page_states values.
  • The update on hide does not mean the page left. hidden is 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.
  • With the feature off, behaviour is unchanged.

Points to review

  1. Cost: one extra view update per hide, and up to three on a desktop close (visibilitychange hidden, beforeunload, pagehide fire in quick order). This is why it is behind a flag; the plan is to measure view updates per view before and after.
  2. Chrome also fires freeze when a page enters the back/forward cache, so a cached page sends a frozen update. Safari fires no freeze.
  3. The view is updated, not ended: is_active stays true.
  4. A frozen page can be resumed (resume) and keep running; the update still says frozen, so a consumer must not read frozen as final.
  5. Each exit flushes on its own, so a desktop close sends up to three requests, not one. Skipping an update when the page state has not changed since the last one sent would cut this; not done yet.

Test instructions

yarn test:unit packages/browser-rum-core/src/domain/view/trackViews.spec.ts packages/browser-rum-core/src/domain/contexts/pageStateHistory.spec.ts

pageStateHistory.spec.ts checks, for each exit reason, that a subscriber added later in the same urgent flush (as trackViews is) assembles view page states that end with the exit state.

Manual check with the sandbox (yarn dev, http://localhost:8080), with view_update_on_page_exit added to enableExperimentalFeatures in sandbox/index.html:

  1. yarn dev-server intake clear.
  2. Switch to another tab, then run yarn dev-server intake rum-views while still away: a new view update ends with page state hidden.
  3. Close the tab: the last view update ends with terminated.
  4. Remove the feature and repeat step 2: no update at the hide.

Validation TODO

Sandbox (yarn dev-server intake rum-views, feature on in sandbox/index.html):

  • Hide the tab: a new view update ends with page state hidden.
  • Close the tab: the last view update ends with terminated, and no update comes after it.
  • Leave for another site, then press Back (Chrome): a view update ends with frozen, and the restored page keeps sending updates.
  • Feature off: no view update at the hide.

web-ui with the dev bundle (?enable-rum=true, Browser SDK panel open, Redirect override):

  • DD_RUM.version is dev, and DD_RUM.getInitConfiguration().enableExperimentalFeatures contains view_update_on_page_exit.

Staging (@type:view version:dev-local @usr.id:<your user id>):

  • For each exit (hide, reload, close, leave and Back), the last stored view event ends with the expected page state. Open the event JSON to read the page states.
  • Compare view updates per view with the feature on and off (point to review 1).
  • No-code operation with an abandonment step on @view.is_active:false: close the tab and wait more than 20 minutes. The @operation.failure_reason:abandoned step comes about 20 minutes after the exit, not after an older keep-alive update.
  • Same operation: leave the tab hidden for more than 15 minutes until the session expires. The abandoned step time follows the hide.
  • Control: navigate to another view (view_replaced). The abandoned step is unchanged with the feature on and off.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@github-actions

Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


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.

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 195.92 KiB 196.68 KiB +784 B +0.39% ✅
Rum 195.89 KiB 196.66 KiB +784 B +0.39% ✅
Rum Salesforce Profiler 7.81 KiB 7.81 KiB 0 B 0.00% ✅
Rum Profiler 7.81 KiB 7.81 KiB 0 B 0.00% ✅
Rum Salesforce Recorder 32.80 KiB 32.80 KiB 0 B 0.00% ✅
Rum Recorder 32.80 KiB 32.80 KiB 0 B 0.00% ✅
Logs 60.43 KiB 60.48 KiB +54 B +0.09% ✅
Rum Slim 151.28 KiB 151.99 KiB +727 B +0.47% ✅
Worker 24.23 KiB 24.23 KiB 0 B 0.00% ✅
Rum Shopify 227.44 KiB 228.20 KiB +784 B +0.34% ✅
Rum Shopify Profiler 7.81 KiB 7.81 KiB 0 B 0.00% ✅
Rum Shopify Recorder 4.85 KiB 4.85 KiB 0 B 0.00% ✅
Early Collection 309 B 309 B 0 B 0.00% ✅

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Tests  Pipelines

⚠️ Warnings

Your PR has warnings. Please review the issues below.

❄️ 2 New flaky tests detected

pageStateHistory > page exit > page states after the exit > should record hidden after terminated by default from packages/browser-rum-core/src/domain/contexts/pageStateHistory.spec.ts
The string did not match the expected pattern.

SyntaxError: The string did not match the expected pattern.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 12, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2, HIERARCHY_REQUEST_ERR: 3, WRONG_DOCUMENT_ERR: 4, INVALID_CHARACTER_ERR: 5, NO_DATA_ALLOWED_ERR: 6, NO_MODIFICATION_ALLOWED_ERR: 7, NOT_FOUND_ERR: 8, NOT_SUPPORTED_ERR: 9, INUSE_ATTRIBUTE_ERR: 10, INVALID_STATE_ERR: 11, SYNTAX_ERR: 12, INVALID_MODIFICATION_ERR: 13, NAMESPACE_ERR: 14, INVALID_ACCESS_ERR: 15, VALIDATION_ERR: 16, TYPE_MISMATCH_ERR: 17, SECURITY_ERR: 18, NETWORK_ERR: 19, ABORT_ERR: 20, URL_MISMATCH_ERR: 21, QUOTA_EXCEEDED_ERR: 22, TIMEOUT_ERR: 23, INVALID_NODE_TYPE_ERR: 24, DATA_CLONE_ERR: 25 }
pageStateHistory > page exit > page states after the exit > with view_update_on_page_exit > should record hidden when a frozen page resumes while hidden from packages/browser-rum-core/src/domain/contexts/pageStateHistory.spec.ts
The string did not match the expected pattern.

SyntaxError: The string did not match the expected pattern.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 12, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2, HIERARCHY_REQUEST_ERR: 3, WRONG_DOCUMENT_ERR: 4, INVALID_CHARACTER_ERR: 5, NO_DATA_ALLOWED_ERR: 6, NO_MODIFICATION_ALLOWED_ERR: 7, NOT_FOUND_ERR: 8, NOT_SUPPORTED_ERR: 9, INUSE_ATTRIBUTE_ERR: 10, INVALID_STATE_ERR: 11, SYNTAX_ERR: 12, INVALID_MODIFICATION_ERR: 13, NAMESPACE_ERR: 14, INVALID_ACCESS_ERR: 15, VALIDATION_ERR: 16, TYPE_MISMATCH_ERR: 17, SECURITY_ERR: 18, NETWORK_ERR: 19, ABORT_ERR: 20, URL_MISMATCH_ERR: 21, QUOTA_EXCEEDED_ERR: 22, TIMEOUT_ERR: 23, INVALID_NODE_TYPE_ERR: 24, DATA_CLONE_ERR: 25 }

View in Flaky Test Management

ℹ️ Info

No other issues found (see more)

🧪 All tests passed

🎯 Code Coverage (details)
• Patch Coverage: 72.22%
• Overall Coverage: 78.12% (+0.01%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 5142d63 | Docs | View more details | Give us feedback!

@matthewnewman43 matthewnewman43 changed the title ⚗️ Send a view update on every page exit feat(rum): Send a view update on every page exit Nov 30, 2026
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
matthewnewman43 force-pushed the matthew.newman/view-update-on-page-exit branch from 3b2559a to f9a7ce3 Compare December 3, 2026 21:34

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant