diff --git a/app/(site)/(root)/useRandomMovie.ts b/app/(site)/(root)/useRandomMovie.ts index bf147a0..3ba097d 100644 --- a/app/(site)/(root)/useRandomMovie.ts +++ b/app/(site)/(root)/useRandomMovie.ts @@ -1,6 +1,6 @@ 'use client'; -import { startTransition, useCallback } from 'react'; +import { startTransition, useCallback, useRef } from 'react'; import { atom, getDefaultStore, useAtom } from 'jotai'; import randomQuery from '@/data/queries/random'; @@ -24,6 +24,10 @@ const useRandomMovie = ({ page }: Props) => { list: 'random' }); + const moviesRef = useRef(movies); + + moviesRef.current = movies; + if (movies.length && !movies[index]) { setIndex(0); } @@ -34,7 +38,7 @@ const useRandomMovie = ({ page }: Props) => { fetchNextPage, onIndexChange: useCallback( (index: number) => { - analytics.randomShuffled({ index }); + analytics.randomShuffled({ index, showTitle: moviesRef.current[index]?.title ?? '' }); startTransition(() => setIndex(index)); }, [setIndex] diff --git a/components/Analytics/index.tsx b/components/Analytics/index.tsx index 1115f3c..feca3bc 100644 --- a/components/Analytics/index.tsx +++ b/components/Analytics/index.tsx @@ -16,11 +16,6 @@ const OPTIONS = { trackOutgoingLinks: true }; -/** - * next/script emits a `rel=preload` even on `lazyOnload`, which pulls the sdk during the - * initial load. The inline stub below is a few hundred bytes and makes `window.op` queue - * calls immediately, so the real script can wait until the page is loaded and idle. - */ const Analytics = () => { useEffect(() => { if (!OPENPANEL_CLIENT_ID) return; diff --git a/components/Person/index.tsx b/components/Person/index.tsx index f4d8057..d142929 100644 --- a/components/Person/index.tsx +++ b/components/Person/index.tsx @@ -25,7 +25,10 @@ const lifespan = ({ birthday, deathday }: Profile) => { const Person: FC = ({ personId }) => { const { data: profile, isError } = useProfile({ personId }); - useTrackOnce(analytics.personViewed, profile ? personId : undefined, () => ({ personId })); + useTrackOnce(analytics.personViewed, profile ? personId : undefined, () => ({ + personId, + personName: profile!.name + })); if (isError) return ; diff --git a/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx b/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx index b6b5c31..c0cfc0c 100644 --- a/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx +++ b/components/Preview/components/Torrents/components/Player/components/Captions/index.tsx @@ -32,9 +32,11 @@ const CaptionsMenu = () => { label: track.label, value: track.value, onSelect: (value) => { - analytics.subtitlesSelected({ language: value }); + const option = live.current.find((option) => option.value === value) ?? track; - return (live.current.find((option) => option.value === value) ?? track).select(); + analytics.subtitlesSelected({ language: option.label }); + + return option.select(); } }))} > diff --git a/components/Preview/components/Torrents/components/Player/index.tsx b/components/Preview/components/Torrents/components/Player/index.tsx index d8fa22f..6d7cc48 100644 --- a/components/Preview/components/Torrents/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/Player/index.tsx @@ -92,6 +92,7 @@ const Player: FC = ({ magnet, playlist, subtitles, show, episodes, onStre useTrackOnce(analytics.playbackFailed, error && show ? `${magnet}:${error}` : undefined, () => ({ showId: show!.id, showType: show!.type, + showTitle: show!.title, reason: error! })); @@ -126,7 +127,7 @@ const Player: FC = ({ magnet, playlist, subtitles, show, episodes, onStre setCanPlay(true); if (show && startedRef.current !== magnet) { startedRef.current = magnet; - analytics.playbackStarted({ showId: show.id, showType: show.type }); + analytics.playbackStarted({ showId: show.id, showType: show.type, showTitle: show.title }); } if (resumeTime.current) { player.current!.currentTime = resumeTime.current; diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx index 35b3892..b2d1eac 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -66,7 +66,11 @@ const Actions: FC = ({ torrent, title, provider, show }) => { const pinPlayed = () => { analytics.torrentSelected({ + showId: show.id, + showType: show.type, + showTitle: show.title, provider, + torrentTitle: torrent.title, seeders: torrent.seeders, quality: torrent.quality, source: torrent.source, diff --git a/components/Preview/components/Torrents/index.tsx b/components/Preview/components/Torrents/index.tsx index a7b4ccc..3fa8833 100644 --- a/components/Preview/components/Torrents/index.tsx +++ b/components/Preview/components/Torrents/index.tsx @@ -52,7 +52,7 @@ const showTorrentsModal = create(({ onResolve, ...show }: Props) => { const isSeries = show.type === 'tv'; - const tracked = { id: show.id, type: show.type }; + const tracked = { id: show.id, type: show.type, title: show.title }; const yts = useTorrents({ query: isSeries ? '' : show.title, diff --git a/components/Preview/components/Torrents/useTorrents.ts b/components/Preview/components/Torrents/useTorrents.ts index 882d85b..21e64b0 100644 --- a/components/Preview/components/Torrents/useTorrents.ts +++ b/components/Preview/components/Torrents/useTorrents.ts @@ -16,7 +16,7 @@ const useTorrents = ({ query: string; sort: Sort; imdbID: string; - show: { id: Show['id']; type: Show['type'] }; + show: { id: Show['id']; type: Show['type']; title: Show['title'] }; queryFn: (params: { query: string; sort: Sort; imdbID: string }) => Promise; }) => { const result = useQuery({ @@ -28,6 +28,7 @@ const useTorrents = ({ useTrackOnce(analytics.torrentsSearched, result.data ? `${key}:${query}` : undefined, () => ({ showId: show.id, showType: show.type, + showTitle: show.title, provider: key, results: result.data?.length ?? 0 })); diff --git a/components/Preview/index.tsx b/components/Preview/index.tsx index 5058e6b..c08527b 100644 --- a/components/Preview/index.tsx +++ b/components/Preview/index.tsx @@ -47,7 +47,7 @@ const Preview: FC = ({ useTrackOnce(analytics.showViewed, show?.id, () => ({ showId: show!.id, showType: show!.type, - title: show!.title, + showTitle: show!.title, surface: isModal ? ('modal' as const) : ('page' as const) })); diff --git a/hooks/useShowState.ts b/hooks/useShowState.ts index 0399e06..9cef2cb 100644 --- a/hooks/useShowState.ts +++ b/hooks/useShowState.ts @@ -52,7 +52,7 @@ export const useMutateShowState = (show: Show) => { list: mutation.variables?.list, isPending: session ? mutation.isPending : false, mutate: ({ list, value }: { list: 'favorite' | 'watchlist'; value: boolean }) => { - analytics.libraryToggled({ showId: show.id, showType: show.type, list, value }); + analytics.libraryToggled({ showId: show.id, showType: show.type, showTitle: show.title, list, value }); if (session) return mutation.mutate({ showId: show.id, showType: show.type, list, value }); diff --git a/lib/analytics/events.ts b/lib/analytics/events.ts index ee7c823..8005c13 100644 --- a/lib/analytics/events.ts +++ b/lib/analytics/events.ts @@ -1,16 +1,12 @@ -type ShowRef = { showId: Show['id']; showType: Show['type'] }; +type ShowRef = { showId: Show['id']; showType: Show['type']; showTitle: string }; export type ListName = 'popular' | 'top' | 'search' | 'favorite' | 'watchlist' | 'random'; -/** - * The whole analytics surface of the app. Adding an event means adding a line here first, - * which keeps names and payloads consistent and makes `track` calls type checked. - */ export type AnalyticsEvents = { - show_viewed: ShowRef & { title: string; surface: 'page' | 'modal' }; - person_viewed: { personId: string }; + show_viewed: ShowRef & { surface: 'page' | 'modal' }; + person_viewed: { personId: string; personName: string }; list_loaded_more: { list: ListName; page: number }; - random_shuffled: { index: number }; + random_shuffled: { index: number; showTitle: string }; search_performed: { query: string; showType: Show['type']; results: number }; gallery_tab_changed: { tab: string }; @@ -20,8 +16,15 @@ export type AnalyticsEvents = { auth_signed_out: void; torrents_searched: ShowRef & { provider: string; results: number }; - torrent_selected: { provider: string; seeders: number; quality?: string; source?: string; codec?: string }; - playback_started: ShowRef & { quality?: string }; + torrent_selected: ShowRef & { + provider: string; + torrentTitle: string; + seeders: number; + quality?: string; + source?: string; + codec?: string; + }; + playback_started: ShowRef; playback_failed: ShowRef & { reason: string }; subtitles_selected: { language: string }; }; diff --git a/lib/analytics/index.ts b/lib/analytics/index.ts index 4fb512b..b377918 100644 --- a/lib/analytics/index.ts +++ b/lib/analytics/index.ts @@ -10,26 +10,16 @@ type CamelCase = Name extends `${infer Head}_${infer Tail}` type Emitter = [Properties] extends [void] ? () => void : (properties: Properties) => void; -/** - * One method per entry in the event catalogue, derived from it rather than written out, so the - * catalogue stays the single place an event is declared. `show_viewed` becomes - * `analytics.showViewed(...)` and keeps its snake_case name on the wire. - */ type Analytics = { [Name in keyof AnalyticsEvents as CamelCase]: Emitter; }; -// The sdk declares `window.op` itself, so this reads it without redeclaring the global. -// It is defined by the inline stub in components/Analytics and queues calls until the real -// script loads, so events fired before then are kept rather than dropped. const op = () => (window as unknown as { op?: (method: string, ...args: unknown[]) => void }).op; const ready = () => Boolean(OPENPANEL_CLIENT_ID) && typeof window !== 'undefined'; const eventName = (method: string) => method.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); -// Emitters are cached so `analytics.showViewed` is referentially stable and safe to pass to a -// hook or use as an effect dependency. const emitters = new Map void>(); const analytics = new Proxy({} as Analytics, { diff --git a/lib/analytics/useTrackOnce.ts b/lib/analytics/useTrackOnce.ts index a3718f1..4b30b0e 100644 --- a/lib/analytics/useTrackOnce.ts +++ b/lib/analytics/useTrackOnce.ts @@ -1,14 +1,7 @@ import { useEffect, useRef } from 'react'; -// Stands in for TypeScript 5.4's NoInfer. Without it the payload widens literal unions such as -// `'page' | 'modal'` to `string` instead of being checked against the emitter's type. type NoInfer = [Type][Type extends unknown ? 0 : never]; -/** - * Fires an emitter once per distinct `key`, skipping while it is nullish. Keeps view style - * events (a show opened, a search resolved) to a single line at the call site instead of an - * effect plus a ref in every component that needs one. - */ const useTrackOnce = ( emit: (properties: Properties) => void, key: string | number | null | undefined, @@ -24,7 +17,7 @@ const useTrackOnce = ( sentRef.current = key; - emit(payloadRef.current()); + emit(payloadRef.current() as Properties); }, [emit, key]); };