Skip to content

fix(autocapture): fire exposure at 50% visibility instead of 100% - #1972

Open
jxiwang wants to merge 2 commits into
mainfrom
cursor/exposure-fire-at-50-percent-visibility-90f3
Open

fix(autocapture): fire exposure at 50% visibility instead of 100%#1972
jxiwang wants to merge 2 commits into
mainfrom
cursor/exposure-fire-at-50-percent-visibility-90f3

Conversation

@jxiwang

@jxiwang jxiwang commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Exposure — the signal behind [Amplitude] Viewport Content Updated — now treats an element as seen once half of it is inside the viewport, instead of requiring the whole element.

  • createExposureObservable observed with threshold: 1.0; it now uses a new shared constant EXPOSURE_INTERSECTION_THRESHOLD (0.5).
  • trackExposure started its exposure timer on entry.isIntersecting alone. It now compares entry.intersectionRatio against that same constant, so the 50% rule lives in the code rather than only in the observer options. The spec defines isIntersecting as any overlap with the root, independent of the configured threshold; Chromium and Firefox both tie it to the threshold instead, but a spec-literal implementation (or a polyfill) would expose an element from a single visible pixel. The explicit ratio check makes the behavior the same either way.
  • The exit branch (!isIntersecting && intersectionRatio < 1.0) collapses to a plain else: anything below the threshold cancels a pending timer, whether the element left the viewport or just scrolled partway out.

With threshold: 0.5 the observer still delivers a callback on viewport entry/exit and on each crossing of 50%, which is all the ratio-based gate needs — no extra thresholds required. Ratios are compared with a small tolerance so sub-pixel rounding cannot strand an element parked exactly at the boundary.

Verification

New unit tests cover exposure at exactly 50%, no exposure at 30%, and cancellation when an element scrolls from 60% down to 40% while still touching the viewport. Package suite: 517 tests passing at 100% coverage.

I also probed a real IntersectionObserver in Chromium and Firefox (both report intersectionRatio 0.505 / isIntersecting: true when half visible, and no threshold-met callback at 40%), then ran the built analytics-browser UMD bundle end to end in Chromium against a button positioned below the fold:

element visibility main this branch
30% not exposed not exposed
52% not exposed button#target exposed

Note for reviewers

An element more than twice the viewport size can never reach 50% of its own area, so it is never exposed. That matches the current behavior — it could never reach 100% either — but it is worth confirming that no customer relies on exposure for full-page containers via [data-amp-default-track] or a custom cssSelectorAllowlist. I prototyped a fallback that exposed such elements once their visible part filled half the viewport, then dropped it: because both engines suppress isIntersecting below the threshold, it would have fired only on the rare rescan that happened to coincide with the element filling the viewport, making behavior inconsistent across browsers and hard to reason about.

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No, but it is an intentional behavior change — elements now need 50% visibility rather than 100%, so Viewport Content Updated will report more elements than before.
Open in Web Open in Cursor 

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

size-limit report 📦

Path Size
packages/analytics-browser/lib/scripts/amplitude-min.js.gz 64.32 KB (+0.02% 🔺)
packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz 135.32 KB (0%)
packages/unified/lib/scripts/amplitude-min.umd.js.gz 218.72 KB (+0.01% 🔺)
@amplitude/element-selector (gzipped esm) 3.4 KB (0%)

@jxiwang
jxiwang marked this pull request as ready for review September 4, 2026 15:44
@jxiwang
jxiwang requested a review from a team as a code owner September 4, 2026 15:44
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.

2 participants