-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Tracker] Use ResizeObserver instead of polling for document height #6174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8758ce1
fe96564
37381e0
1824094
0e24d26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "tracker_script_version": 33, | ||
| "tracker_script_version": 34, | ||
| "type": "module", | ||
| "scripts": { | ||
| "lint": "eslint", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,10 +137,8 @@ function getDocumentHeight() { | |
| } | ||
|
|
||
| function getCurrentScrollDepthPx() { | ||
| var body = document.body || {} | ||
| var el = document.documentElement || {} | ||
| var viewportHeight = window.innerHeight || el.clientHeight || 0 | ||
| var scrollTop = window.scrollY || el.scrollTop || body.scrollTop || 0 | ||
| var viewportHeight = window.innerHeight | ||
| var scrollTop = window.scrollY | ||
|
|
||
| return currentDocumentHeight <= viewportHeight | ||
| ? currentDocumentHeight | ||
|
|
@@ -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 () { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
| currentDocumentHeight = getDocumentHeight() | ||
| if (++count === 15) { | ||
| clearInterval(interval) | ||
| } | ||
| }, 200) | ||
| }) | ||
| }).observe(document.documentElement) | ||
|
|
||
| document.addEventListener('scroll', function () { | ||
| currentDocumentHeight = getDocumentHeight() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
| var currentScrollDepthPx = getCurrentScrollDepthPx() | ||
|
|
||
| if (currentScrollDepthPx > maxScrollDepthPx) { | ||
|
|
||
There was a problem hiding this comment.
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.scrollYandwindow.innerHeightare well-supported:https://caniuse.com/mdn-api_window_innerheight
https://caniuse.com/mdn-api_window_scrolly