Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './globals.css';
import {Theme} from '@radix-ui/themes';
import type {Metadata} from 'next';
import {Rubik} from 'next/font/google';
import Script from 'next/script';
import PlausibleProvider from 'next-plausible';
Copy link
Member

Choose a reason for hiding this comment

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

What is the advantage of having this instead of the script?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

send events dynamically through a hook, nice TS and local DX experience


import {ThemeProvider} from 'sentry-docs/components/theme-provider';

Expand Down Expand Up @@ -31,6 +31,9 @@ export const metadata: Metadata = {
export default function RootLayout({children}: {children: React.ReactNode}) {
return (
<html lang="en" suppressHydrationWarning>
<head>
<PlausibleProvider domain="docs.sentry.io,rollup.sentry.io" />
Copy link
Member

Choose a reason for hiding this comment

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

do we need dev docs as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably, we should ask @lizokm

Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like we have Plausible installed on dev docs (if it is, it's not hooked up to the company account).

</head>
<body className={rubik.variable} suppressHydrationWarning>
<ThemeProvider
attribute="class"
Expand All @@ -43,12 +46,6 @@ export default function RootLayout({children}: {children: React.ReactNode}) {
</Theme>
</ThemeProvider>
</body>
<Script
defer
data-domain="docs.sentry.io,rollup.sentry.io"
data-api="https://plausible.io/api/event"
src="https://plausible.io/js/script.tagged-events.js"
/>
</html>
);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"micromark": "^4.0.0",
"next": "15.0.3",
"next-mdx-remote": "^4.4.1",
"next-plausible": "^3.12.4",
"next-themes": "^0.3.0",
"nextjs-toploader": "^1.6.6",
"parse-numeric-range": "^1.3.0",
Expand Down Expand Up @@ -137,4 +138,4 @@
"node": "20.11.0",
"yarn": "1.22.22"
}
}
}
2 changes: 2 additions & 0 deletions src/components/docPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {PaginationNav} from '../paginationNav';
import {PlatformSdkDetail} from '../platformSdkDetail';
import {Sidebar} from '../sidebar';
import {TableOfContents} from '../tableOfContents';
import {ReaderDepthTracker} from '../track-reader-depth';

type Props = {
children: ReactNode;
Expand Down Expand Up @@ -114,6 +115,7 @@ export function DocPage({
</main>
</section>
<Mermaid />
<ReaderDepthTracker />
</div>
);
}
65 changes: 65 additions & 0 deletions src/components/track-reader-depth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use client';
import {useEffect} from 'react';
import {usePlausible} from 'next-plausible';

import {debounce} from 'sentry-docs/utils';

const EVENT = 'Read Progress';
const milestones = [25, 50, 75, 100] as const;
type Milestone = (typeof milestones)[number];
type EVENT_PROPS = {page: string; readProgress: Milestone};

export function ReaderDepthTracker() {
const plausible = usePlausible<{[EVENT]: EVENT_PROPS}>();

const sendProgressToPlausible = (progress: Milestone) => {
// TODO: remove this after PR review
const args = [
EVENT,
{props: {readProgress: progress, page: document.title}},
] as const;
plausible(...args);
console.log('plausible event', ...args);

Check warning on line 22 in src/components/track-reader-depth.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want to remove this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, the last thing before merging

};

useEffect(() => {
const reachedMilestones = new Set<Milestone>();

const trackProgress = () => {
// calculate the progress based on the scroll position
const scrollPosition = window.scrollY;
const totalHeight = document.documentElement.scrollHeight - window.innerHeight;
let progress = Math.floor((scrollPosition / totalHeight) * 100);
// it's hard to trigger the 100% milestone, so we'll just assume beyond 95%
if (progress > 95) {
progress = 100;
}

// find the biggest milestone that has not been reached yet
const milestone = milestones.findLast(
m =>
progress >= m &&
!reachedMilestones.has(m) &&
// we shouldn't report smaller milestones once a bigger one has been reached
Array.from(reachedMilestones).every(r => m > r)
);
if (milestone) {
reachedMilestones.add(milestone);
sendProgressToPlausible(milestone);
}
};

// if the page is not scrollable, we don't need to track anything
if (document.documentElement.scrollHeight - window.innerHeight === 0) {
return () => {};
}
const debouncedTrackProgress = debounce(trackProgress, 20);
Copy link
Member

Choose a reason for hiding this comment

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

As discussed offline: this prevents events from being sent when a user very quickly scrolls to the bottom and then leaves the page - which might not count as a read anyway

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, it doesn't happen if you scroll at any reasonable reading speed

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you considered using the IntersectionObserver API for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the current approach is more precise

the marketing site / blog use the intersection observer and report 100% when the last heading is reached

a useful heuristic, but not very precise

Copy link
Contributor

Choose a reason for hiding this comment

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

The IntersectionObserver is more performant and since it's already being used by the marketing site, we should be keeping the measurement methods the same for better comparison.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

please keep in mind the measurement on the marketing sites is very rough (based on headers instead of the actual page scroll)

But I can do it @elijames-codecov

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

turns out I had bug in my debounce logic, now it works as intended

it's simple, precise and performant

maybe we should port it to the marketing site @elijames-codecov once we make sure it works well?

Copy link
Contributor

Choose a reason for hiding this comment

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

@a-hariti - Glad you got the debounce working! Since we're looking for insights as to which sections the users scroll into (to inform if there's seeing certain content and make content and some layout decisions), each section having a header, IntersectionObserver actually works well for our needs without added complexity and expensive operations...which is why we will continue to use it.


window.addEventListener('scroll', debouncedTrackProgress);
return () => {
window.removeEventListener('scroll', debouncedTrackProgress);
};
});
// do not render anything
return null;
}
24 changes: 24 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,27 @@ export const isLocalStorageAvailable = () => typeof localStorage !== 'undefined'
export const stripTrailingSlash = (url: string) => {
return url.replace(/\/$/, '');
};

/**
* Debounce function to limit the number of times a function is called.
* @param func The function to be debounced.
* @param wait The time to wait before calling the function.
* @param immediate Whether to call the function immediately.
* @returns A debounced function.
* @see https://davidwalsh.name/javascript-debounce-function
*/
export function debounce(func: Function, wait = 20, immediate = true) {
let timeout: ReturnType<typeof setTimeout> | null;
return function () {
const context = this;
const args = arguments;
const later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9897,6 +9897,11 @@ next-mdx-remote@^4.4.1:
vfile "^5.3.0"
vfile-matter "^3.0.1"

next-plausible@^3.12.4:
version "3.12.4"
resolved "https://registry.yarnpkg.com/next-plausible/-/next-plausible-3.12.4.tgz#d0ac1d7dcbe9836b6c93e37d42b80e3661fdaa34"
integrity sha512-cD3+ixJxf8yBYvsideTxqli3fvrB7R4BXcvsNJz8Sm2X1QN039WfiXjCyNWkub4h5++rRs6fHhchUMnOuJokcg==

next-themes@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.3.0.tgz#b4d2a866137a67d42564b07f3a3e720e2ff3871a"
Expand Down
Loading