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

Reader: fix scroll depth tracking #98093

Merged
merged 6 commits into from
Jan 10, 2025
Merged

Conversation

holdercp
Copy link
Contributor

@holdercp holdercp commented Jan 8, 2025

Fixes Automattic/loop#300

Proposed Changes

  • Fixes large values for scroll depth in future Tracks events, keeping them as a percentage between 0 and 100
  • Tracks scroll depth on mobile and desktop viewports
  • Tracks scroll depth on both stream and two-column views
  • Separates the scroll tracking logic into its own class that's composed into the React component. This removes the need to component state, reducing re-renders drastically.

Why are these changes being made?

  • Some of the values in Tracks were above 100, indicating there was a bug in the calculation.
  • The events weren't being sent to Tracks depending on how the component was rendered, specifically in a mobile viewport. Now the events are sent in every rendering of the full post component.
  • Separating out the scroll tracking from the component rendering meant we don't need to re-render the component whenever a scroll value changed, improving performance.

Testing Instructions

  • Go to /read and navigate between full post views
  • Make sure to test both the stream and two-column layout, as well as mobile and desktop
  • When you navigate away from the post, you should see the track event calypso_reader_article_scroll_depth logged
  • If the scroll depth doesn't go past 90% then a track event calypso_reader_article_exit_before_completion should also be logged.
  • You should be able to see these events on tracks after 5 mins
  • To see them live, I suggest adding console.log to the recordTrackForPost method.
  • Make sure all track values represent a percentage (0-100); they should be whole numbers.

Pre-merge Checklist

  • Has the general commit checklist been followed? (PCYsg-hS-p2)
  • Have you written new tests for your changes?
  • Have you tested the feature in Simple (P9HQHe-k8-p2), Atomic (P9HQHe-jW-p2), and self-hosted Jetpack sites (PCYsg-g6b-p2)?
  • Have you checked for TypeScript, React or other console errors?
  • Have you used memoizing on expensive computations? More info in Memoizing with create-selector and Using memoizing selectors and Our Approach to Data
  • Have we added the "[Status] String Freeze" label as soon as any new strings were ready for translation (p4TIVU-5Jq-p2)?
    • For UI changes, have we tested the change in various languages (for example, ES, PT, FR, or DE)? The length of text and words vary significantly between languages.
  • For changes affecting Jetpack: Have we added the "[Status] Needs Privacy Updates" label if this pull request changes what data or activity we track or use (p4TIVU-aUh-p2)?

@holdercp holdercp self-assigned this Jan 8, 2025
@matticbot
Copy link
Contributor

matticbot commented Jan 8, 2025

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

Async-loaded Components (~108 bytes added 📈 [gzipped])

name                                        parsed_size           gzip_size
async-load-calypso-blocks-reader-full-post       +364 B  (+0.9%)     +108 B  (+0.9%)

React components that are loaded lazily, when a certain part of UI is displayed for the first time.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@matticbot
Copy link
Contributor

matticbot commented Jan 8, 2025

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • notifications
  • wpcom-block-editor

To test WordPress.com changes, run install-plugin.sh $pluginSlug fix/reader-tracks-scroll-depth on your sandbox.

@holdercp holdercp marked this pull request as ready for review January 8, 2025 23:00
@holdercp holdercp requested a review from a team as a code owner January 8, 2025 23:00
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jan 8, 2025
Comment on lines -104 to -105
maxScrollDepth: 0, // Track the maximum scroll depth achieved
hasCompleted: false, // Track whether the user completed the post
Copy link
Contributor Author

@holdercp holdercp Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer need this component state because we just need to know the max scroll depth at the moment when we unmount or the post/feed changes. All of that tracking logic was moved to scroll-tracker.ts and just uses event listeners.

this.scrollableContainer.removeEventListener( 'scroll', this.setScrollDepth );
}
// Track scroll depth and remove related instruments
this.trackScrollDepth( this.props.post );
Copy link
Contributor Author

@holdercp holdercp Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to track the scroll depth when the component is about to unmount. We were missing events when the feed would change or a new page in the middle column was requested because the component actually unmounts/remounts in those cases.

@@ -18,6 +18,7 @@ body.is-reader-full-post {
);

height: $feed-content-height !important; // Using !important here to avoid having a lengthy selector.
overflow-y: hidden !important; // Keeps the scrolling container consistent between one and two column layouts.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These css changes fix an issue where the scroll container changed based on the viewport width, meaning we were no longer getting events. This ensures that the same DOM element is the scrolling container in the Recent feed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meaning we were no longer getting events

I understand the motivation, but looks like this had a side-effect. In the reader recent tab, if you scroll down while reading a longish post and click on another post, the scroll position doesn't reset to the top of the post.

We fixed it here should you want to see the before and after videos, but I suspect this change makes it come back since it doesn't happen on trunk.

Copy link
Contributor

@spsiddarthan spsiddarthan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to work well, I left a comment about the CSS changes.

Please let me know if it doesn't make sense.

@@ -18,6 +18,7 @@ body.is-reader-full-post {
);

height: $feed-content-height !important; // Using !important here to avoid having a lengthy selector.
overflow-y: hidden !important; // Keeps the scrolling container consistent between one and two column layouts.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meaning we were no longer getting events

I understand the motivation, but looks like this had a side-effect. In the reader recent tab, if you scroll down while reading a longish post and click on another post, the scroll position doesn't reset to the top of the post.

We fixed it here should you want to see the before and after videos, but I suspect this change makes it come back since it doesn't happen on trunk.

@holdercp
Copy link
Contributor Author

holdercp commented Jan 9, 2025

@spsiddarthan Thank you for that catch! I unintentionally removed that code. Should be fixed now.

Copy link
Contributor

@spsiddarthan spsiddarthan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well, thanks for your work on this!

@holdercp holdercp merged commit 64d3372 into trunk Jan 10, 2025
14 checks passed
@holdercp holdercp deleted the fix/reader-tracks-scroll-depth branch January 10, 2025 14:28
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants