Next.js + FastAPI Tutorial: Track Every YouTube Video Event!

CodingEntrepreneurs April 27, 2025
Video Thumbnail
CodingEntrepreneurs Logo

CodingEntrepreneurs

@codingentrepreneurs

About

Learn, Build, and Earn. Coding Entrepreneurs is here to help you learn the skills you need to build the projects you want. Learn by doing everything we do step-by-step. Practical and hands-on. Let's do this. 💽 Code: https://github.com/codingforentrepreneurs 🚀 Enroll. Learn to code. Launch your project: https://cfe.sh/enroll 🚀

Video Description

Watching videos create a ton of data. Let's track, analyze, and own it. In this course, we're going to integrate YouTube Video embeds into a Next.js application. Then, we're going to track and monitor every possible event from YouTube videos with FastAPI and time-series Postgres. This type of data is called "time series" because it's data that occurs over time. Time series data is typically a *ton* of data since so much can happen in only a few seconds. For example... - video loaded (cued) - video played - video paused - video played - Video progress (after N seconds) - video progress (after N Seconds) - video paused - video played That's 8 events that could occur in 5 seconds. What about 250 users? 10,000? 100,000? As you can see, this amount of data can balloon very quickly for a single user -- especially if they are jump around videos often. Modern video platforms and websites do this for good reason: what resonates with our users? How can we help them find more of it? To answer these questions we need to store the data. But how? - Postgres? - MySQL? - Redis? - S3? While all these data stores *might* be able to handle the load, they a missing a key ingredient: scale & time-series. If you were using plain Postgres to track this data. I'd recommend your front-end does some more heavy lifting and just combine the events so you're not sending so much data. So you sacrifice some of the data so you can save your database. That's why you need Timescale. It's an extension for Postgres that optimizes Postgres for time series data while still giving you all the benefits of PostgresSQL and SQL databases. With time series optimized Postgres, we can send *all* of the events. Timescale can handle massive amounts of data coming in and can aggregate that massive amounts of data over chunks of time. This course is about how to technically implement Next.js for the frontend, FastAPI for the backend, and tied together with SQLModel and TimescaleDB though timescaledb-python and Docker. Are you ready to get started? 🕹️ Key Tech: Next.js FastAPI Timescale + PostgresSQL Custom React.js hooks SQLModel + SQLAlchemy TimescaleDB Docker 🖥️ References - Next.js Repo on Github: https://github.com/codingforentrepreneurs/youtube-video-player-nextjs - FastAPI Repo on GitHub: https://github.com/codingforentrepreneurs/youtube-video-analytics-fastapi - My timescaledb-python package: https://pypi.org/project/timescaledb/ - My FastAPI deployment to Railway guide: https://codingforentrepreneurs.com/blog/deploy-fastapi-to-railway-with-this-dockerfile ⭐️ Get production-ready & managed Timescale for free right now: https://tsdb.co/justin-api-2 (sign up at this link as it helps us make more videos)⭐️ 👉 Topics Covered: ✅ Next.js Integration with YouTube Player ✅ Creating React Hooks for Video Tracking ✅ FastAPI Backend with Docker ✅ Data Storage with SQLModel and TimescaleDB ✅ Time Series Analytics and Aggregations ✅ Session-based User Tracking ✅ Building Interactive Metrics Dashboards Chapters: 00:00:00 Welcome 00:02:48 Demo 00:08:11 Next.js Hello World 00:10:15 YouTube Video Embed in Next.js 00:17:05 YouTube Player API as Reactjs Hook 00:28:45 Tracking Video Player State 00:43:04 Start Time & Playback Tracking 00:50:10 YouTube URL Input Form 00:54:47 Extract YouTube ID & Redirect 01:03:23 Mock Sending YouTube Player Events 01:12:13 FastAPI + Docker Rapid Fire Hello World 01:27:11 FastAPI Endpoint for Video Events 01:35:34 FastAPI CORS & Origin Referer 01:43:23 Environment Variables and Docker Compose 01:48:54 camelCase to snake_case 01:54:30 Pydantic to SQLModel 01:59:16 Integrate SQLModel with FastAPI 02:06:32 Storing Data to Postgres with SQLModel 02:13:46 SQLModel to TimescaleModel 02:21:42 Watch Sessions 02:30:07 useWatchSession Hooks 02:42:55 Enrich Requests with Watch Session ID 02:47:28 Ensure Valid Watch Sessions 02:54:15 Time Series Data Aggregations with SQLModel & TimescaleDB 03:05:07 Response Model & Video Stats Aggregations 03:15:09 Query Params for Time Bucket & Range 03:22:14 Metrics Table with SWR 03:31:37 Select Time Bucket in Next.js 03:37:42 Improved Metrics Query and Display Table 03:44:35 Top Videos by Unique Views 03:54:34 Thank you and Next Steps

