Skip to content

Commit

Permalink
Update metric events to fire on player callback (#693)
Browse files Browse the repository at this point in the history
* Move player started/paused analytics events to player callback. Add error event

* Wrap sendEvent with try/catch

* Initialize GA only once

* Add more detail to event sending error

* Add events for when user starts and pauses player
  • Loading branch information
skanderm authored Nov 5, 2024
1 parent 1c1de4d commit 4ce27d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
32 changes: 21 additions & 11 deletions ui/src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,26 @@ export default function Player({
}
}, [playerRef, updateMediaSession]);

const handleReady = useCallback((player: VideoJSPlayer) => {
playerRef.current = player;
const handleReady = useCallback(
(player: VideoJSPlayer) => {
playerRef.current = player;

player.on("playing", () => {
setPlayerStatus("playing");
});
player.on("pause", () => setPlayerStatus("paused"));
player.on("waiting", () => setPlayerStatus("loading"));
player.on("error", () => setPlayerStatus("error"));
}, []);
player.on("playing", () => {
setPlayerStatus("playing");
currentFeed?.slug && analytics.stream.started(currentFeed.slug);
});
player.on("pause", () => {
setPlayerStatus("paused");
currentFeed?.slug && analytics.stream.paused(currentFeed.slug);
});
player.on("waiting", () => setPlayerStatus("loading"));
player.on("error", () => {
setPlayerStatus("error");
currentFeed?.slug && analytics.stream.error(currentFeed.slug);
});
},
[currentFeed?.slug],
);

const handlePlayPauseClick = async () => {
const player = playerRef.current;
Expand All @@ -146,10 +156,10 @@ export default function Player({
try {
if (playerStatus === "loading" || playerStatus === "playing") {
await player.pause();
currentFeed?.slug && analytics.stream.paused(currentFeed.slug);
currentFeed?.slug && analytics.stream.userPaused(currentFeed.slug);
} else {
await player.play();
currentFeed?.slug && analytics.stream.started(currentFeed.slug);
currentFeed?.slug && analytics.stream.userStarted(currentFeed.slug);
}
} catch (e) {
console.error(e);
Expand Down
35 changes: 32 additions & 3 deletions ui/src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ReactGA from "react-ga4";

export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID;
let analyticsInitialized = false;

const about = {
sampleAudioPlayed: (exampleTitle: string) =>
Expand Down Expand Up @@ -49,6 +50,24 @@ const stream = {
action: "Player paused",
label: feedSlug,
}),
userStarted: (feedSlug: string) =>
sendEvent({
category: "Stream",
action: "User started player",
label: feedSlug,
}),
userPaused: (feedSlug: string) =>
sendEvent({
category: "Stream",
action: "User paused player",
label: feedSlug,
}),
error: (feedSlug: string) =>
sendEvent({
category: "Stream",
action: "Player errored",
label: feedSlug,
}),
playerTextClicked: (playerText: string) => {
sendEvent({
category: "Stream",
Expand All @@ -59,9 +78,19 @@ const stream = {
};

function sendEvent(...eventParams: Parameters<typeof ReactGA.event>) {
if (GA_TRACKING_ID) {
ReactGA.initialize(GA_TRACKING_ID);
ReactGA.event(...eventParams);
try {
if (GA_TRACKING_ID) {
if (!analyticsInitialized) {
ReactGA.initialize(GA_TRACKING_ID);
analyticsInitialized = true;
}
ReactGA.event(...eventParams);
}
} catch (e) {
console.error("Failed to send analytics event:", {
error: e,
eventParams,
});
}
}

Expand Down

0 comments on commit 4ce27d8

Please sign in to comment.