Coding level: Beginner
Duration: 20 minutes
Project type: Automation with a time-driven trigger
Objectives
- Understand what the solution does.
- Understand what the Apps Script services do within the solution.
- Set up the script.
- Run the script.
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.
- YouTube Data API advanced service–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.
Prerequisites
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.
Set up the script
Create the Apps Script project
- Click the following button to make a copy of the Track YouTube video views and
comments spreadsheet. The Apps Script project for
this solution is attached to the spreadsheet.
Make a copy - In your copied spreadsheet, change the name of the Your_Email_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
www.youtube.com/watch?v=
. - Click Extensions > Apps Script. If YouTube is already listed under Services, you can skip to the next 2 steps.
- Next to Services, click Add a service .
- From the list, select YouTube Data API and click Add.
Create a trigger
- In the Apps Script project, 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. If the OAuth consent screen displays the warning, This app isn't verified, continue by selecting Advanced > Go to {Project Name} (unsafe).
Run the script
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.
Review the code
To review the Apps Script code for this solution, click View source code below:
View source code
Code.gs
email.html
Contributors
This sample is maintained by Google with the help of Google Developer Experts.