You May Also Like

Web Dev's Top Tech Gear

AI-recommended products based on this video

Loading...
140W C to C Super Fast Charger,Compatible with MacBook Pro 16, 15, 14, 13 Inch, MacBook Air 13 Inch, iPad Pro 2021/2020/2019/2018, Lenovo and All Type C Devices 7.2ft

140W C to C Super Fast Charger,Compatible with MacBook Pro 16, 15, 14, 13 Inch, MacBook Air 13 Inch, iPad Pro 2021/2020/2019/2018, Lenovo and All Type C Devices 7.2ft

(1,273)
$28.49
FREE delivery Sat, Jun 14 on your first order
400+ bought in past month
Loading...
LISEN USB C Cable 240W - [6.6ft 2Pack] Nylon Braided Type C Cable Fast Charge PD3.1 USBC Fast Charging Data Cord for iPhone 17 16 Pro Max Samsung MacBook Pro,Xbox,iPad Pro/Air, Switch 2

LISEN USB C Cable 240W - [6.6ft 2Pack] Nylon Braided Type C Cable Fast Charge PD3.1 USBC Fast Charging Data Cord for iPhone 17 16 Pro Max Samsung MacBook Pro,Xbox,iPad Pro/Air, Switch 2

(7,293)
$16.13
Prime
2K+ bought in past month
Loading...
UGREEN 130W Car Charger USB C Fast Charger PD3.0 QC4.0 PPS Fast Charging Car Adapter with 100W USB C Cable Compatible with Dell XPS, MacBook, iPhone 16 15 14 Pro Max, Galaxy S25 S24 Ultra, iPad Pro

UGREEN 130W Car Charger USB C Fast Charger PD3.0 QC4.0 PPS Fast Charging Car Adapter with 100W USB C Cable Compatible with Dell XPS, MacBook, iPhone 16 15 14 Pro Max, Galaxy S25 S24 Ultra, iPad Pro

(4,725)
$31.99
500+ bought in past month
Loading...
IFEART 70W USB C Charger for MacBook Air MacBook Pro 13 14 15 16 inch 2025 2024 2023 2022 2021 2020 2019 2018, M1 M2 M3 M4 Laptop USB C Power Adapter, 6.6ft USB C to C Cable, Original Quality
Best Seller

IFEART 70W USB C Charger for MacBook Air MacBook Pro 13 14 15 16 inch 2025 2024 2023 2022 2021 2020 2019 2018, M1 M2 M3 M4 Laptop USB C Power Adapter, 6.6ft USB C to C Cable, Original Quality

(4,167)
$29.99
FREE delivery Sat, Jun 14 on your first order
1K+ bought in past month
Loading...
Laptop Sleeve Case 11–14 Inch MacBook Air MacBook Pro Surface Laptop Dell XPS 13 HP Envy Pavilion Lenovo Yoga Slim IdeaPad Acer Swift Chromebook Samsung Galaxy Book Shockproof Leather Bag Colour Blue

Laptop Sleeve Case 11–14 Inch MacBook Air MacBook Pro Surface Laptop Dell XPS 13 HP Envy Pavilion Lenovo Yoga Slim IdeaPad Acer Swift Chromebook Samsung Galaxy Book Shockproof Leather Bag Colour Blue

(98)
$81.45
Loading...
Gumdrop ProTech Case for Dell New XPS 13 Plus 9320 Laptop- Slim Lightweight Case with Energy Displacement Protection, Non-Skid Rubber Feet and Frosted Exterior for Bumps and Drops (Black)

Gumdrop ProTech Case for Dell New XPS 13 Plus 9320 Laptop- Slim Lightweight Case with Energy Displacement Protection, Non-Skid Rubber Feet and Frosted Exterior for Bumps and Drops (Black)

(49)
$109.43$108.34
FREE delivery Aug 11 - 13
Loading...
65W 45W USB C Laptop Charger Compatible with Dell Latitude 5420 5430 5520 5530 7340 7430 Inspiron 14 16 XPS 13 Chromebook 3100 Dell Computer Type C Fast Charging AC Adapter Power Cord

65W 45W USB C Laptop Charger Compatible with Dell Latitude 5420 5430 5520 5530 7340 7430 Inspiron 14 16 XPS 13 Chromebook 3100 Dell Computer Type C Fast Charging AC Adapter Power Cord

