Coding level: Beginner
Duration: 20 minutes
Project type: Automation with a time-driven trigger
About this solution
This solution tracks the performance of public YouTube videos, including views, likes, and comments, in a Google Sheets spreadsheet. The trigger checks for updated information each day and sends an email if videos have new comment activity so that you can engage with questions and comments.
How it works
The script uses the advanced YouTube service to get YouTube video details and statistics for the video URLs listed in the Video Link column in each sheet. If the number of comments for a listed video has gone up, the script sends an email notification to the email address that the sheet is named after.
Apps Script services
This solution uses the following services:
- Spreadsheet Service: Gets the YouTube URL information from the spreadsheet.
- Advanced Service: YouTube Data API: Gets the YouTube video details and statistics for each video URL.
- Mail Service: Creates and sends an email with a list of videos that have new comments.
Before you begin
To use this sample, you need the following prerequisites:
- A Google Account (Google Workspace accounts might require administrator approval)
- A web browser with access to the internet
Step 1: Set up the spreadsheet
- Click the button below to make a copy of the Track YouTube video views and
Make a copy
- Change the name of the Your_EmailL_Address sheet to your email address.
- Add the YouTube video URLs you want to track or use the provided URLs for
testing.The URLs must start with the format
Step 2: Set up the YouTube advanced service
- Click Extensions > Apps Script. If YouTube is already listed under Services, you can skip to the next set of steps.
- Next to Services, click Add a service .
- From the list, select YouTube Data API and click Add.
Step 3: Add a trigger
- Click Triggers > Add trigger.
- For Choose which function to run, select markVideos.
- For Select event source, select Time-driven.
- For Select type of time based trigger, select Day timer.
- For Select time of day, choose your preferred time.
- When prompted, authorize the script.
Step 4: Test this solution
The trigger you set up runs the script once daily. You can run the script manually to test it.
- In the Apps Script project, click Editor .
- In the function dropdown, select markVideos.
- Click Run.
- Switch back to the spreadsheet to review the information the script added to the sheet.
- Open your email to review the email with the list of videos that have more than zero comments. When the script runs in the future, it only sends an email with videos whose comment count has increased since the last time the script ran.
Open the Apps Script project
The sample script's code includes in-line comments to help you understand how it works. To access the code, in the spreadsheet, click Extensions > Apps Script.
This sample is maintained by Google with the help of Google Developer Experts.