Build a YouTube analytics tracker
Log Northwind's YouTube channel views and subscribers daily into a Sheet.
Published Sep 13, 2025
YouTube Studio shows Northwind’s channel numbers in the moment, but it does not keep a tidy daily history you can chart or hand to a marketing meeting. The team wants to see the trend — is the subscriber count climbing steadily, did a video spike views — and that means logging the figures somewhere of their own.
This script does exactly that. Once a day it reads the channel’s lifetime statistics through the YouTube Data API and appends a dated row to a Sheet. Over a few weeks the Sheet becomes a clean time series you can graph, with no manual copying from YouTube Studio.
What you’ll need
- Enable the YouTube Data API in Advanced Services. In the Apps Script
editor, open Services, add YouTube Data API v3, and keep the
identifier
YouTube. - The channel ID you want to track. Find it in YouTube Studio under
Settings → Channel → Advanced settings; it starts with
UC. - A Google Sheet to log into, with its ID copied into the config below. The script appends to the first tab — add a header row yourself if you want one.
The script
// The YouTube channel to track — find this in Studio's advanced settings.
const CHANNEL_ID = 'UCxxxxxxxxxxx';
// The Sheet that stores the daily history.
const STATS_SHEET_ID = '1abcYoutubeId';
/**
* Reads the channel's lifetime statistics and appends one dated row
* to the log Sheet. Designed to run once a day on a trigger.
*/
function logYoutubeStats() {
// 1. Ask the YouTube Data API for just the statistics block of
// this one channel — views, subscribers, video count.
const res = YouTube.Channels.list('statistics', { id: CHANNEL_ID });
// 2. Guard: a bad channel ID or an API hiccup returns no items.
if (!res.items || !res.items.length) {
Logger.log('No channel found for ID ' + CHANNEL_ID + ' — nothing logged.');
return;
}
// 3. Pull the statistics object from the single result.
const stats = res.items[0].statistics;
// 4. Append a dated row. Counts arrive as strings, so Number()
// stores them as real numbers the Sheet can chart and sum.
const sheet = SpreadsheetApp.openById(STATS_SHEET_ID).getSheets()[0];
sheet.appendRow([
new Date(),
Number(stats.viewCount),
Number(stats.subscriberCount),
Number(stats.videoCount),
]);
Logger.log('Logged ' + stats.subscriberCount + ' subscribers.');
}
How it works
logYoutubeStatscallsYouTube.Channels.list, asking only for thestatisticspart of the channel identified byCHANNEL_ID. Requesting one part keeps the response small and the quota cost low.- It checks that the response actually contains a channel. A mistyped ID
returns an empty
itemsarray, so the script logs a message and stops rather than throwing. - It reads the
statisticsobject from the single result — this holdsviewCount,subscriberCount, andvideoCount. - It appends a row to the first tab of the log Sheet: a timestamp plus the
three counts. The API returns counts as strings, so
Number()converts them so Sheets treats them as numbers, not text.
Example run
The YouTube Data API returns a statistics block like this:
{
"viewCount": "184320",
"subscriberCount": "2710",
"videoCount": "48"
}
After a run, the log Sheet gains a row:
| Date | Views | Subscribers | Videos |
|---|---|---|---|
| 2025-09-13 06:14 | 184320 | 2710 | 48 |
| 2025-09-14 06:13 | 184905 | 2718 | 48 |
| 2025-09-15 06:12 | 185640 | 2729 | 49 |
A few weeks of rows like these is a ready-made dataset for a line chart of subscriber growth.
Trigger it
To build the daily history without thinking about it, run the script on a schedule:
- In the Apps Script editor, open Triggers (the clock icon).
- Click Add Trigger.
- Choose
logYoutubeStats, event source Time-driven, type Day timer, and pick an early hour such as 6am to 7am. - Save and approve the authorisation prompt.
Run it at the same time each day so the rows are evenly spaced and the trend line is honest.
Watch out for
subscriberCountis rounded by YouTube once a channel grows past a few thousand subscribers — the API reports the same rounded figure the public sees. The trend stays accurate; the exact number may not.statisticsis lifetime, not per-day. The script logs running totals, so to see daily change you subtract one row from the previous one (or add a Sheet formula that does it).- The YouTube Data API has a daily quota. One
channels.listcall costs a single quota unit, so a once-daily trigger is trivial — but do not loop the call across many channels without checking your quota. - If subscriber counts are hidden in the channel’s settings,
subscriberCountmay be absent from the response. Add a fallback (stats.subscriberCount || 0) if that applies to your channel. - The script logs whatever
getSheets()[0]returns. If someone reorders the tabs, point it at a named sheet withgetSheetByNameinstead.
Related
Sync calendar bookings with Calendly
Bridge Google Calendar and Calendly — Northwind bookings on either side appear on both.
Updated Jan 7, 2026
Connect to an air-quality and weather feed
Build a Northwind environmental dashboard — current London AQI plus 5-day forecast.
Updated Dec 30, 2025
Build a podcast and media stats tracker
Pull Northwind's podcast download numbers across platforms into a single sheet.
Updated Dec 10, 2025
Track real-estate listings for new matches
Monitor property feeds for Northwind office hunts — alert when a match appears.
Updated Nov 28, 2025
Translate columns with a translation API
Localise Northwind text in bulk without manual work — via Google Translate or DeepL.
Updated Nov 24, 2025