Skip to content

fix(analytics-core): flush heartbeat events before awaiting delivery - #2199

Open
daniel-graham-amplitude wants to merge 1 commit into
video-analyticsfrom
codex/sdk-723-video-pagehide-delivery
Open

daniel-graham-amplitude wants to merge 1 commit into
video-analyticsfrom
codex/sdk-723-video-pagehide-delivery

Conversation

@daniel-graham-amplitude

@daniel-graham-amplitude daniel-graham-amplitude commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Start the heartbeat client flush immediately after queueing events. On ordinary pagehide, the video capture finalizes the active play as untracked. Awaiting delivery before starting the flush could leave that event in the destination queue when the browser discarded the document.

Preserve the existing back-forward-cache behavior. A persisted pagehide keeps the video observer and play session alive across restoration.

Related to SDK-723 and the corpsite reproduction.

Validation

  • Core and Browser SDK builds and 1,691 unit tests passed. The new regression makes the track promise resolve only once the client flushes.
  • A Chrome fixture verified one final untracked stop beacon on ordinary Back and continuity of the observer and play across cached Back/Forward.
  • TypeDoc and the unused-dependency check passed. The commit hook passed ESLint and Prettier.
  • pnpm test:examples is blocked on the base branch because jest.setup.examples.js is missing.

Checklist

  • PR title follows the conventional commit format.
  • Breaking change? No.

Note

Medium Risk
Changes page-unload send timing in a core analytics path; impact is scoped to ordering of flush vs. await but affects reliability-critical lifecycle behavior.

Overview
heartbeat() now triggers client.flush() as soon as events are queued for send, instead of waiting for every track promise to finish first.

When flushClient is true (e.g. non-persisted pagehide), delivery starts immediately while track promises can still be pending until the flush completes. That avoids losing final snapshots—such as video untracked stop beacons—when the browser tears down the page before the destination queue drains.

The unit test was tightened to assert track then flush ordering using a track promise that only resolves when flush runs.

Reviewed by Cursor Bugbot for commit db6ba32. Bugbot is set up for automated code reviews on this repo. Configure here.

@daniel-graham-amplitude
daniel-graham-amplitude requested a review from a team as a code owner November 24, 2026 03:28
@linear-code

linear-code Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

SDK-723

@github-actions

Copy link
Copy Markdown

size-limit report 📦

Path Size
packages/analytics-browser/lib/scripts/amplitude-min.js.gz 74.05 KB (-0.03% 🔽)
packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz 139.19 KB (0%)
packages/unified/lib/scripts/amplitude-min.umd.js.gz 229.27 KB (-0.01% 🔽)
@amplitude/element-selector (gzipped esm) 3.48 KB (0%)
@amplitude/analytics-browser/lite (gzipped CDN bundle) 22.06 KB (0%)

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