refactor(analytics): method per event instead of track(name, props)

analytics.showViewed({...}) reads better than track('show_viewed', {...}) and
drops the track_ alias Captions needed, where vidstack already exports track.

The methods are derived from the event catalogue with a mapped type plus a
Proxy rather than written out, so the catalogue stays the only place an event
is declared and the two cannot drift. Names stay snake_case on the wire.
Emitters are cached so a method is referentially stable and safe to pass to a
hook or use as an effect dependency.

Events with no payload are typed void and take no arguments.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nerandClaude Opus 5 committed 2026-08-14 12:39:54 +03:00
1 parent d67a0d4df6
commit 01753d4eb6
16 files changed
+84 -40

No files matched your search

@@ -2,7 +2,7 @@ import { useMemo, useRef } from 'react';
import { CaptionOption, useCaptionOptions } from '@vidstack/react';
import { ClosedCaptionsIcon, ClosedCaptionsOnIcon } from '@vidstack/react/icons';
import { track as track_ } from '@/lib/analytics';
import analytics from '@/lib/analytics';
import { cn } from '@/lib/utils';
import PlayerMenu from '../Menu';
@@ -32,7 +32,7 @@ const CaptionsMenu = () => {
label: track.label,
value: track.value,
onSelect: (value) => {
track_('subtitles_selected', { language: value });
analytics.subtitlesSelected({ language: value });
return (live.current.find((option) => option.value === value) ?? track).select();
}
@@ -16,7 +16,7 @@ import { NextIcon } from '@vidstack/react/icons';
import { PlyrLayout, plyrLayoutIcons } from '@vidstack/react/player/layouts/plyr';
import { useSetCanPlay } from '@/hooks/useCanPlay';
import { track } from '@/lib/analytics';
import analytics from '@/lib/analytics';
import useTrackOnce from '@/lib/analytics/useTrackOnce';
import { cn } from '@/lib/utils';
@@ -89,7 +89,7 @@ const Player: FC<Props> = ({ magnet, playlist, subtitles, show, episodes, onStre
onStreamError?.(error);
}, [error, onStreamError]);
useTrackOnce('playback_failed', error && show ? `${magnet}:${error}` : undefined, () => ({
useTrackOnce(analytics.playbackFailed, error && show ? `${magnet}:${error}` : undefined, () => ({
showId: show!.id,
showType: show!.type,
reason: error!
@@ -126,7 +126,7 @@ const Player: FC<Props> = ({ magnet, playlist, subtitles, show, episodes, onStre
setCanPlay(true);
if (show && startedRef.current !== magnet) {
startedRef.current = magnet;
track('playback_started', { showId: show.id, showType: show.type });
analytics.playbackStarted({ showId: show.id, showType: show.type });
}
if (resumeTime.current) {
player.current!.currentTime = resumeTime.current;
@@ -8,7 +8,7 @@ import ButtonsGroup from '@/components/ui/buttons-group';
import { Quality } from '@/data/parsers/yts/models';
import { useM3UUrl } from '@/hooks/useM3UStreamUrl';
import { useStreamUrl } from '@/hooks/useStreamUrl';
import { track } from '@/lib/analytics';
import analytics from '@/lib/analytics';
import { cn, detectSafari, getMagnetHash, isStandaloneApp, seasonFromEpisodes, torrentKey } from '@/lib/utils';
import usePinnedTorrents from '../../../../usePinnedTorrents';
@@ -65,7 +65,7 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
}, [pinned, magnet, key, torrent.magnet]);
const pinPlayed = () => {
track('torrent_selected', {
analytics.torrentSelected({
provider,
seeders: torrent.seeders,
quality: torrent.quality,
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query';
import { Sort } from '@/data/parsers';
import analytics from '@/lib/analytics';
import useTrackOnce from '@/lib/analytics/useTrackOnce';
const useTorrents = ({
@@ -24,7 +25,7 @@ const useTorrents = ({
queryFn: () => queryFn({ query, sort, imdbID })
});
useTrackOnce('torrents_searched', result.data ? `${key}:${query}` : undefined, () => ({
useTrackOnce(analytics.torrentsSearched, result.data ? `${key}:${query}` : undefined, () => ({
showId: show.id,
showType: show.type,
provider: key,