getChannelStatistics
Retrieve YouTube channel performance metrics including subscriber count, view count, video count, and creation date for multiple channels to analyze reach and engagement.
Instructions
Retrieves statistics for multiple channels. Returns detailed metrics including subscriber count, view count, video count, and channel creation date for each channel. Use this when you need to analyze the performance and reach of multiple YouTube channels.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| channelIds | Yes | Array of YouTube channel IDs to get statistics for |
Implementation Reference
- The main handler function for the getChannelStatistics tool. Validates input, fetches statistics for multiple channels using YoutubeService, and formats the response.export const getChannelStatisticsHandler = async ( params: ChannelStatisticsParams, youtubeService: YoutubeService ): Promise<CallToolResult> => { try { const validatedParams = getChannelStatisticsSchema.parse(params); const statsPromises = validatedParams.channelIds.map((channelId) => youtubeService.getChannelStatistics(channelId) ); const statisticsResults = await Promise.all(statsPromises); return formatSuccess(statisticsResults); } catch (error: unknown) { return formatError(error); } };
- Zod schema for validating the input parameters: array of channel IDs.export const getChannelStatisticsSchema = z.object({ channelIds: z .array(channelIdSchema) .min(1, "Channel IDs array must contain at least 1 element(s)") .describe("Array of YouTube channel IDs to get statistics for"), });
- src/tools/index.ts:107-113 (registration)Registration of the tool in the allTools function, providing config and wrapped handler with youtubeService dependency.config: getChannelStatisticsConfig, handler: (params) => getChannelStatisticsHandler( params as unknown as ChannelStatisticsParams, youtubeService ), },
- Helper method in YoutubeService that fetches channel statistics via YouTube API, parses numbers, handles caching, and errors.async getChannelStatistics( channelId: string ): Promise<LeanChannelStatistics> { const cacheKey = channelId; const operation = async (): Promise<LeanChannelStatistics> => { try { const response = await this.trackCost( () => this.youtube.channels.list({ part: ["snippet", "statistics"], id: [channelId], }), API_COSTS["channels.list"] ); if (!response.data.items?.length) { throw new Error("Channel not found."); } const channel = response.data.items[0]; return { channelId: channelId, title: channel.snippet?.title, subscriberCount: parseYouTubeNumber( channel.statistics?.subscriberCount ), viewCount: parseYouTubeNumber(channel.statistics?.viewCount), videoCount: parseYouTubeNumber(channel.statistics?.videoCount), createdAt: channel.snippet?.publishedAt, }; } catch (error) { throw new YouTubeApiError( `YouTube API call for getChannelStatistics failed for channelId: ${channelId}`, error ); } }; return this.cacheService.getOrSet( cacheKey, operation, CACHE_TTLS.STANDARD, CACHE_COLLECTIONS.CHANNEL_STATS ); }