Automatically Track PageSpeed Insights Over Time

I have written two detailed guides to improve your website’s page speed (How to Score 100 on Google PSI: A Complete Guide to Optimizing Your WordPress Site Performance and Speed and How to Score 100 on Google PSI: More Lessons Learned). Those discuss multiple online testing tools that are useful, but the core focus was on Google’s PagesPeed Insights tool.

After working to improve your page speed you’ll want to monitor it to make sure things are still performing well. Life being what it is, remembering to regularly run a manual check is not convenient or likely. Ideally, there should be a way to automate the process. Recently, I began to investigate the options and found three options worth considering.

Google API Key

For the potential solutions discussed below you will need an API key. This is something Google provides for free but you will need a Gmail or similar Google account. The layout and process involved in enabling APIs and obtaining an API Key often changes, but the general process is:

  1. Login to Google API Console.
  2. Create a new project. Name the project anything you’d like, such as: PageSpeed Insights. Agree to the terms and service if asked. It may take 10-20 seconds for the new project to be created, be patient. If you have already created a project previously that you want to use, select that project.
  3. Enable the PageSpeed Insights API. After your project has been created, you can enable the google services you want to use from the API Library. Find / Search for “PageSpeed Insights API”, and click it. Enable the PageSpeed Insights API.
  4. Create credentials for using the PageSpeed API from the credentials page. The type of credentials that we want to make is “API Key.” Google will generate an API Key for you to use. Copy it to your clipboard to use it later. You can come back to the credentials page if you misplace it. (Note: You will be given the option to restrict usage of your API key. Be careful that you do not accidentally add a restriction that will block your site form using this key.)

WordPress Google PageSpeed Insights Plugin

The Google Pagespeed Insights WordPress plugin is a pretty impressive tool. It will query PageSpeed Insights for all the pages of your site (beware if you have many). You can sort your page reports by their score to prioritize the largest areas of opportunity on your site. Click on any page to see a detailed report of the results, with recommendations based upon current industry best practices.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

In the options section you can configure the plugin to run reports for desktop, mobile or both. Since the results vary a lot between the two I recommend using both.

One nice feature is the ability to schedule the plugin to run. Unfortunately, I am unable to actually access the scheduling section of the options page (or the advanced options section). I have posted in the support section asking about this so hopefully the plugin author will address it. It may just be an issue specific to my setup, but until resolved it won’t allow for ongoing tracking.

Another concern I have with the plugin is that it seems to automatically check every page on the site. Once you have those results you can click on any page result and choose Ignore to prevent it from running. But, it would be nice to specify which pages you want to run in the main options page. That may actually be an advanced setting, but I have no idea since I cannot access that section.

Google Sheet Monitor for PageSpeed Insights

The more I use Google Sheets the more impressed I become. I have created several public projects, including:

Recently, I came across projects by Robert Ellison and Rick Viscomi that can track PageSpeed Insights. These spreadsheets use the script editor and pivot tables. I haven’t previously used either, so it was a good chance to learn some new things.

Neither of those spreadsheets quite do what I want so I decided to create my own. My spreadsheet uses the best of both and adds new pieces as well. One of the new features is the ability to email yourself the results. I have made a public version that you can copy.

PageSpeed Insights Monitor: Lighthouse and Fast FCP from CrUX – Public
(please don’t ask for editing permission, just use the “Make a copy” option in the “File” menu)

This spreadsheet will track various PageSpeed Lighthouse and CrUX results. You can track one or more websites (useful if you want to compare your site to competitors). If you schedule it to run daily (recommended) it will create charts to track desktop and mobile performance over time.

Modifying the Spreadsheet

After making a copy of the sheet, you will need to modify it a bit to suit your needs. First, use the “Tools” menu to select the “Script editor” option.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Next, edit the pageSpeedApiKey and pageSpeedMonitorUrls variables to use your API key and the URLs that you would like to monitor. Note that PageSpeed Insights now lets you test not only a single page of your site but also the aggregated page speed data across all pages of your site. To test that simply use the syntax origin:https://www.domain.com.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Now edit the email address where you want to send the results of each run.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Running the Script

Finally, save the changes and run the script to verify everything is working properly. There are two ways you can run the script. First, you can use the “Select function” option on the toolbar and choose monitor and then click the right facing triangle (next to the spider icon). This will be useful if you want to run the script often to debug changes you want to make.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Alternatively, just run the monitor script from the “Run” menu.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

However you choose to run the script, you will first need to authorize it. A window will pop up warning you that authorization is required. Just choose the “Review Permissions” button.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Next you will be asked to choose your account.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Now you will be shown a warning that the script isn’t verified. You will need to override this warning by clicking the “Advanced” link at the bottom.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Now just click the “Go to PSI CrUX Monitor (unsafe)” link.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

If the script runs with no errors, view the spreadsheet “Results” sheet. You should see a row entered for each URL you are tracking. Note that one error that might appear while running the script looks like:

