Scroll Tracking State
function scroll_tracking(initState) {
var SCROLL_DEPTHS = [10, 25, 50, 75, 90];
var throttleTime = 200;
var hasReached = {};
for (var i = 0; i < SCROLL_DEPTHS.length; i++) {
hasReached[SCROLL_DEPTHS[i]] = false;
}
function calculateScrollDepth() {
var scrollTop = window.scrollY;
var docHeight = document.documentElement.scrollHeight - window.innerHeight;
return (scrollTop / docHeight) * 100;
}
function updateState(depth) {
initState({
"contextualStateInformation": {
"scrollDepth": depth
}
});
}
// Throttled scroll event listener
var lastTracked = 0;
window.addEventListener('scroll', function() {
var now = Date.now();
if (now - lastTracked > throttleTime) {
var scrollDepth = calculateScrollDepth();
for (var i = 0; i < SCROLL_DEPTHS.length; i++) {
var depth = SCROLL_DEPTHS[i];
if (scrollDepth >= depth && !hasReached[depth]) {
hasReached[depth] = true;
updateState(depth);
}
}
lastTracked = now;
}
}, {
passive: true
});
}Overview
Scroll Depth Trigger

Scroll percentage variable
Last updated
Was this helpful?