DEV Community

Lucy Green
Lucy Green

Posted on

Automating Page Speed Audits: Python Script for Core Web Vitals Monitoring

 Just automated my weekly page speed audit with a Python script that pulls Core Web Vitals data and flags regressions before they hit production. No more surprise Lighthouse drops on Monday mornings.

Here's the core loop:

python
import requests
import json
from datetime import datetime

for url in urls:
response = requests.get
data = response.json()
if data['lcp'] > 2500 or data['cls'] > 0.1:
print(f"ALERT: {url} has poor vitals - LCP: {data['lcp']}ms, CLS: {data['cls']}")

It checks LCP, FID, and CLS against Google's thresholds. If any metric exceeds the limit, it logs the URL with a timestamp. I've got it running as a cron job every 6 hours.

The biggest win? Catching a third-party widget that bloated my TBT from 50ms to 350ms after an update. Rolled back within the hour.

This approach works with any API that exposes raw Web Vitals data. I'm using SERPSpur's Speed Forensics endpoint because it also gives me field data from Chrome UX Report, not just lab metrics.

Anyone else automating performance monitoring? What thresholds do you set for alerts?

Top comments (1)

Collapse
 
micheljee profile image
Michel Jee

Nice work catching that third-party widget regression so fast. I've been meaning to set something similar up—do you find that using field data instead of just lab metrics makes a big difference in catching real user issues before they pile up?