-
Notifications
You must be signed in to change notification settings - Fork 131
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
feat: Support custom scroll selector #961
Conversation
Hey @robbie-c! 👋 |
Size Change: +2.11 kB (0%) Total Size: 758 kB
ℹ️ View Unchanged
|
@@ -15,6 +15,8 @@ if (typeof window !== 'undefined') { | |||
}, | |||
debug: true, | |||
__preview_send_client_session_params: true, | |||
__preview_measure_pageview_stats: true, | |||
scroll_root_selector: ['#scroll_element', 'html'], |
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.
Quick note that you can't use the css comma here, i.e. "#scroll_element,html"
as it would always return the html root element, even if #scroll_element
is present on the page :)
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.
Looks like a good solution.
The only thing I'm unsure about is the option naming but I don't have a better suggestion so 👍
@@ -128,6 +128,7 @@ export interface PostHogConfig { | |||
segment?: any | |||
__preview_measure_pageview_stats?: boolean | |||
__preview_send_client_session_params?: boolean | |||
scroll_root_selector?: string | string[] |
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.
Nit: could add a doc comment here so it is clear what it does (given the fact that documentation might come much later and this is an edge case option)
// setting the third argument to `true` means that we will receive scroll events for other scrollable elements | ||
// on the page, not just the window | ||
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture | ||
window?.addEventListener('scroll', this._updateScrollData, true) | ||
window?.addEventListener('scrollend', this._updateScrollData, true) | ||
window?.addEventListener('resize', this._updateScrollData) |
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.
trying to think defensively (so feel free to shoot this down) is it worth debouncing here?
the processing looks cheap but we'll get a lot of updates processed that we don't need 🤷
return window | ||
? Math.max(0, window.document.documentElement.scrollHeight - window.document.documentElement.clientHeight) | ||
: 0 | ||
const element = this._scrollElement() |
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.
similar nit on these helpers, we could lookup the element once and pass it into each of these helpers...
(may be so fast it doesn't matter :))
Changes
Scroll depth tracking can use a custom scroll element, e.g.
main
or#scroll_element
. You can also provide an array of selectors, so you can use#scroll_element
andhtml
as a fallback by setting it to['#scroll_element', 'html']
, which will be useful if different pages in the site have different scroll behaviours.Checklist