(7)
$19.99
FREE delivery Sun, Aug 24 on your first order
Loading...
45W 65W AC Adapter Laptop Charger for Dell Inspiron 15 14 13 17 3000 5000 7000 Series 5558 5559 Charger Latitude E6440 E6430 3520 3420 XPS 13 14 9333 9350 Notebook Dell Computer Power Supply Cord

45W 65W AC Adapter Laptop Charger for Dell Inspiron 15 14 13 17 3000 5000 7000 Series 5558 5559 Charger Latitude E6440 E6430 3520 3420 XPS 13 14 9333 9350 Notebook Dell Computer Power Supply Cord

(52)
$13.99
FREE delivery Sun, Aug 24 on your first order
100+ bought in past month
Loading...
Logitech G203 Wired Gaming Mouse, 8,000 DPI, Rainbow Optical Effect LIGHTSYNC RGB, 6 Programmable Buttons, On-Board Memory, Screen Mapping, PC/Mac Computer and Laptop Compatible - Black
Best Seller

Logitech G203 Wired Gaming Mouse, 8,000 DPI, Rainbow Optical Effect LIGHTSYNC RGB, 6 Programmable Buttons, On-Board Memory, Screen Mapping, PC/Mac Computer and Laptop Compatible - Black

(17,871)
$27.98$22.38
FREE delivery Tue, Oct 14 on your first order
500+ bought in past month
Loading...
Logitech G305 Lightspeed Wireless Gaming Mouse, Hero 12K Sensor, 12,000 DPI, Lightweight, 6 Programmable Buttons, 250h Battery Life, On-Board Memory, PC/Mac - Black

Logitech G305 Lightspeed Wireless Gaming Mouse, Hero 12K Sensor, 12,000 DPI, Lightweight, 6 Programmable Buttons, 250h Battery Life, On-Board Memory, PC/Mac - Black

(35,879)
$49.99$44.99
FREE delivery Mon, Jan 26
1K+ bought in past month
Loading...
IMGadgets 47 x 24 Inches Height Adjustable Electric Standing Desk 2024, 5th Gen Quieter Motor Ergonomic Sit Stand Up Home Office Computer Desk with Height Memory Buttons, Display, 2 Headphone Holders
Best Seller

IMGadgets 47 x 24 Inches Height Adjustable Electric Standing Desk 2024, 5th Gen Quieter Motor Ergonomic Sit Stand Up Home Office Computer Desk with Height Memory Buttons, Display, 2 Headphone Holders

(438)
$169.95
FREE delivery Thu, Jun 26
200+ bought in past month
Loading...
MeLE Quieter4C Fanless Mini PC 16GB RAM 512GB Storage Micro PC Twin Lake N150(up to 3.6GHz) USB-C 2 X HDMI 4K@60 Triple Display Small Form Factor Desktop Computers with VESA Mount for Home/Office

MeLE Quieter4C Fanless Mini PC 16GB RAM 512GB Storage Micro PC Twin Lake N150(up to 3.6GHz) USB-C 2 X HDMI 4K@60 Triple Display Small Form Factor Desktop Computers with VESA Mount for Home/Office

(83)
$339.99$333.46
PrimeFREE delivery Sat, Jun 14
Loading...
KAMRUI Hyper H2 Mini PC, i5-14450HX Processor Mini Gaming PC (Up to 4.8GHz, 10C/16T), 16GB RAM 512GB SSD Mini Desktop Computers, Triple 4K Display 6*USB3.2, USB-C, HDMI, DP, WiFi6, BT5.2, Office Home Global Recycled Standard

KAMRUI Hyper H2 Mini PC, i5-14450HX Processor Mini Gaming PC (Up to 4.8GHz, 10C/16T), 16GB RAM 512GB SSD Mini Desktop Computers, Triple 4K Display 6*USB3.2, USB-C, HDMI, DP, WiFi6, BT5.2, Office Home Global Recycled Standard

(210)
$602.65
Prime
Loading...
MeLE Overclock X5 Core i5-12450H (8C/12T, up to 4.4GHz) Ultrathin Mini PC 32GB DDR4 1TB SSD, Mini Desktop Computer with 2X HDMI + 2X USB-C 4K Quad Display, Dual LAN 1G+ 2.5G LAN, 2.4/5GHz WiFi BT5.2

MeLE Overclock X5 Core i5-12450H (8C/12T, up to 4.4GHz) Ultrathin Mini PC 32GB DDR4 1TB SSD, Mini Desktop Computer with 2X HDMI + 2X USB-C 4K Quad Display, Dual LAN 1G+ 2.5G LAN, 2.4/5GHz WiFi BT5.2

