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:
1 parent
d67a0d4df6
commit
01753d4eb6
16 files changed
+84
-40
No files matched your search
@@ -2,6 +2,7 @@ import { useRef } from 'react';
|
||||
|
||||
import searchQuery, { SearchQueryProps } from '@/data/queries/search';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
import analytics from '@/lib/analytics';
|
||||
import useTrackOnce from '@/lib/analytics/useTrackOnce';
|
||||
|
||||
const useSearch = (props: SearchQueryProps) => {
|
||||
@@ -10,7 +11,7 @@ const useSearch = (props: SearchQueryProps) => {
|
||||
const infiniteQuery = useInfiniteList({ ...searchQuery(props), mode: 'default', list: 'search' });
|
||||
|
||||
useTrackOnce(
|
||||
'search_performed',
|
||||
analytics.searchPerformed,
|
||||
props.query && infiniteQuery.isFetched ? `${props.type}:${props.query}` : undefined,
|
||||
() => ({ query: props.query, showType: props.type, results: infiniteQuery.shows.length })
|
||||
);
|
||||
|
||||
@@ -5,7 +5,7 @@ import sessionMutation from '@/data/mutations/session';
|
||||
import tokenMutation from '@/data/mutations/token';
|
||||
import { useSetSession } from '@/hooks/useSession';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { track } from '@/lib/analytics';
|
||||
import analytics from '@/lib/analytics';
|
||||
|
||||
const AUTH_RETRIES = 10;
|
||||
|
||||
@@ -25,7 +25,7 @@ const useAuth = () => {
|
||||
const session = useMutation({
|
||||
...sessionMutation(),
|
||||
onSuccess({ session_id }) {
|
||||
track('auth_completed', {});
|
||||
analytics.authCompleted();
|
||||
setSession(session_id);
|
||||
clearInterval(intervalRef.current);
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useSetSession } from '@/hooks/useSession';
|
||||
import { clearIdentity, track } from '@/lib/analytics';
|
||||
import analytics, { clearIdentity } from '@/lib/analytics';
|
||||
|
||||
const useLogOut = () => {
|
||||
const setSession = useSetSession();
|
||||
|
||||
return useCallback(() => {
|
||||
track('auth_signed_out', {});
|
||||
analytics.authSignedOut();
|
||||
clearIdentity();
|
||||
setSession(undefined);
|
||||
}, [setSession]);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { FC } from 'react';
|
||||
|
||||
import List from '@/components/List';
|
||||
import NoResults from '@/components/NoResults';
|
||||
import analytics from '@/lib/analytics';
|
||||
import useTrackOnce from '@/lib/analytics/useTrackOnce';
|
||||
import { formatDate } from '@/lib/utils';
|
||||
|
||||
@@ -24,7 +25,7 @@ const lifespan = ({ birthday, deathday }: Profile) => {
|
||||
const Person: FC<Props> = ({ personId }) => {
|
||||
const { data: profile, isError } = useProfile({ personId });
|
||||
|
||||
useTrackOnce('person_viewed', profile ? personId : undefined, () => ({ personId }));
|
||||
useTrackOnce(analytics.personViewed, profile ? personId : undefined, () => ({ personId }));
|
||||
|
||||
if (isError) return <NoResults />;
|
||||
|
||||
|
||||
+2
-2
@@ -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;
|
||||
|
||||
+2
-2
@@ -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,
|
||||
|
||||
@@ -7,6 +7,7 @@ import Poster from '@/components/Poster';
|
||||
import Credits from '@/components/Preview/components/Credits';
|
||||
import useDetails from '@/components/Preview/useDetails';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import analytics from '@/lib/analytics';
|
||||
import useTrackOnce from '@/lib/analytics/useTrackOnce';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
@@ -43,11 +44,11 @@ const Preview: FC<Props> = ({
|
||||
|
||||
const isModal = Boolean(onClose);
|
||||
|
||||
useTrackOnce('show_viewed', show?.id, () => ({
|
||||
useTrackOnce(analytics.showViewed, show?.id, () => ({
|
||||
showId: show!.id,
|
||||
showType: show!.type,
|
||||
title: show!.title,
|
||||
surface: isModal ? 'modal' : 'page'
|
||||
surface: isModal ? ('modal' as const) : ('page' as const)
|
||||
}));
|
||||
|
||||
if (!show) return null;
|
||||
|
||||
@@ -4,7 +4,7 @@ import { FC, useState } from 'react';
|
||||
|
||||
import useSimilar from '@/components/Trending/useSimilar';
|
||||
import Nav from '@/components/ui/nav';
|
||||
import { track } from '@/lib/analytics';
|
||||
import analytics from '@/lib/analytics';
|
||||
|
||||
import Gallery from './components/Gallery';
|
||||
import { tabs as initialTabs } from './constants';
|
||||
@@ -46,7 +46,7 @@ const Trending: FC<Props> = ({ showId, showType, preview }) => {
|
||||
tabs={tabs}
|
||||
active={active}
|
||||
onChange={(active) => {
|
||||
track('gallery_tab_changed', { tab: active.type });
|
||||
analytics.galleryTabChanged({ tab: active.type });
|
||||
setActive(active);
|
||||
}}
|
||||
className='mb-3'
|
||||
|
||||
Reference in new issue
Block a user