refactor(analytics): send titles alongside ids, drop comments

Bare ids are unreadable in the dashboard, so every event that points at a
show or a person now carries its title or name. Ids stay for disambiguation,
since titles collide across remakes.

- ShowRef gains showTitle, so show_viewed, library_toggled, torrents_searched,
  torrent_selected, playback_started and playback_failed all report it
- person_viewed sends personName
- random_shuffled sends the title of the movie scrolled to
- torrent_selected sends the torrent title next to the show title
- subtitles_selected sends the track label, such as English, rather than the
  internal track value

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nerandClaude Opus 5 committed 2026-08-14 12:44:38 +03:00
1 parent 01753d4eb6
commit 66776c582f
13 files changed
+39 -43

No files matched your search

+13 -10
View File
@@ -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 };
};
-10
View File
@@ -10,26 +10,16 @@ type CamelCase<Name extends string> = Name extends `${infer Head}_${infer Tail}`
type Emitter<Properties> = [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<Name & string>]: Emitter<AnalyticsEvents[Name]>;
};
// 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<string, (properties?: Payload) => void>();
const analytics = new Proxy({} as Analytics, {
+1 -8
View File
@@ -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][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 = <Properties>(
emit: (properties: Properties) => void,
key: string | number | null | undefined,
@@ -24,7 +17,7 @@ const useTrackOnce = <Properties>(
sentRef.current = key;
emit(payloadRef.current());
emit(payloadRef.current() as Properties);
}, [emit, key]);
};