Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Access document more defensively (#5148)

Uku Tahtc85862e9eda0
diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js
index b6a87f1304..8ff3ed02ad 100644
--- a/tracker/src/plausible.js
+++ b/tracker/src/plausible.js
@@ -69,8 +69,10 @@
}
function getCurrentScrollDepthPx() {
- var viewportHeight = window.innerHeight || document.documentElement.clientHeight || 0
- var scrollTop = window.scrollY || document.documentElement.scrollTop || document.body.scrollTop || 0
+ var body = document.body || {}
+ var el = document.documentElement || {}
+ var viewportHeight = window.innerHeight || el.clientHeight || 0
+ var scrollTop = window.scrollY || el.scrollTop || body.scrollTop || 0
return currentDocumentHeight <= viewportHeight ? currentDocumentHeight : scrollTop + viewportHeight
}
diff --git a/tracker/test/fixtures/no-defer.html b/tracker/test/fixtures/no-defer.html
new file mode 100644
index 0000000000..d119866c09
--- /dev/null
+++ b/tracker/test/fixtures/no-defer.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
+ <title>Plausible Playwright tests</title>
+ <script src="/tracker/js/plausible.local.js"></script>
+</head>
+
+<body>
+ HELLO!
+</body>
+
+</html>
diff --git a/tracker/test/scroll-depth.spec.js b/tracker/test/scroll-depth.spec.js
index 36d46b4e69..3d94fabbba 100644
--- a/tracker/test/scroll-depth.spec.js
+++ b/tracker/test/scroll-depth.spec.js
@@ -116,7 +116,7 @@ test.describe('scroll depth (engagement events)', () => {
})
})
- test('re-sends engagement events only when user has scrolled in-between', async ({ page, context }) => {
+ test('re-sends engagement events only when user has scrolled in-between', async ({ page }) => {
await expectPlausibleInAction(page, {
action: async () => {
await page.goto('/scroll-depth.html')
@@ -140,4 +140,20 @@ test.describe('scroll depth (engagement events)', () => {
expectedRequests: [{n: 'engagement', u: `${LOCAL_SERVER_ADDR}/scroll-depth.html`, sd: 20}],
})
})
+
+
+ test('gets correct scroll depth when script has no defer', async ({ page }) => {
+ await expectPlausibleInAction(page, {
+ action: async () => {
+ await page.goto('/no-defer.html')
+ await hideAndShowCurrentTab(page)
+ },
+ expectedRequests: [
+ {n: 'pageview'},
+ {n: 'engagement', u: `${LOCAL_SERVER_ADDR}/no-defer.html`, sd: 100}
+ ],
+ })
+
+ await page.waitForTimeout(1000)
+ })
})