[AI Library] Chapter 20: YouTube Analytics Workflow and Email Automation
Mastering Claude Code
Chapter 20: YouTube Analytics Workflow and Email Automation
Kim Kyung-jin
Mastering Claude Code
Collecting YouTube Channel Data and Analyzing Trends
VS Code sits open. In the left file explorer, a project folder named "YouTube Analysis" waits empty. On the right, Claude Code's chat window blinks. Starting from this empty folder, we will build an automated system that collects YouTube channel data, analyzes trends, and produces professional reports delivered by email.
This chapter shows how to integrate MCP, skills, the GWS command-line approach, and the WAT framework into a single workflow.
Project Initialization
The first step is to place a claude.md file in the project. This file serves as the agent's system prompt. It communicates the three-layer structure of the WAT framework,workflow, agent, and tool,to the agent.
After placing the file, ask Claude Code: "Please initialize the project based on this claude.md file." The agent creates temp, tools, and workflows folders, configures the .env file and .gitignore. The project skeleton is now in place.
Communicating Requirements in Plan Mode
Switch to plan mode. Since the importance of this step was thoroughly emphasized in the previous chapter, we will proceed directly to practice.
This request is intentionally in "brain dump" form. It is not a perfectly polished set of requirements. Plan mode's power reveals itself precisely here.
Agent Research and Questions
The agent does not merely think. It performs web searches to investigate methods for collecting YouTube data and how to use MCP connection servers. Questions come back.
Answer each question. Auto-discover channels, weekly frequency, include sheet records, send email to your Gmail. These answers fill in the blanks in your requirements document.
[Figure 20-1] Screen showing the agent asking questions and collecting answers in plan mode]
Establishing the Execution Plan
The scale of the plan the agent proposes is substantial. It will generate seven Python tools.
1. fetch_youtube_data.py - collect YouTube data 2. analyze_youtube_data.py - analyze collected data 3. generate_charts.py - generate chart images 4. generate_slides.py - compose slide deck 5. send_email_report.py - send email report 6.
export_sheets.py - export to Google Sheets 7. discover_channels.py - auto-discover AI channels
The workflow is created with the name youtube_weekly_report. This workflow acts as conductor, calling the seven tools in sequence.
Approve the plan and the agent creates a task list and executes it one by one. During this time, work on something else on another monitor and check progress occasionally.
Installing Dependencies and Configuring API Keys
When tool creation completes, the agent guides you through the next steps.
Store API keys in the .env file. Tell the agent: "Please install the dependencies. I will provide the YouTube API key." When passing the API key to Claude Code, confirm it is recorded only in the .env file.
For OAuth authentication, you must enable YouTube Data API, Gmail API, and Google Sheets API in your Google Cloud project. This process is identical to what we covered in Chapter 19 with the GWS command-line approach.
First Validation Run
When all configuration is complete, the agent validates the pipeline automatically. The results follow.
[Figure 20-2] Summary of first validation run results - scale of collected data]
Automatic Slide Deck Generation
Open the report that arrived by email. It is a weekly report titled "AI Automation YouTube Analytics."
Email Body
Key metrics,tracking 30 channels and analyzing 187 videos,are positioned at the top. The body includes a list of top videos for the week and content creation recommendations.
PowerPoint Deck Composition
Open the attached PowerPoint file and the following slides appear.
[Figure 20-3] Screen capture of auto-generated PowerPoint slide deck]
Google Sheets Data Structure
Along with the report, data is recorded in Google Sheets. The sheet has three tabs.
Channel Stats: Channel ID, channel name, subscriber count, total views, and video count are recorded by date. Each time it runs weekly, a new row is added, allowing you to track channel growth over time.
Top Videos: Video ID, title, channel name, view count, like count, comment count, engagement rate, and days since publication. Viewing engagement rate alongside days elapsed lets you pinpoint "trending right now" videos with precision.
Weekly Summary: Execution date, tracked channel count, analyzed video count, median view count, median engagement score, and top keywords are summarized in one line. As weekly data accumulates, industry trend shifts become visible at a glance.
Converting to PDF Reports
If you prefer a branded PDF report instead of PowerPoint, you can use the Canvas Design skill. Find and install this skill from the Claude Code template website. Pasting the installation command into Claude Code creates skill files and fonts in the .claude/skills folder.
The agent replaces the existing generate_slides.py tool with a new PDF generation tool and updates the workflow file. The email sending tool also changes the attachment format from PowerPoint to PDF.
The first PDF generation may produce unexpected results. Only the cover and final page are included, while the body charts are missing. In this case, we offer a correction suggestion in natural language.
The agent finds the root cause, fixes the tool, updates the workflow, and then generates a new completed 9-page PDF. The logo is placed at the top, the date is displayed, and all charts and recommendations are included.
[Figure 20-4] Completed branded PDF report screen]
Gmail Integration: Automated Report Delivery
The final step of the workflow is sending the completed report via Gmail.
Structure of the Email Delivery Tool
The send_email_report.py tool handles the following elements.
Since Gmail API is called through the GWS command-line approach, no separate SMTP configuration or app password is required. Once OAuth authentication is complete, a single gws command sends the email.
Simultaneous Google Sheet Update
Data is also recorded in the Google Sheet at the same time the email is sent. You do not need to create the sheet manually. The agent creates a new sheet, adds tabs to an existing sheet, sets the schema (column structure), and enters the data. A new row is added each time the workflow runs weekly, so time-series data accumulates naturally.
Verification and Optimization
Completing the workflow does not mean it can be deployed immediately. A verification and optimization phase remains.
Iterative Refinement in Practice
It is rare for everything to work perfectly on the first run. The YouTube analytics workflow also required multiple iterations.
Each time a problem is discovered, describing it to the agent in natural language prompts the agent to investigate the root cause and fix it. The corrections are reflected in the tool files and workflow files, so the same issues do not recur in subsequent runs.
Security Review
A security review must be performed before deployment.
The agent reviews all code and checks the following items.
Even if items requiring attention are found in the security review, the system may not actually be in a vulnerable state. If the agent reports "There are 3 items requiring attention, but all secrets are being managed securely," deployment can proceed.
[Figure 20-5] Security review summary screen]
Deployment: Scheduled Execution Using Modal
To run the workflow on a weekly schedule, you must deploy it to the cloud. We use Modal, an AI infrastructure platform. Modal charges only when automation runs, making it cost-effective. It also provides free credits.
The deployment process works like this.
The agent performs the following tasks in sequence.
1. Update environment variable paths in scripts 2. Create Modal deployment files 3. Configure Cron schedules 4. Register API keys and authentication credentials as Modal secrets 5. Execute deployment and validate
Once deployment is complete, you can view the app in the Modal dashboard. Validate once more with a manual trigger, then activate the scheduled trigger once normal operation is confirmed.
Maintenance After Deployment
We must emphasize an important distinction mentioned in the previous chapter: what gets deployed are the workflow and tools, not the agent. Therefore, if the deployed workflow encounters a new error during execution, it cannot self-heal.
The maintenance cycle works as follows.
As the workflow stabilizes, this cycle gradually decreases. Initially, checks are needed once or twice per week, but as edge cases are resolved, automated execution alone becomes sufficient.
Complete Architecture Overview
Here we summarize the entire structure of the YouTube analytics workflow at a glance.
[Figure 20-6] Complete YouTube analytics workflow architecture diagram]
MCP (Firecrawl), API (YouTube Data), command-line approach (GWS), skills (Canvas Design), and the WAT framework are organically integrated within a single workflow. Natural language conversation was the only thing needed to orchestrate all of this.
We have walked through the entire process from starting with an empty folder to having a weekly YouTube trends report automatically arrive in your inbox. Beginning with a single prompt, we exchanged questions with the agent to create seven tools and one workflow, then verified, refined, and deployed them.
The intuition gained here,the rhythm of describing your goal in natural language, answering the agent's questions, reviewing the output, and offering refinements,applies equally to any automation you build going forward.
Gyeongkin Kim, AI Policy Expert and Lawyer
AI law and policy specialist · former member of parliament · author of numerous works
If this book has been with you even briefly, please support us so the next story can reach the world.
(Voluntary contributions welcome: Account 302-1096-0948-81, Nonghyup Bank, Account holder: Gyeongkin Kim)
Kim Kyung-jin
Attorney · Former Member of the National Assembly · AI Policy Researcher
© 2026 Kim Kyung-jin. All rights reserved.