(111)
$559.00
FREE delivery Wed, Jun 18
Loading...
MeLE Quieter4C N150 Fanless Mini PC (up to 3.6GHz 4C/4T) 16GB LPDDR5 512GB Micro Desktop Computer, Support 4K Triple Display, Dual HDMI, All-in-One USB-C WiFi 5 BT5.1 Ethernet

MeLE Quieter4C N150 Fanless Mini PC (up to 3.6GHz 4C/4T) 16GB LPDDR5 512GB Micro Desktop Computer, Support 4K Triple Display, Dual HDMI, All-in-One USB-C WiFi 5 BT5.1 Ethernet

(170)
$349.99
PrimeFREE delivery Sat, Jun 14
Loading...
NEEWER Advanced 18 inch LED Ring Light for Phone, LCD Touch Screen, 2.4G Remote Lights Control, 3200-5600K, Tripod Light for iPhone Action Camera, for Studio Makeup TikTok YouTube Video Salon (Black)

NEEWER Advanced 18 inch LED Ring Light for Phone, LCD Touch Screen, 2.4G Remote Lights Control, 3200-5600K, Tripod Light for iPhone Action Camera, for Studio Makeup TikTok YouTube Video Salon (Black)

(2,807)
$181.49
FREE delivery Sun, Aug 10
Loading...
HOMIEE HOMIEE 15.6-Inch Laptop Backpack, High-Capacity Backpack for Men Women USB Charging Port Travel/Business/College Backpack, Blue

HOMIEE HOMIEE 15.6-Inch Laptop Backpack, High-Capacity Backpack for Men Women USB Charging Port Travel/Business/College Backpack, Blue

(1,409)
$37.99
PrimeFREE delivery Sat, Jun 14
Loading...
RAOYI 5Pack 64GB USB Flash Drive USB Stick USB 2.0 Memory Stick Slide Retractable Thumb Drive 64G USB Drive USB Key for Data Storage (Black Red Blue Green Purple)

RAOYI 5Pack 64GB USB Flash Drive USB Stick USB 2.0 Memory Stick Slide Retractable Thumb Drive 64G USB Drive USB Key for Data Storage (Black Red Blue Green Purple)

(4,402)
$26.09
Prime
1K+ bought in past month
Loading...
Asus ZenScreen 16” Portable USB Monitor (MB169CK) - Full HD, IPS, Dual USB-C, Eye Care, Flicker Free, Blue Light Filter, Anti-Glare Surface, 360° Kickstand, 3 yr Warranty

Asus ZenScreen 16” Portable USB Monitor (MB169CK) - Full HD, IPS, Dual USB-C, Eye Care, Flicker Free, Blue Light Filter, Anti-Glare Surface, 360° Kickstand, 3 yr Warranty

(111)
$129.99$100.27
FREE delivery Jan 9 - Feb 4
100+ bought in past month
Loading...
RAOYI 5pcs 32GB USB 3.0 Flash Drive Memory Stick Fold Storage Thumb Stick Pen Drive U Disk Swivel Design (5 Mixed Colors: Black Red Blue Green Purple)

RAOYI 5pcs 32GB USB 3.0 Flash Drive Memory Stick Fold Storage Thumb Stick Pen Drive U Disk Swivel Design (5 Mixed Colors: Black Red Blue Green Purple)

(1,667)
$31.99
FREE delivery Sat, Jun 14 on your first order
Loading...
llano RGB Laptop Cooling Pad with Powerful Turbofan Gaming Cooler Radiator with Infinitely Variable Speed, Touch Control, LCD Screen, 3-Port USB Hub, Seal Foam for Notebook Computer 15-19in

llano RGB Laptop Cooling Pad with Powerful Turbofan Gaming Cooler Radiator with Infinitely Variable Speed, Touch Control, LCD Screen, 3-Port USB Hub, Seal Foam for Notebook Computer 15-19in

(1,404)
$129.99
Prime
200+ bought in past month
Loading...
Monster N-Lite 211 Wireless Earbuds, Bluetooth 6.0 Stereo Headphones with Noise-Cancelling Mic and LED Display, Built-in Mic for Clear Calls, Waterproof Design for Sports

Monster N-Lite 211 Wireless Earbuds, Bluetooth 6.0 Stereo Headphones with Noise-Cancelling Mic and LED Display, Built-in Mic for Clear Calls, Waterproof Design for Sports

(8)
$39.99
FREE delivery Tue, Nov 4
Loading...
Monster N-Lite 211 Wireless Earbuds, Bluetooth 6.0 Stereo Headphones with Powerful Bass, Built-in Noise-Cancelling Mic for Clear Calls, LED Display, 36H Playtime, IPX5 Waterproof Design for Sports

