appscript.dev
Automation Intermediate Sheets

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

  1. logYoutubeStats calls YouTube.Channels.list, asking only for the statistics part of the channel identified by CHANNEL_ID. Requesting one part keeps the response small and the quota cost low.
  2. It checks that the response actually contains a channel. A mistyped ID returns an empty items array, so the script logs a message and stops rather than throwing.
  3. It reads the statistics object from the single result — this holds viewCount, subscriberCount, and videoCount.
  4. 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:

DateViewsSubscribersVideos
2025-09-13 06:14184320271048
2025-09-14 06:13184905271848
2025-09-15 06:12185640272949

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:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. Choose logYoutubeStats, event source Time-driven, type Day timer, and pick an early hour such as 6am to 7am.
  4. 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

  • subscriberCount is 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.
  • statistics is 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.list call 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, subscriberCount may 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 with getSheetByName instead.

Related