PageSpeed Insights API - Track Page Speed for Free
Tags: webperf, webdev, pagespeed, seo
Published: 2020-01-31 17:51:15


There are many ways that you can track your website's PageSpeed. You could integrate Google Lighthouse into your CI pipeline. You could use a paid service to track performance for you.

Did you know that you can track it for free?

You can use Google Sheets and the PageSpeed Insights API to track the performance daily.

I have been using it to track the [performance of the top 10 websites in the US](https://pagedart.com/blog/benchmark-websites/):

![Top 10 US Website Page Speed](https://dev-to-uploads.s3.amazonaws.com/i/9k523yjmqcs6ascbe6kx.png)

It has been running for over 5 months. The graph above shows that Youtube and Reddit's performance is getting worse! This data is useful to see how you are improving site performance over time.

In this post, we are going to set up your own Google Sheet. It will track key pages on your website using the lighthouse performance score.
## What is PageSpeed Insights?
[PageSpeed Insights](https://developers.google.com/speed/pagespeed/insights) is a free web tool from Google. It allows you to analyze the performance of a website. Enter a page URL into the analyze box and click analyze. Google will then read your page and come back with some results.

Here are the results from https://dev.to a score of 91 is very impressive:

![Dev.to Lighthouse Performance Results](https://dev-to-uploads.s3.amazonaws.com/i/wn62woraifsuck968pqu.png)

The 91 is the Lighthouse performance on mobile this is the default score that is shown. Lighthouse is another free tool from Google that runs a few different types of audits on your site. The one we are focusing on here is the performance audit.

To get the lighthouse score it will run through a series of audits and present a score from 0-100. So this is not a metric in time but a performance score. With 100 being the fastest and 0 being the slowest. 

You want to be in the 90s if you can and you should set yourself this goal. One of the main reasons that you need a fast site is that it will increase conversion. It will also help with your Technical SEO. If Google sees that your site is fast it will rank better in their Search results.

The PageSpeed Insights Tool is good at running a one-off test but how can we use it to track performance over time.

Well, they have an API.

## PageSpeed Insights API
You can get the same data in JSON for free by using the [PageSpeed Insights API](https://developers.google.com/speed/docs/insights/v5/get-started). So that we can set up the spreadsheet later you need an API key. To do this first visit the [getting started page](https://developers.google.com/speed/docs/insights/v5/get-started).

Once on the getting started page scroll down to the [Optional: Set up an API Key](https://developers.google.com/speed/docs/insights/v5/get-started#key) section.

![Set up an API Key](https://dev-to-uploads.s3.amazonaws.com/i/8s451tjo3aaaoh9b5ysn.png)

Click on the “Get a Key” button and a pop up will appear:

![New key pop over](https://dev-to-uploads.s3.amazonaws.com/i/wx2v0twadih1yfezd2g5.png)

Select “Create a new project” name the project “PageSpeed” and click “Next”:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ex280qidyob9tdc88vi3.png)

You will then see your API Key:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tocxwrtz2uwlvyz5hr2p.png)

Make a note of this API key as we will need it later when you copy the Google Sheet.

Let’s look at this next.

## PageSpeed Tracking with Google Sheets
Now we have set up an API key for PageSpeed Insights you can make a copy of the [“Track Page Speed for Free”](https://docs.google.com/spreadsheets/d/149MDBLNDcKLABX0vs7XBsAi-0Yg7HbI6EgGNRI4Ez2A/edit?usp=sharing) Google Sheet.

Open the Sheet and then choose “File” and “Make a copy…".

This will make a copy of the sheet and move it to your Google Drive account.

We are now only three steps away from this working for you:

1. Set the API key in the script
1. Set up the page URLs that you want to track
1. Create a trigger to run the script daily

Let’s start with setting the API Key.

### 1) Set the API key in the script

Now that you have a copy of the Google Sheet we can change the script and add the API key that we grabbed earlier. 

To do this we need to open the script editor in Google Sheets. To do this choose “Tools” -> “Script Editor” from the Goole Sheets menu:

![Open the Script Editor](https://dev-to-uploads.s3.amazonaws.com/i/8chgemq3nxyjjti30j3x.png)

This will open up a new tab showing the script. It is JavaScript and is easy to edit so that you can track your pages.

We first need to change a script property called “PSI_API_KEY”. This is like an environment variable in code. It uses this property when it makes a call to the PageSpeed Insights API.

So in the script editor click on the File menu and then select “Project properties”:

![Project Properties](https://dev-to-uploads.s3.amazonaws.com/i/7ds0bqbf8u05gt1dds76.png)

This will open up a pop-up, select the “Script properties” tab and then click on the “Add row” button.

Give the new row the name of “PSI_API_KEY” and the value is the API key you copied from earlier.

![API Key added to the script](https://dev-to-uploads.s3.amazonaws.com/i/qpgep4lw6l4fll33e7c7.png)

Once done, click save and we can move on to step 2.
### 2) Set up the page URLs that you want to track

In the script editor, you can see the JavaScript code that runs the test. There is an array that looks like this:

```
var pageSpeedMonitorUrls = [
  'https://www.google.com',
  'https://www.youtube.com',
  'https://www.amazon.com',
  'https://www.facebook.com',
  'https://www.yahoo.com',
  'https://www.reddit.com',
  'https://www.wikipedia.org',
  'https://www.bing.com',
  'https://www.ebay.com',
  'https://www.netflix.com'
];
```

To track your pages you can replace these URLs with your own. Do this now and we can move on to the final step.
### 3) Create a trigger to run the script daily

The final thing to configure is the trigger that will run the script every night. So back in the script editor click on the “Edit” menu and then select “Current project’s triggers”:

![Open Current Project triggers](https://dev-to-uploads.s3.amazonaws.com/i/zwta3641q3zggy5am9co.png)

This will open up a new tab showing the triggers for the project. As this is a new project the triggers should be empty. 

Go ahead and click the “Add Trigger” button and you will get a pop-up. Configure the “monitor” function to run daily and from midnight by selecting the set up as below:

![Add trigger](https://dev-to-uploads.s3.amazonaws.com/i/vvzmtz4ipn5udtxe4un7.png)

Then click Save. 

Once you have finished this the script is ready to run every night. 
## Wrapping Up, PageSpeed Insights: Track Page Speed for Free
You have learned a few things in this post:

* What is PageSpeed Insights?
* How you can use the PageSpeed Insights API
* How to use Google Lighthouse Performance Report
* How to use Google Sheets to run a custom script
* How to use triggers to run a script daily

Putting all this together you can track the performance of key pages on your website over time. 

There are [many ways to improve your site performance](https://pagedart.com/blog/30-ways-to-improve-your-website-performance/) but the first step is to measure it. This will give you feedback when you make changes so that you can see the site improve.