Skip to content

Commit

Permalink
Add analytics for report actions (open, play)
Browse files Browse the repository at this point in the history
  • Loading branch information
skanderm committed Nov 20, 2023
1 parent 9ef2b38 commit 9c9748e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ui/src/components/DetectionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ import {
TableRow,
} from "@mui/material";

import { Detection, Feed } from "@/graphql/generated";
import { Candidate, Detection, Feed } from "@/graphql/generated";
import { analytics } from "@/utils/analytics";
import { formatTimestamp } from "@/utils/time";

import { DetectionsPlayer } from "./Player/DetectionsPlayer";

export default function DetectionsTable({
detections,
feed,
candidate
}: {
detections: Detection[];
feed: Pick<Feed, "slug" | "nodeName">;
candidate: Pick<Candidate, "id">;
}) {
const offsetPadding = 15;
const minOffset = Math.min(...detections.map((d) => +d.playerOffset));
const maxOffset = Math.max(...detections.map((d) => +d.playerOffset));
const startOffset = Math.max(0, minOffset - offsetPadding);
const endOffset = maxOffset + offsetPadding;

return (
<Box>
<DetectionsPlayer
Expand All @@ -37,6 +41,11 @@ export default function DetectionsTable({
timestamp={Math.min(...detections.map((d) => d.playlistTimestamp))}
startOffset={startOffset}
endOffset={endOffset}
onAudioPlay={() => {
if (candidate.id) {
analytics.reports.reportAudioPlayed(candidate.id);
}
}}
/>
<Table sx={{ marginTop: 6 }}>
<TableHead>
Expand Down
3 changes: 3 additions & 0 deletions ui/src/components/Player/DetectionsPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export function DetectionsPlayer({
timestamp,
startOffset,
endOffset,
onAudioPlay
}: {
feed: Pick<Feed, "nodeName">;
marks: { label: string; value: number }[];
timestamp: number;
startOffset: number;
endOffset: number;
onAudioPlay?: () => void;
}) {
const [playerStatus, setPlayerStatus] = useState<PlayerStatus>("idle");
const playerRef = useRef<VideoJSPlayer | null>(null);
Expand Down Expand Up @@ -113,6 +115,7 @@ export function DetectionsPlayer({
player.pause();
} else {
player.play();
onAudioPlay?.();
}
} catch (e) {
console.error(e);
Expand Down
6 changes: 6 additions & 0 deletions ui/src/pages/reports/[candidateId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import DetectionsTable from "@/components/DetectionsTable";
import Header from "@/components/Header";
import { useCandidateQuery } from "@/graphql/generated";
import type { NextPageWithLayout } from "@/pages/_app";
import { analytics } from "@/utils/analytics";

const CandidatePage: NextPageWithLayout = () => {
const router = useRouter();
Expand All @@ -23,6 +24,10 @@ const CandidatePage: NextPageWithLayout = () => {
});
const candidate = candidatesQuery.data?.candidate;

if (candidateId && typeof candidateId === "string") {
analytics.reports.reportOpened(candidateId);
}

return (
<div>
<Head>Report {candidateId} | Orcasound </Head>
Expand Down Expand Up @@ -62,6 +67,7 @@ const CandidatePage: NextPageWithLayout = () => {
<DetectionsTable
detections={candidate.detections}
feed={candidate.feed}
candidate={candidate}
/>
)}
</Box>
Expand Down
3 changes: 3 additions & 0 deletions ui/src/pages/reports/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import DetectionsTable from "@/components/DetectionsTable";
import Header from "@/components/Header";
import { CandidatesQuery, useCandidatesQuery } from "@/graphql/generated";
import type { NextPageWithLayout } from "@/pages/_app";
import { analytics } from "@/utils/analytics";
import { formatTimestamp } from "@/utils/time";

type CandidateQueryCandidates = NonNullable<CandidatesQuery["candidates"]>;
Expand Down Expand Up @@ -73,6 +74,7 @@ const DetectionsPage: NextPageWithLayout = () => {
if (candidate) {
setSelectedCandidate(candidate);
setDetectionModalOpen(true);
analytics.reports.reportOpened(candidateIdParam);
}
}
}, [candidates, candidateIdParam]);
Expand Down Expand Up @@ -122,6 +124,7 @@ const DetectionsPage: NextPageWithLayout = () => {
<DetectionsTable
detections={selectedCandidate.detections}
feed={selectedCandidate.feed}
candidate={selectedCandidate}
/>
)}
</Box>
Expand Down
16 changes: 16 additions & 0 deletions ui/src/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,26 @@ const form = {
sendEvent({ category: "Form", action: "Feedback button clicked" }),
};

const reports = {
reportOpened: (candidateId: string) =>
sendEvent({
category: "Reports",
action: "Report opened",
label: candidateId,
}),
reportAudioPlayed: (candidateId: string) =>
sendEvent({
category: "Reports",
action: "Report audio played",
label: candidateId,
}),
};

export const analytics = {
about,
detection,
stream,
nav,
form,
reports,
};

0 comments on commit 9c9748e

Please sign in to comment.