Add scroll heatmaps.

This commit is contained in:
Mike Cao committed 2026-05-05 10:43:39 -07:00
1 parent c7f71e5758
commit 1b69ce6e90
13 files changed
+461 -55

No files matched your search

+69 -2
View File
@@ -149,11 +149,78 @@ import { record } from 'rrweb';
...(blockSelector && { blockSelector }),
});
let scrollUrl = location.href;
let maxScrollPct = 0;
let scrollTimer = null;
const computeScrollPct = () => {
const pageH = document.documentElement.scrollHeight;
const visible = window.scrollY + window.innerHeight;
return {
pct: Math.max(0, Math.min(100, Math.round((visible / Math.max(1, pageH)) * 100))),
pageH,
};
};
const flushScroll = () => {
if (maxScrollPct <= 0) return;
record.addCustomEvent('scroll-progress', {
url: scrollUrl,
scrollPct: maxScrollPct,
viewportW: window.innerWidth,
viewportH: window.innerHeight,
pageH: document.documentElement.scrollHeight,
});
maxScrollPct = 0;
};
const onScroll = () => {
if (scrollTimer) return;
scrollTimer = setTimeout(() => {
const { pct } = computeScrollPct();
if (pct > maxScrollPct) maxScrollPct = pct;
scrollTimer = null;
}, 200);
};
const onUrlChange = () => {
if (location.href === scrollUrl) return;
flushScroll();
scrollUrl = location.href;
record.addCustomEvent('url-change', { url: scrollUrl });
};
const hookHistory = method => {
const orig = history[method];
history[method] = function (...args) {
const result = orig.apply(this, args);
onUrlChange();
return result;
};
};
hookHistory('pushState');
hookHistory('replaceState');
window.addEventListener('popstate', onUrlChange);
window.addEventListener('scroll', onScroll, { passive: true });
// Capture initial scroll position
{
const { pct } = computeScrollPct();
if (pct > maxScrollPct) maxScrollPct = pct;
}
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') flush(true);
if (document.visibilityState === 'hidden') {
flushScroll();
flush(true);
}
});
window.addEventListener('beforeunload', () => flush(true));
window.addEventListener('beforeunload', () => {
flushScroll();
flush(true);
});
};
if (document.readyState === 'complete') {