Automate Google PageSpeed Insights and Core Web Vitals (CrUX) Logging with Apps Script

By . Updated on

Upload

Here's a quick script to automatically monitor your Google PageSpeed Insights desktop and mobile scores for a web page, together with core web vitals (LCP, INP and CLS):

You need a spreadsheet with a tab called results and an API key for PageSpeed Insights and the CrUX API (activate the APIs in the console and create an API key for them, the browser based / JavaScript option). Paste the code above into the script editor for the spreadsheet and add your API key and URLs to monitor. Then just choose triggers from the Resources menu and schedule the monitor function to run once per day.

The script will log the overall PageSpeed score out of 100 for the monitored page. It also logs 75th percentile origin level core web vitals (largest contentful paint (LCP, seconds), interaction to next paint (INP, seconds) and cumulative layout shift (CLS, percent)). If your origin does not have enough data the metric will be omitted. You can change from origin to page level web vitals if you have enough data, just change originLoadingExperience to loadingExperience for PageSpeed Insights and origin to url in the CrUX call.

The results are repeated for desktop and mobile, so your spreadsheet header should be Desktop PSI, Desktop LCP, Desktop INP, Desktop CLS, Mobile PSI, Mobile LCP, Mobile INP, Mobile CLS.

There are a lot of other values returned that you could choose to monitor as well. It would also be easy to extend this to monitor more URLs, or to send you an email if the score drops below a threshold.

Updated May 5, 2019 to use version 5 of the PageSpeed API.

Updated June 13, 2021 to include core web vitals.

Updated October 18, 2025 to use CrUX instead of PageSpeed for core web vitals (Google is removing web vitals from PageSpeed). CrUX might not have device level data for your origin and so the script will now report total aggregate data for both devices if this is the case. I ignore tablet, easy to add if you want it.