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

Updated on Sunday, August 15, 2021

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, FID and CLS):

You need a spreadsheet with a tab called results and an API key for PageSpeed Insights (activate the API in the console and create an API key for it, the browser based / JavaScript option). Paste the code above into the script editor for the spreadsheet and add your API key and URL 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), first input delay (FID, 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 in the script.

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

There are a lot of other values returned (like number and types of resources on the page) 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.

More Google Apps Script Projects

(All Code Posts)

Comments

Mike Wong

Hello Robert,

This is just what I've been looking for. A way to automate the collection/export of the Google Analytics PageSpeed report.

I'm fairly new to programming - okay, very new - and I'm running into a problem. Hoping you might be able to assist me.

I've created a Google API key from the console as you instructed. I then launched the Script Editor from within my Google Sheet and I pasted your above script into the editor, replacing everything that shows up in the Script Editor by default.

After adding my API key and my URL to monitor, I ran the script and I receive the following error:

"Request failed for https ://www.googleapis.com/pagespeedonline/v1/runPagespeed?"

I looked up that error message and I found the page (

Pagespeedapi: runpagespeed) - which references v4 instead of v1, but I still get the error if I use v4.

If you have any suggestions on how to resolve this, I would be incredibly grateful.

Best,

Mike

Robert Ellison

Hi Mike, I think that version 1 of the API was deprecated. It was still working for me but probably because I've had the API key for long enough. I've just updated the script to use v5. The two changes are the v5 in the PageSpeed API URL and the score has moved to lighthouseResult.categories.performance.score and is from 0-1 rather than 0-100. I multiply by 100 above to make it consistent with the old score. Let me know if you're still having any problems after making these changes.

Augusto

Hi, Robert.

I´m trying to add more features to this script. I tried adding

desktop.lighthouseResult.audits.network-rtt.id,

but it returns

TypeError: Cannot read property "items" from undefined. (line 9, file "Code")Dismiss

could you help me?

Robert Ellison

Go to PageSpeed Insights, click Docs and then API Reference. There is a Try this API pane where you can make a call to the API and browse the JSON. This is really helpful for figuring out the response structure.

Mike Wong

Thank you Robert!

Terry

Are you available for freelance? I have a monitoring project.

Robert Ellison

Terry, sorry, I'm not. Happy to help with any questions I can answer in comments.

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.

I Thought He Came With You is Robert Ellison's blog.

Newsletter

Related

Get an email if your site stops being mobile friendly