Skip to content
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

Merged
merged 2 commits into from
Jan 15, 2024
Merged

Conversation

robbie-c
Copy link
Member

@robbie-c robbie-c commented Jan 15, 2024

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 and html 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

@posthog-bot
Copy link
Collaborator

Hey @robbie-c! 👋
This pull request seems to contain no description. Please add useful context, rationale, and/or any other information that will help make sense of this change now and in the distant Mars-based future.

@benjackwhite benjackwhite changed the title Support custom scroll selector feat: Support custom scroll selector Jan 15, 2024
Copy link

github-actions bot commented Jan 15, 2024

Size Change: +2.11 kB (0%)

Total Size: 758 kB

Filename Size Change
dist/array.full.js 178 kB +527 B (0%)
dist/array.js 119 kB +529 B (0%)
dist/es.js 119 kB +529 B (0%)
dist/module.js 119 kB +529 B (0%)
ℹ️ View Unchanged
Filename Size
dist/exception-autocapture.js 12 kB
dist/recorder-v2.js 104 kB
dist/recorder.js 58.4 kB
dist/surveys.js 48.7 kB

compressed-size-action

@@ -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'],
Copy link
Member Author

@robbie-c robbie-c Jan 15, 2024

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 :)

@robbie-c robbie-c added the bump minor Bump minor version when this PR gets merged label Jan 15, 2024
@robbie-c robbie-c requested review from benjackwhite, pauldambra and Twixes and removed request for pauldambra January 15, 2024 14:09
Copy link
Collaborator

@benjackwhite benjackwhite left a 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[]
Copy link
Collaborator

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)

@robbie-c robbie-c merged commit ffc3d4a into master Jan 15, 2024
13 checks passed
@robbie-c robbie-c deleted the support-custom-scroll-selector branch January 15, 2024 14:53
Comment on lines +144 to 149
// 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)
Copy link
Member

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()
Copy link
Member

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 :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump minor Bump minor version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants