feat(analytics): self-hosted openpanel with a typed event catalogue

Replaces Vercel Analytics with OpenPanel, driven entirely by env vars so the
whole layer is inert until NEXT_PUBLIC_OPENPANEL_CLIENT_ID is set.

Zero initial load cost. next/script emits a rel=preload even on lazyOnload,
which pulls the sdk during the first paint, so the component skips it: it
inlines the 416 byte official stub (window.op queues calls immediately) and
appends the real script itself on window load plus requestIdleCallback. The
built html carries no preload and no script src for the analytics host.

Events live in one typed catalogue (lib/analytics/events.ts) and track() is
keyed to it, so a wrong name or payload is a type error rather than a silent
bad row. Emitters sit in the hooks that already own the behaviour instead of
being sprinkled through components:

- useShowState covers every favourite and watchlist toggle
- useInfiniteList covers pagination for all six lists
- useTorrents covers all three providers
- Preview, Person and useSearch use a shared useTrackOnce hook

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nerandClaude Opus 5 committed 2026-08-14 12:08:16 +03:00
1 parent 0186c793f2
commit d67a0d4df6
26 files changed
+504 -89

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
type ShowRef = { showId: Show['id']; showType: Show['type'] };
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 };
list_loaded_more: { list: ListName; page: number };
random_shuffled: { index: number };
search_performed: { query: string; showType: Show['type']; results: number };
gallery_tab_changed: { tab: string };
library_toggled: ShowRef & { list: 'favorite' | 'watchlist'; value: boolean };
auth_completed: EmptyPayload;
auth_signed_out: EmptyPayload;
torrents_searched: ShowRef & { provider: string; results: number };
torrent_selected: { provider: string; seeders: number; quality?: string; source?: string; codec?: string };
playback_started: ShowRef & { quality?: string };
playback_failed: ShowRef & { reason: string };
subtitles_selected: { language: string };
};
export type EmptyPayload = Record<string, never>;
export type AnalyticsEvent = keyof AnalyticsEvents;
+30
View File
@@ -0,0 +1,30 @@
import { OPENPANEL_CLIENT_ID } from '@/env';
import type { AnalyticsEvents } from './events';
// 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';
export const track = <Name extends keyof AnalyticsEvents>(name: Name, properties: AnalyticsEvents[Name]) => {
if (!ready()) return;
op()?.('track', name, properties);
};
export const identify = (profileId: string) => {
if (!ready()) return;
op()?.('identify', { profileId });
};
export const clearIdentity = () => {
if (!ready()) return;
op()?.('clear');
};
export type { AnalyticsEvents, ListName } from './events';
+30
View File
@@ -0,0 +1,30 @@
import { useEffect, useRef } from 'react';
import type { AnalyticsEvents } from './events';
import { track } from '.';
/**
* Fires an event 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 = <Name extends keyof AnalyticsEvents>(
name: Name,
key: string | number | null | undefined,
payload: () => AnalyticsEvents[Name]
) => {
const payloadRef = useRef(payload);
const sentRef = useRef<string | number | null>(null);
payloadRef.current = payload;
useEffect(() => {
if (key == null || sentRef.current === key) return;
sentRef.current = key;
track(name, payloadRef.current());
}, [name, key]);
};
export default useTrackOnce;