Skip to content

Commit f44ba4f

Browse files
authored
feat(platform): Add reusable hook for custom plausible events (#12380)
1 parent b679ee7 commit f44ba4f

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

src/components/track-reader-depth.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
'use client';
22
import {useEffect} from 'react';
3-
import {usePlausible} from 'next-plausible';
43

4+
import {usePlausibleEvent} from 'sentry-docs/hooks/usePlausibleEvent';
5+
import {PROGRESS_MILESTONES, ReadProgressMilestone} from 'sentry-docs/types/plausible';
56
import {debounce} from 'sentry-docs/utils';
67

7-
const EVENT = 'Read Progress';
8-
const milestones = [25, 50, 75, 100] as const;
9-
type Milestone = (typeof milestones)[number];
10-
type EVENT_PROPS = {page: string; readProgress: Milestone};
11-
128
export function ReaderDepthTracker() {
13-
const plausible = usePlausible<{[EVENT]: EVENT_PROPS}>();
9+
const {emit} = usePlausibleEvent();
1410

15-
const sendProgressToPlausible = (progress: Milestone) => {
16-
plausible(EVENT, {props: {readProgress: progress, page: document.title}});
11+
const sendProgressToPlausible = (progress: ReadProgressMilestone) => {
12+
emit('Read Progress', {props: {readProgress: progress, page: document.title}});
1713
};
1814

1915
useEffect(() => {
20-
const reachedMilestones = new Set<Milestone>();
16+
const reachedMilestones = new Set<ReadProgressMilestone>();
2117

2218
const trackProgress = () => {
2319
// calculate the progress based on the scroll position
@@ -30,7 +26,7 @@ export function ReaderDepthTracker() {
3026
}
3127

3228
// find the biggest milestone that has not been reached yet
33-
const milestone = milestones.findLast(
29+
const milestone = PROGRESS_MILESTONES.findLast(
3430
m =>
3531
progress >= m &&
3632
!reachedMilestones.has(m) &&

src/hooks/usePlausibleEvent.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {usePlausible} from 'next-plausible';
2+
3+
import {ReadProgressMilestone} from 'sentry-docs/types/plausible';
4+
5+
// Adding custom events here will make them available via the hook
6+
type PlausibleEventProps = {
7+
['Read Progress']: {
8+
page: string;
9+
readProgress: ReadProgressMilestone;
10+
};
11+
};
12+
13+
/**
14+
* A hook that provides type-safe access to Plausible Analytics events.
15+
*
16+
* @example
17+
* ```tsx
18+
* function MyComponent() {
19+
* const {emit} = usePlausibleEvent();
20+
*
21+
* return (
22+
* <button
23+
* onClick={() => {
24+
* emit('Some Typed Event', {
25+
* props: {
26+
* page: document.title,
27+
* }
28+
* });
29+
* }}
30+
* >
31+
* Trigger event
32+
* </button>
33+
* );
34+
* }
35+
* ```
36+
*/
37+
38+
export const usePlausibleEvent = () => {
39+
const plausible = usePlausible<PlausibleEventProps>();
40+
41+
return {
42+
emit: plausible,
43+
};
44+
};

src/types/plausible.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const PROGRESS_MILESTONES = [25, 50, 75, 100] as const;
2+
export type ReadProgressMilestone = (typeof PROGRESS_MILESTONES)[number];

0 commit comments

Comments
 (0)