Skip to content

Commit d883d1d

Browse files
committed
Add COMPAT fallback for engagement tracking
1 parent 663bd23 commit d883d1d

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

tracker/src/engagement.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ function getDocumentHeight() {
137137
}
138138

139139
function getCurrentScrollDepthPx() {
140+
if (COMPILE_COMPAT) {
141+
var el = document.documentElement || {}
142+
var body = document.body || {}
143+
var viewportHeight = window.innerHeight || el.clientHeight || 0
144+
var scrollTop = window.scrollY || window.pageYOffset || el.scrollTop || body.scrollTop || 0
145+
return currentDocumentHeight <= viewportHeight
146+
? currentDocumentHeight
147+
: scrollTop + viewportHeight
148+
}
149+
140150
var viewportHeight = window.innerHeight
141151
var scrollTop = window.scrollY
142152

@@ -149,9 +159,20 @@ export function init() {
149159
currentDocumentHeight = getDocumentHeight()
150160
maxScrollDepthPx = getCurrentScrollDepthPx()
151161

152-
new ResizeObserver(function () {
153-
currentDocumentHeight = getDocumentHeight()
154-
}).observe(document.documentElement)
162+
if (COMPILE_COMPAT) {
163+
window.addEventListener('load', function () {
164+
currentDocumentHeight = getDocumentHeight()
165+
var count = 0
166+
var interval = setInterval(function () {
167+
currentDocumentHeight = getDocumentHeight()
168+
if (++count === 15) clearInterval(interval)
169+
}, 200)
170+
})
171+
} else {
172+
new ResizeObserver(function () {
173+
currentDocumentHeight = getDocumentHeight()
174+
}).observe(document.documentElement)
175+
}
155176

156177
document.addEventListener('scroll', function () {
157178
var currentScrollDepthPx = getCurrentScrollDepthPx()

tracker/src/plausible.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ function init(overrides) {
1616

1717
initConfig(options)
1818

19-
if (!COMPILE_COMPAT) {
20-
initEngagementTracking()
21-
}
19+
initEngagementTracking()
2220

2321
if (!COMPILE_MANUAL || (COMPILE_CONFIG && config.autoCapturePageviews)) {
2422
initAutocapture(track)

0 commit comments

Comments
 (0)