Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions tracker/src/engagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ function getDocumentHeight() {
}

function getCurrentScrollDepthPx() {
var body = document.body || {}
var el = document.documentElement || {}
var viewportHeight = window.innerHeight || el.clientHeight || 0
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

By removing IE support for engagement tracking, these fallbacks are not necessary since window.scrollY and window.innerHeight are well-supported:

https://caniuse.com/mdn-api_window_innerheight
https://caniuse.com/mdn-api_window_scrolly

var scrollTop = window.scrollY || el.scrollTop || body.scrollTop || 0
var viewportHeight = window.innerHeight
var scrollTop = window.scrollY

return currentDocumentHeight <= viewportHeight
? currentDocumentHeight
Expand All @@ -151,23 +149,11 @@ export function init() {
currentDocumentHeight = getDocumentHeight()
maxScrollDepthPx = getCurrentScrollDepthPx()

window.addEventListener('load', function () {
new ResizeObserver(function () {
currentDocumentHeight = getDocumentHeight()

// Update the document height again after every 200ms during the
// next 3 seconds. This makes sure dynamically loaded content is
// also accounted for.
var count = 0
var interval = setInterval(function () {
Copy link
Copy Markdown
Contributor Author

@ukutaht ukutaht Mar 18, 2026

Choose a reason for hiding this comment

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

No need to poll the document for height changes during initial load. Any time DOM is loaded and updated, ResizeObserver will keep currentDocumentHeight variable in sync.

currentDocumentHeight = getDocumentHeight()
if (++count === 15) {
clearInterval(interval)
}
}, 200)
})
}).observe(document.documentElement)

document.addEventListener('scroll', function () {
currentDocumentHeight = getDocumentHeight()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No need to recompute document height on every scroll event. If the document height changes at any point, ResizeObserver will keep it updated.

var currentScrollDepthPx = getCurrentScrollDepthPx()

if (currentScrollDepthPx > maxScrollDepthPx) {
Expand Down
4 changes: 3 additions & 1 deletion tracker/src/plausible.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function init(overrides) {

initConfig(options)

initEngagementTracking()
if (!COMPILE_COMPAT) {
initEngagementTracking()
}

if (!COMPILE_MANUAL || (COMPILE_CONFIG && config.autoCapturePageviews)) {
initAutocapture(track)
Expand Down
Loading