Here’s another awesome Google Sheet app that you will surely find useful. A step-by-step tutorial on leveraging Google Sheets script to scrape YouTube channel total views. Yes, you can easily find out the total view count of any channel you want in bulk by using Google Sheet!
This is a fantastic tool for content creators, marketers, and data enthusiasts who wish to analyze channel performance directly from Google Sheets. Without further ado, lLet’s dive into the process.
PART 1: Get A Youtube V3 API Key
Obtain a YouTube V3 API Key, follow the steps below:
- Go to https://console.cloud.google.com/
- Create a new project by selecting “New Project.”
- Once your project is set up, go to the dashboard, then access the “Library” section.
- Search for “YouTube Data API v3” click it and click the enable button.
- Now, go to “Credentials,” then “Create credentials“, select the “API key.” and copy this key.
PART 2: Gather Channel IDs
On this part, you cannot simply copy the Youtube Channel URL, there’s a reason for this. This is because the Youtube v3 API does not have a feature for directly gathering details from Vanity channel URLs that ends with @MrBeast as an example. But, we have to use the Channel ID instead.
There are actually a lot of ways to gather the YouTube channel IDs, on this purpose we are going to use the quickiest method. To acquire the channel ID you wish to analyze, follow these steps:
- Visit the Google Chrome Web Store and search for a “YouTube Channel ID” extension.
- Install the first extension that appears in the search results.
- Add it to your chrome, then go to Youtube and find the channel’s you want to scrape.
- Navigate to the channel page, click on the extension icon, and copy the displayed ID.
PART 3: Running the Script
With the API key ready you have created on the first part, and channel IDs you gathered on the 2nd part, it’s time to run the script that does the magic.
- Access your Google Sheets’ “Extensions” menu, select “Apps Script,” and paste the script below:
function fetchChannelStats() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Assume channel IDs are directly in column A, starting from row 2
const range = sheet.getRange("A2:A" + sheet.getLastRow());
const values = range.getValues();
const apiKey = 'YOUR_API_HERE'; // Replace with your actual API key from Google Cloud Consolehttps://youtu.be/7wRVd6Rjxvc
const baseApiUrl = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&key=' + apiKey;
values.forEach((row, index) => {
const channelId = row[0].trim(); // Trim whitespace in Column A just in case you missed space
if (channelId && channelId.startsWith('UC')) { // Basic check to ensure it looks like a channel ID
const apiUrl = baseApiUrl + '&id=' + channelId;
try {
const response = UrlFetchApp.fetch(apiUrl);
const json = JSON.parse(response.getContentText());
if (json.items && json.items.length > 0) {
const viewCount = json.items[0].statistics.viewCount;
// Output the view count in column B, corresponding to each channel ID in column A
sheet.getRange("B" + (index + 2)).setValue(viewCount);
} else {
sheet.getRange("B" + (index + 2)).setValue("No data found");
}
} catch (e) {
// Log and indicate an error if the fetch fails
sheet.getRange("B" + (index + 2)).setValue("Error fetching data");
Logger.log(e.toString());
}
} else {
// If the input does not appear to be a valid channel ID, indicate this in the sheet
sheet.getRange("B" + (index + 2)).setValue("Invalid channel ID format");
}
});
}
- Replace the placeholder API key in the script with the one you obtained from Google Cloud Console.
- Save and run the script. If it’s your first time running it, you’ll need to authorize permissions. Follow the prompts to proceed.
How It Works
This script iterates through each channel ID listed in column A, fetching its total view count using the YouTube Data API, and then populating column B with these view counts. It includes basic error handling, such as identifying invalid channel IDs and API fetch issues, ensuring your sheet remains clear and informative.