Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(youtube): load channels that have no public videos

spacesdrive8094e0377ec8
diff --git a/backend/services/youtube.js b/backend/services/youtube.js
index 728afbf..68ad8fc 100644
--- a/backend/services/youtube.js
+++ b/backend/services/youtube.js
@@ -6,7 +6,12 @@ async function ytFetch(endpoint, params, apiKey) {
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
const res = await fetch(url.toString());
const data = await res.json();
- if (data.error) throw new Error(data.error.message);
+ if (data.error) {
+ const err = new Error(data.error.message);
+ err.reason = data.error.errors?.[0]?.reason || '';
+ err.status = data.error.code;
+ throw err;
+ }
return data;
}
@@ -94,7 +99,14 @@ export async function fetchAllVideos(playlistId, apiKey, maxPages = 10) {
const params = { part: 'snippet,contentDetails', playlistId, maxResults: '50' };
if (pageToken) params.pageToken = pageToken;
- const data = await ytFetch('playlistItems', params, apiKey);
+ let data;
+ try {
+ data = await ytFetch('playlistItems', params, apiKey);
+ } catch (err) {
+ // YouTube reports a channel's uploads playlist as missing until its first public video.
+ if (err.reason === 'playlistNotFound') break;
+ throw err;
+ }
if (!data.items || data.items.length === 0) break;
const videoIds = data.items.map(i => i.snippet.resourceId.videoId).join(',');
diff --git a/frontend/src/features/analytics/channel/ChannelAnalytics.jsx b/frontend/src/features/analytics/channel/ChannelAnalytics.jsx
index 61043fd..08dab5a 100644
--- a/frontend/src/features/analytics/channel/ChannelAnalytics.jsx
+++ b/frontend/src/features/analytics/channel/ChannelAnalytics.jsx
@@ -212,6 +212,22 @@ export default function ChannelAnalytics() {
}
/>
+ {analytics.totalVideos === 0 ? (
+ <>
+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-3">
+ <MetricBox small icon={<Users />} label="Subscribers" value={fmtNum(channel.subscriberCount)} subtitle={fmtNumFull(channel.subscriberCount)} />
+ <MetricBox small icon={<Eye />} label="Total views" value={fmtNum(channel.viewCount)} subtitle={fmtNumFull(channel.viewCount)} />
+ <MetricBox small icon={<VideoIcon />} label="Total videos" value={fmtNum(channel.videoCount)} subtitle="Public uploads" />
+ </div>
+ <SurfaceBox>
+ <EmptyState
+ icon={<VideoIcon />}
+ title="No public videos yet"
+ description="Video analytics start with this channel's first public upload. After publishing, choose Refresh."
+ />
+ </SurfaceBox>
+ </>
+ ) : (
<Tabs value={tab} onValueChange={setTab} className="gap-6">
<div className="scrollbar-hide -mx-4 overflow-x-auto px-4 sm:mx-0 sm:px-0">
<TabsList className="w-max min-w-full">
@@ -476,6 +492,7 @@ export default function ChannelAnalytics() {
)}
</TabsContent>
</Tabs>
+ )}
</div>
);
}