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

Updated on Friday, September 30, 2022

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.

Add your comment...

More Google Apps Script Projects

(All Code Posts)

(Published to the Fediverse as: Automate Google PageSpeed Insights and Core Web Vitals Logging with Apps Script #code #google #appsscript #gas #pagespeed How to automatically monitor page load performance using the Google PageSpeed Insights API and Apps Script )

Comments

Usama

Ahh so ive just realised the issue I had was that I was using Loadingexperience and not loadingexperience lol.

FYI, in order to access the live metrics of the API you would need to use the below format:

desktop.lighthouseResult.audits["speed-index"].numericValue

I hope that helps someone as it near enough drove me insane finding that out today.

Usama

Hi Rob,

Absolutely fantastic work btw! This is the clearest and simplest script ive seen written for accomplishing this task.

Extremely helpful. I do have one question however.

I notice that the data being extracted is referring to the originLoadingexperience. Am I right in assuming this is using the value from the origin dataset? Is there anyway to use the lighthouse data set for This URL, and not Origin?

I have checked the data on the PageSpeed Insights page by toggling between them, and it seems that the data thats pulled is the Origin set.

I've tried using just Loadingexperience instead, but it doesnt pull anything across.

landed

The API appears to be more lighthouse based data not field data (CRUX), if you compare the two you get very different results. Even the API docs are not clear on what this data represents. But it appears to be the non field data.

Rod

You're right. A personal account won't do. A Workspace account did.

Robert Ellison

Hi Rod - do you use Google Workspace? If so, you could try going to API access in the admin console, find this app and then mark it as trusted (or ask your Workspace Admin to do this if it isn't you). If not then my best guess would be to go to your personal Google Account security settings, revoke any access for the script and then reauthorize.

Rod

Hi Robert,

I keep getting the error below. Do you know what causes it or how to fix?

This app is blocked

This app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked this access.

Robert Ellison

Wessel - the callPageSpeed() runs the test for a URL and device type. If you call this multiple times you can pass in different urls and get results back from each. Depending on the number of URLs you'll likely want to refactor how the results are written out as well.

wessel

Hi Robert, thank you for this! you mentioned it would be easy to monitor for more urls. I'm wondering if you could perhaps show how that is done. I have tried multiple approaches, but the script only gives me results on one url at a time, no matter what I try. Any ideas how to solve this?

Robert Ellison

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

Terry

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

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.

Newsletter

Related

GSC Monitor