Monster N-Lite 211 Wireless Earbuds, Bluetooth 6.0 Stereo Headphones with Powerful Bass, Built-in Noise-Cancelling Mic for Clear Calls, LED Display, 36H Playtime, IPX5 Waterproof Design for Sports

(40)
$39.99
FREE delivery Thu, Nov 13
Loading...
Anker USB C Cable, [2-Pack, 6 ft] Type C Charger Premium Nylon USB Cable, USB A to Type C Charging Cable Fast Charge for Samsung Galaxy S10 S10+ / Note 8, LG V20 and Other USB C Charger (Black)

Anker USB C Cable, [2-Pack, 6 ft] Type C Charger Premium Nylon USB Cable, USB A to Type C Charging Cable Fast Charge for Samsung Galaxy S10 S10+ / Note 8, LG V20 and Other USB C Charger (Black)

(103,365)
$13.49
5K+ bought in past month
Loading...
Anker USB C Charger Cable (6ft 100W, 2Pack), USB 2.0 Type C Fast Charging Cable for iPhone 15 / 15Pro / 15Plus / 15ProMax MacBook Pro 2020, iPad Pro 2020, iPad Air 4, Samsung Galaxy S23+/S23 Ultra ClimatePartner certified

Anker USB C Charger Cable (6ft 100W, 2Pack), USB 2.0 Type C Fast Charging Cable for iPhone 15 / 15Pro / 15Plus / 15ProMax MacBook Pro 2020, iPad Pro 2020, iPad Air 4, Samsung Galaxy S23+/S23 Ultra ClimatePartner certified

(44,904)
$17.99
FREE delivery Wed, Aug 20 on your first order
6K+ bought in past month
Loading...
Anker Nano USB C Wall Charger,45W Fast Charging Smart Display Charger,with 180°Foldable Plug,Smart Recognition,Built-in Care Mode,for iPhone17/16/15 (Non-Battery,One USB-C Port,No Cable Included) ClimatePartner certified

Anker Nano USB C Wall Charger,45W Fast Charging Smart Display Charger,with 180°Foldable Plug,Smart Recognition,Built-in Care Mode,for iPhone17/16/15 (Non-Battery,One USB-C Port,No Cable Included) ClimatePartner certified

(698)
$39.99
FREE delivery Wed, Mar 4
1K+ bought in past month
Loading...
Anker 332 USB-C Hub (5-in-1) with 4K HDMI Display, 5Gbps - and 2 5Gbps USB-A Data Ports and for MacBook Pro, MacBook Air, Dell XPS, Lenovo Thinkpad, HP Laptops and More

Anker 332 USB-C Hub (5-in-1) with 4K HDMI Display, 5Gbps - and 2 5Gbps USB-A Data Ports and for MacBook Pro, MacBook Air, Dell XPS, Lenovo Thinkpad, HP Laptops and More

(11,639)
$23.99
FREE delivery Sat, Jun 14 on your first order
1K+ bought in past month
Loading...
NEXPOW Car Jump Starter,Car Battery Jump Starter Pack 1500A Peak Q10S for Up to 7.0L Gas and 5.5L Diesel Engine12V Auto Battery Booster,Jumper Cables,Portable Lithium Jump Box with LED Light/USB QC3.0

NEXPOW Car Jump Starter,Car Battery Jump Starter Pack 1500A Peak Q10S for Up to 7.0L Gas and 5.5L Diesel Engine12V Auto Battery Booster,Jumper Cables,Portable Lithium Jump Box with LED Light/USB QC3.0

(4,578)
$71.99
FREE delivery Sat, Jan 3
2K+ bought in past month
Loading...
Firefly Variety 8 Pack - Fire Starter Accessory for Swiss Army Victorinox Knives (Neon Green-Yellow Glow)

Firefly Variety 8 Pack - Fire Starter Accessory for Swiss Army Victorinox Knives (Neon Green-Yellow Glow)

(181)
$70.15$61.13
$5.05 delivery Thu, Jun 26Only 3 left in stock.
Loading...
9-in-1 5000A 150PSI Car Battery Booster Jump Starter with Air Compressor (All Gas/9L Diesel), Portable Car Battery Booster Pack, Safe Durable Car Jump Starter with Extended Jumper Cables, Glove, Light

9-in-1 5000A 150PSI Car Battery Booster Jump Starter with Air Compressor (All Gas/9L Diesel), Portable Car Battery Booster Pack, Safe Durable Car Jump Starter with Extended Jumper Cables, Glove, Light

(261)
$99.99
FREE delivery Sat, Sep 20
1K+ bought in past month