Blog

How to extract YouTube stats automatically and at scale

today November 1, 2022

Extract YouTube data with Google Apps Script Learn more

There's often a need to extract statistics about YouTube channels; specifically, data about the channels themselves, as well as their videos and playlists. While it's possible to extract this data manually by visiting various YouTube pages and doing a lot of copying and pasting, this approach does not scale well.

Luckily, Google offers an API (application programming interface) to extract this very information. The Google Data API offers a consistent way for developers to pull data about YouTube channels, videos, and playlists. Developers can write programs or scripts that call the different endpoints (URLs) of the API to extract the information. The programs or scripts can be made to run automatically on a set schedule.

The best part of the API is that it's free to use.

What kind of YouTube data can I pull?

Having worked with many APIs, I can safely say that the YouTube Data API is very generous with the information that programmers can extract from it. For instance, just for channels alone, we can pull the following information:

  • Metadata: This includes data elements such as the channel's title, description, URL, create date, language, and country.
  • Content: This covers things like the channel's playlists, liked videos, favorite videos, and more.
  • Statistics: Here we have data elements such as view count, comment count, video count, and topics.
  • Status: We can get information about the channel's visibility, whether it's made for kids, etc.
  • Branding: This category covers information about the channel's branding, such as keywords, language, and country.

The channel API endpoint provides additional categories of information. And besides channels, developers can use the API to get information about playlists, videos, and more.

Finally, developers can use the API to update information on YouTube, not just to retrieve it.

Where can I implement the YouTube Data API?

The YouTube Data API is a REST API, which means it can be used in pretty much any environment that has Internet access. For instance, it's possible to include the API in a script file that resides on your computer. You can run the script manually and have it save information to your local hard drive. This approach, however, isn't very easy to use over time and for many channels.

A better approach in my opinion is to use Google Apps Script (GAS). GAS enables you to run script in the cloud securely, on remote Google Servers. GAS offers several unique advantages that make it my preferred choice for these kinds of applications:

  • GAS comes free with your Gmail account or Google Workspace account.
  • GAS can be tied to Google Sheets, which means that data from the API can be saved in an organized and easy-to-use way.
  • GAS offers an Advanced Service for the YouTube Data API, which is essentially a thin wrapper for the API, making development even easier.
  • GAS lets you define triggers that will call the API on different time intervals or when specific events occur (e.g, a Google Sheet is updated).
  • You can add more functionality into the script, based on your needs. For instance, you can have GAS send out an email message after new information is pulled from YouTube.

In summary, Google Apps Script offers a robust solution to create scripts that call the YouTube Data API with specific requests, store the data into a Google Sheet, and further manipulate or operate on the data as needed – all done automatically and at scale.

What else should I know about the API?

When we request information from the API, we specify what categories of data we are interested in (e.g, metadata, statistics), and how the API should filter the data (e.g, fetch data about a specific channel). Filtering is usually done by IDs – every YouTube channel has a unique ID.

The data we get back from the API is packaged inside JSON object. For instance, all the statistics-related information is nested under 'statistics'. It's important to refer to the API documentation to understand how to get the exact data we need.

Sometimes, the API returns a small set of data (e.g, the name and title of a channel). In such situations it's sufficient to make a single API request. But often we want to return a large set of data (e.g, the title and URL of every video in the channel). This could involve hundreds or even thousands of data elements. The API has a limit of how much information it can bring back in each response. When the data we want exceed that limit, we must make several API requests to get all the data.

Luckily, the YouTube Data API provides a simple mechanism to page through the results, just as we would do on a Google search results page. Our script must therefore include the code to progressively fetch more data, until all data has been received from the API server.

While the API is free to use, it imposes usage quotas. You can find more information about the quotas here.

Extract YouTube data with Google Apps Script Learn more