From d67a0d4df646e3eb4a8464ccc28362d7fbf1201d Mon Sep 17 00:00:00 2001 From: ner Date: Fri, 14 Aug 2026 12:08:16 +0300 Subject: [PATCH] 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) --- app/(site)/(root)/useRandomMovie.ts | 8 +- app/(site)/popular/usePopular.ts | 2 +- app/(site)/top/useTop.ts | 3 +- app/layout.tsx | 2 +- components/Analytics/index.tsx | 66 ++++ .../Header/components/Search/useSearch.ts | 9 +- components/Header/components/User/useAuth.ts | 2 + .../Header/components/User/useLogOut.ts | 7 +- components/Person/index.tsx | 3 + .../Player/components/Captions/index.tsx | 7 +- .../Torrents/components/Player/index.tsx | 13 + .../components/Actions/index.tsx | 13 +- .../Preview/components/Torrents/index.tsx | 11 +- .../components/Torrents/useTorrents.ts | 16 +- components/Preview/index.tsx | 14 +- components/Trending/index.tsx | 11 +- env.example | 4 + env.ts | 4 + hooks/useInfiniteList.ts | 19 +- hooks/useShowState.ts | 3 + hooks/useUsersShows.ts | 2 +- lib/analytics/events.ts | 31 ++ lib/analytics/index.ts | 30 ++ lib/analytics/useTrackOnce.ts | 30 ++ package-lock.json | 281 +++++++++++++----- package.json | 2 +- 26 files changed, 504 insertions(+), 89 deletions(-) create mode 100644 components/Analytics/index.tsx create mode 100644 lib/analytics/events.ts create mode 100644 lib/analytics/index.ts create mode 100644 lib/analytics/useTrackOnce.ts diff --git a/app/(site)/(root)/useRandomMovie.ts b/app/(site)/(root)/useRandomMovie.ts index f72f148..109a25c 100644 --- a/app/(site)/(root)/useRandomMovie.ts +++ b/app/(site)/(root)/useRandomMovie.ts @@ -5,6 +5,7 @@ import { atom, getDefaultStore, useAtom } from 'jotai'; import randomQuery from '@/data/queries/random'; import useInfiniteList from '@/hooks/useInfiniteList'; +import { track } from '@/lib/analytics'; const indexAtom = atom(0); @@ -17,7 +18,11 @@ interface Props { const useRandomMovie = ({ page }: Props) => { const [index, setIndex] = useAtom(indexAtom); - const { shows: movies, fetchNextPage } = useInfiniteList({ ...randomQuery({ page }), mode: 'default' }); + const { shows: movies, fetchNextPage } = useInfiniteList({ + ...randomQuery({ page }), + mode: 'default', + list: 'random' + }); if (movies.length && !movies[index]) { setIndex(0); @@ -29,6 +34,7 @@ const useRandomMovie = ({ page }: Props) => { fetchNextPage, onIndexChange: useCallback( (index: number) => { + track('random_shuffled', { index }); startTransition(() => setIndex(index)); }, [setIndex] diff --git a/app/(site)/popular/usePopular.ts b/app/(site)/popular/usePopular.ts index 58bb290..64b7e55 100644 --- a/app/(site)/popular/usePopular.ts +++ b/app/(site)/popular/usePopular.ts @@ -3,6 +3,6 @@ import popularQuery from '@/data/queries/popular'; import useInfiniteList from '@/hooks/useInfiniteList'; -const usePopular = () => useInfiniteList({ ...popularQuery(), mode: 'default' }); +const usePopular = () => useInfiniteList({ ...popularQuery(), mode: 'default', list: 'popular' }); export default usePopular; diff --git a/app/(site)/top/useTop.ts b/app/(site)/top/useTop.ts index 50ade6c..9f28160 100644 --- a/app/(site)/top/useTop.ts +++ b/app/(site)/top/useTop.ts @@ -3,6 +3,7 @@ import popularQuery from '@/data/queries/popular'; import useInfiniteList from '@/hooks/useInfiniteList'; -const useTop = () => useInfiniteList({ ...popularQuery({ sortBy: 'vote_average.desc' }), mode: 'default' }); +const useTop = () => + useInfiniteList({ ...popularQuery({ sortBy: 'vote_average.desc' }), mode: 'default', list: 'top' }); export default useTop; diff --git a/app/layout.tsx b/app/layout.tsx index f355c43..c85df53 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,8 +1,8 @@ import React, { ReactNode } from 'react'; -import { Analytics } from '@vercel/analytics/next'; import type { Metadata, Viewport } from 'next'; import { Jost } from 'next/font/google'; +import Analytics from '@/components/Analytics'; import Header from '@/components/Header'; import ThemeColor from '@/components/ThemeColor'; import { SITE_URL, TITLE } from '@/env'; diff --git a/components/Analytics/index.tsx b/components/Analytics/index.tsx new file mode 100644 index 0000000..1115f3c --- /dev/null +++ b/components/Analytics/index.tsx @@ -0,0 +1,66 @@ +'use client'; + +import { useEffect } from 'react'; +import { getInitSnippet } from '@openpanel/web'; + +import { OPENPANEL_API_URL, OPENPANEL_CLIENT_ID, OPENPANEL_SCRIPT_URL } from '@/env'; + +const SCRIPT_ID = 'openpanel-sdk'; + +const SRC = OPENPANEL_SCRIPT_URL || 'https://openpanel.dev/op1.js'; + +const OPTIONS = { + clientId: OPENPANEL_CLIENT_ID, + apiUrl: OPENPANEL_API_URL, + trackScreenViews: true, + trackOutgoingLinks: true +}; + +/** + * next/script emits a `rel=preload` even on `lazyOnload`, which pulls the sdk during the + * initial load. The inline stub below is a few hundred bytes and makes `window.op` queue + * calls immediately, so the real script can wait until the page is loaded and idle. + */ +const Analytics = () => { + useEffect(() => { + if (!OPENPANEL_CLIENT_ID) return; + + let cancelled = false; + + const append = () => { + if (cancelled || document.getElementById(SCRIPT_ID)) return; + + const script = document.createElement('script'); + + script.id = SCRIPT_ID; + script.src = SRC; + script.async = true; + + document.body.appendChild(script); + }; + + const schedule = () => + typeof window.requestIdleCallback === 'function' + ? window.requestIdleCallback(append, { timeout: 5000 }) + : window.setTimeout(append, 2000); + + if (document.readyState === 'complete') schedule(); + else window.addEventListener('load', schedule, { once: true }); + + return () => { + cancelled = true; + }; + }, []); + + if (!OPENPANEL_CLIENT_ID) return null; + + return ( +