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,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 })
);
+2 -2
View File
@@ -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]);