fix(plugin-autocapture-browser): flush Viewport Content Updated on pagehide/visibilitychange - #1934
Draft
jxiwang wants to merge 1 commit into
Draft
fix(plugin-autocapture-browser): flush Viewport Content Updated on pagehide/visibilitychange#1934jxiwang wants to merge 1 commit into
jxiwang wants to merge 1 commit into
Conversation
…gehide/visibilitychange
size-limit report 📦
|
Collaborator
Author
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 1aadef9. Configure here.
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.
Summary
[Amplitude] Viewport Content Updatedwas systematically under-delivered relative to[Amplitude] Page Viewed, especially for land-and-leave sessions. Two root causes:beforeunloadhandler, buttrack()only schedules a send on a timer (flushIntervalMillis, default 1s). The document is normally torn down before that timer runs, so the event was written to theAMP_unsent_*local storage queue and only replayed (with a stale timestamp) on the browser's next init — if the user ever returned. OnMemoryStorageor for bounced users it was lost entirely.beforeunloaddoes not fire on mobile app-switch, tab discard, or bfcache eviction, so those page views produced no exit event at all.This PR makes the exit path reliable without changing the event's semantics (still at most one page-end event per page view; mid-page 18k-char buffer flushes are unchanged):
pagehideoverbeforeunloadfor the page-end trigger (feature-detected via a newgetPageEndEventNamehelper; falls back tobeforeunloadwhenpagehideis unavailable).pagehidealso fires on mobile teardown and bfcache eviction and, unlikebeforeunload, doesn't make the page bfcache-ineligible. This mirrors the pattern already used bysession-replay-browser.visibilitychange→hiddencheckpoint. This is the last signal reliably delivered before a tab is backgrounded and possibly discarded. It flushes as a mid-page checkpoint (isPageEnd === false) so page-level scroll/exposure state is preserved if the user returns; a real page end shortly after is collapsed by the existing 100ms dedupe window.amplitude.flush()after firing, so the request starts (withkeepalive, already set by the fetch transport for small bodies) while the page is still alive, instead of waiting on the interval timer.pageViewEndFired, which could suppress a genuine page-end event (and its state reset) that followed within 100ms. Now onlyisPageEnd === truetriggers arm it.Tests updated to dispatch
pagehideand to cover the newvisibilitychange(hidden and visible) paths and the flush-on-exit behavior; a unit test coversgetPageEndEventNameacross all branches. Package tests pass at 100% coverage and lint is clean.Checklist