Skip to content

Sdki 26/end video on pagehide - #2025

Open
daniel-graham-amplitude wants to merge 7 commits into
video-analyticsfrom
SDKI-26/end-video-on-pagehide
Open

daniel-graham-amplitude wants to merge 7 commits into
video-analyticsfrom
SDKI-26/end-video-on-pagehide

Conversation

@daniel-graham-amplitude

@daniel-graham-amplitude daniel-graham-amplitude commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  1. If "pagehide" is invoked (ie: a user exits a page permanently) attempt to capture and flush the final stop event with the stop_reason as "ended"
  2. If the visibility change happens (becomes hidden), then reset the heartbeat so that we get the stop value at the time of the visibility change. This is because a visiblity change sometimes predicts that a page is going to be exited so be sure to capture that value as it may be the final one.
  3. Invoke the "playhandler" if, when a video view is captured, the video is already playing. This is to handle the case where a video is playing before it's tracked.

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No

Note

Medium Risk
Changes delayed-event delivery and flush timing on tab/page lifecycle, which can affect analytics completeness and ordering but avoids false stops for bfcache (persisted) pages.

Overview
Improves video stream analytics when users leave a page or start tracking late in playback.

Page lifecycle: Heartbeat registers pagehide and visibilitychange listeners (browser-only via getGlobalScope). On a non–back/forward-cache pagehide, it re-sends queued delayed events and calls client.flush(); when the document becomes hidden, it resets the heartbeat so the latest delayed stop payload is sent sooner. VideoCapture also listens for pagehide and ends an in-progress session with stop_reason: 'ended', skipping teardown when event.persisted is true.

Already-playing media: HTML and embedded player tracking now synthesize a play when capture starts while playback is already active (embedded players use optional getPaused on ready).

API / behavior tweaks: VideoCapture.stop accepts an optional stop reason; heartbeat stop(flush) and public heartbeat(flushClient) support optional flush; heartbeat error handlers call stop() without binding so rejections do not pass stray arguments.

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

cely404 and others added 6 commits September 28, 2026 17:00
 - @amplitude/analytics-browser@2.47.1
 - @amplitude/analytics-core@2.58.1
 - @amplitude/analytics-node@1.5.75
 - @amplitude/analytics-react-native@1.10.2
 - @amplitude/element-selector@0.3.1
 - @amplitude/plugin-autocapture-browser@1.29.4
 - @amplitude/plugin-event-property-attribution-browser@0.2.18
 - @amplitude/plugin-experiment-browser@1.0.0-beta.45
 - @amplitude/plugin-experiment-react-native@1.0.0-beta.5
 - @amplitude/plugin-network-capture-browser@1.10.18
 - @amplitude/plugin-page-url-enrichment-browser@0.7.28
 - @amplitude/plugin-page-view-tracking-browser@2.11.18
 - @amplitude/plugin-session-replay-browser@1.35.4
 - @amplitude/plugin-session-replay-react-native@0.5.5
 - @amplitude/plugin-web-attribution-browser@2.2.28
 - @amplitude/plugin-web-vitals-browser@1.2.1
 - @amplitude/segment-session-replay-plugin@0.0.47
 - @amplitude/session-replay-browser@1.50.4
 - @amplitude/unified@1.1.37
 - @amplitude/unified-react-native@1.0.0-beta.4
…plugin-session-replay-react-native/example (#1897)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@linear-code

linear-code Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

SDKI-26

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/analytics-browser/src/video-capture/video-capture.ts Outdated
Comment thread packages/analytics-core/src/heartbeat.ts Outdated
Comment thread packages/analytics-browser/src/video-capture/video-capture.ts Outdated
Comment thread packages/analytics-browser/src/video-capture/video-capture.ts Outdated
Comment thread test-server/video-analytics/track-embedded-video.html Outdated
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Session Replay Browser E2E Results

passed  157 passed

Details

stats  157 tests across 18 suites
duration  4 minutes, 26 seconds
commit  699bc03

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

size-limit report 📦

Path Size
packages/analytics-browser/lib/scripts/amplitude-min.js.gz 69.44 KB (+0.43% 🔺)
packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz 135.9 KB (0%)
packages/unified/lib/scripts/amplitude-min.umd.js.gz 221.95 KB (0%)
@amplitude/element-selector (gzipped esm) 3.48 KB (0%)

@daniel-graham-amplitude
daniel-graham-amplitude force-pushed the SDKI-26/end-video-on-pagehide branch 4 times, most recently from 1ef63e5 to 699bc03 Compare October 1, 2026 23:56

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/analytics-browser/src/video-capture/video-capture.ts Outdated
Comment thread packages/session-replay-browser/package.json Outdated
@daniel-graham-amplitude
daniel-graham-amplitude force-pushed the SDKI-26/end-video-on-pagehide branch 2 times, most recently from f483515 to 33871de Compare October 2, 2026 00:16
@daniel-graham-amplitude

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-02T00:43:21.466146Z b912e0d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 33871de018

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/analytics-core/src/heartbeat.ts
Comment thread packages/analytics-browser/src/video-capture/video-capture.ts Outdated
Comment thread packages/analytics-browser/test/video-capture/video-capture.test.ts Outdated
Comment on lines +754 to +760
expect(trackNoDelay).toHaveBeenCalledWith(
expect.objectContaining({
event_type: '[Amplitude] Stream Stopped',
event_properties: expect.objectContaining({ stop_reason: 'ended' }),
}),
true,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Match the trackNoDelay call signature in the assertion

flushStopEvent invokes trackNoDelay(stopEvent) with one argument, but this assertion requires an additional true argument, so the new pagehide test fails even after the heartbeat mock is corrected. Assert the actual one-argument call, or change the production API consistently if the boolean is intended.

AGENTS.md reference: AGENTS.md:L19-L23

Useful? React with 👍 / 👎.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/analytics-core/src/heartbeat.ts
@daniel-graham-amplitude
daniel-graham-amplitude force-pushed the SDKI-26/end-video-on-pagehide branch 2 times, most recently from 8736baf to 82c34f2 Compare October 2, 2026 00:31

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/analytics-browser/src/video-capture/video-capture.ts Outdated
@daniel-graham-amplitude
daniel-graham-amplitude force-pushed the SDKI-26/end-video-on-pagehide branch from 82c34f2 to b912e0d Compare October 2, 2026 00:36
@daniel-graham-amplitude

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b912e0dabc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/analytics-browser/test/video-capture/video-capture.test.ts Outdated
@daniel-graham-amplitude
daniel-graham-amplitude force-pushed the SDKI-26/end-video-on-pagehide branch from b912e0d to 9321979 Compare October 2, 2026 03:09

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9321979. Configure here.

const onPageHide = (evt: PageTransitionEvent) => {
if (!evt.persisted) {
void this.heartbeat(true);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop reason lost on pagehide

Medium Severity

Heartbeat and VideoCapture each handle pagehide independently. heartbeat(true) snapshots the queue as soon as it runs, so a capture registered after the shared heartbeat already has listeners still has stop_reason timeout when that snapshot is sent.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9321979. Configure here.

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.

4 participants