This happens if the server hosting a site you are trying to test is configured to block scripts like PageSpeed Insights. If it’s your own URL you will need to investigate with your hosting provider. If it is a competitor URL you may be out of luck. FWIW, I was unable to run the script using either Google or Yahoo when I was trying to test things.

Scheduling Daily Runs

If everything is working well, you will want to set a schedule to have the script run itself automatically. To do so, you need to set “triggers” to run the monitor function daily, which will append a new row for each URL every day. To set that up, go to the “Edit” menu, select “Current project’s triggers.”

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Add a new trigger with the following configuration:

  • Run the monitor function
  • “Time-driven”
  • “Day timer”
  • Select any hour for the script to run, or leave it on the default “Midnight to 1am”
yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time
yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time
yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

After saving the trigger, return to this sheet daily to see the performance stats for all monitored URLs or origins.

Pivot Tables

I am pretty new to pivot tables myself so I am in no position to offer detailed advice or help. Still, one thing that may happen is that your pivot table data won’t populate automatically. This is because it was looking for the URLs that you changed. The way to fix this is to change the “Filters” section on the “Pivot Table Editor” right sidebar. On the “URL” box use the “Status” dropdown box to select all your URLs. Once you do this, the data from the “Result” sheet should populate the rows.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Once you have some days of data the charts should graph the values for each URL in a nice way. The screenshot below is from Rick Viscomi’s version of the script since my own hasn’t yet run for enough days.

yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 - Automatically Track PageSpeed Insights Over Time

Finally, make sure you receive an email after successfully running the script. That should provide convenient push notifications of the results and remind you to check the sheet occasionally.

PHP PageSpeed Insights Monitoring Script

I think the Google Sheets solution I just described is the best option and the WordPress plugin has a lot of promise too, but before I knew about either of those I used a PHP script run with a cron job to monitor my sites. That script was lightly modified from one written by Adam Malone and served me well for the past few years.

That script was written for the first version of Google’s API, which is now on the fifth version. Older versions are ending, which is what prompted me to research a replacement and how I found the other options. I didn’t find any similar script already written for the latest version so I decided to write my own.

The current version of PageSpeed Insights barely resembles the one used a few years ago, for better and worse. Previously, Google spelled out where your page was doing well and where it was falling short, providing useful tips for each. Those tips still sort of exist but in a different, more confusing format. It’s also not clear how those tips tie directly to the score you see on the results page.

I still haven’t mastered all the results that the API returns but I decided to use the ones that the PSI page highlights. Note that many of these are not listed in the results of the Google Sheets tracking as they aren’t numeric. They are, however, given in the reports generated by the WordPress plugin.

All of which is a long-winded way of saying I am not sure how helpful this script will be. It may need some small to big modifications in the future. But, for now, if you are interested, here is the code.

Conclusion

I hope one of the three ways to automate your PageSpeed Insights checks will prove useful. I am writing this post before really trying them out so if you notice any problems please leave a comment and I will investigate.

Like this content? Why not share it?
z927iFhzZhhHlG0vLRrW0rNOtvomSRnScsIUGYEOoGWhQHZCAD51BxKmftlAwAAAABJRU5ErkJggg== - Automatically Track PageSpeed Insights Over TimerCAA+N5JPqrDdnnVjMCDtV3aOq+dynp0fAgEsHNpInPMvAAAAAElFTkSuQmCC - Automatically Track PageSpeed Insights Over TimefYbFK7fndm+e1LH+kbz6ZQER+HeaNohGxgAAAABJRU5ErkJggg== - Automatically Track PageSpeed Insights Over Timeedfvr+8cdvzQdvCndu33HnFTCc55x7yN4E0py04fz9919vvPkMjHMUzRDE2rRZunenXN8uwY5tTA2bJLpBbNGuHYwNIFnetq1ArmzfLs7mLejxDLcfjCBsCBddlqqFAdkIAEZaZD6ak+MlAAAAAElFTkSuQmCC - Automatically Track PageSpeed Insights Over TimekZwb+rrmiWtf3ej011fDyWUdE3D3PX0McITKAQDxIc8AJ3Xhy1ZmirwAAAAAElFTkSuQmCC - Automatically Track PageSpeed Insights Over TimelBqK4mcnbAAAAAElFTkSuQmCC - Automatically Track PageSpeed Insights Over TimenXuolUE1LSK7Ikslnc4VQTCqRhOYZ0nc2hRw3hK+pxpzKp2hqEjVAACJTFcy8WQrEEoj2QfDH+ftT8vfQcs4fyzcHAAAAAASUVORK5CYII= - Automatically Track PageSpeed Insights Over TimeXn19tOURXs4NFOIzRgsKonqLhVu8d2ucV2abpVcWqmk5WdQkpYHIXDapkVhQDYCAEkHQoyIRHVHAAAAAElFTkSuQmCC - Automatically Track PageSpeed Insights Over Time6wBVv3UpPm2PsQAAAABJRU5ErkJggg== - Automatically Track PageSpeed Insights Over Time

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.