<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Steve</title>
    <description>The latest articles on Forem by Steve (@pagedart).</description>
    <link>https://forem.com/pagedart</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F327106%2Fa5fc86f4-0b7a-4926-8e23-fbdb62898250.jpg</url>
      <title>Forem: Steve</title>
      <link>https://forem.com/pagedart</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pagedart"/>
    <language>en</language>
    <item>
      <title>PageSpeed Insights API - Track Page Speed for Free</title>
      <dc:creator>Steve</dc:creator>
      <pubDate>Fri, 31 Jan 2020 17:51:15 +0000</pubDate>
      <link>https://forem.com/pagedart/pagespeed-insights-api-track-page-speed-for-free-2h1j</link>
      <guid>https://forem.com/pagedart/pagespeed-insights-api-track-page-speed-for-free-2h1j</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Did you know that you can track it for free?&lt;/p&gt;

&lt;p&gt;You can use Google Sheets and the PageSpeed Insights API to track the performance daily.&lt;/p&gt;

&lt;p&gt;I have been using it to track the &lt;a href="https://pagedart.com/blog/benchmark-websites/" rel="noopener noreferrer"&gt;performance of the top 10 websites in the US&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9k523yjmqcs6ascbe6kx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9k523yjmqcs6ascbe6kx.png" alt="Top 10 US Website Page Speed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is PageSpeed Insights?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developers.google.com/speed/pagespeed/insights" rel="noopener noreferrer"&gt;PageSpeed Insights&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;Here are the results from &lt;a href="https://dev.to"&gt;https://dev.to&lt;/a&gt; a score of 91 is very impressive:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwn62woraifsuck968pqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwn62woraifsuck968pqu.png" alt="Dev.to Lighthouse Performance Results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

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

&lt;p&gt;Well, they have an API.&lt;/p&gt;

&lt;h2&gt;
  
  
  PageSpeed Insights API
&lt;/h2&gt;

&lt;p&gt;You can get the same data in JSON for free by using the &lt;a href="https://developers.google.com/speed/docs/insights/v5/get-started" rel="noopener noreferrer"&gt;PageSpeed Insights API&lt;/a&gt;. So that we can set up the spreadsheet later you need an API key. To do this first visit the &lt;a href="https://developers.google.com/speed/docs/insights/v5/get-started" rel="noopener noreferrer"&gt;getting started page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once on the getting started page scroll down to the &lt;a href="https://developers.google.com/speed/docs/insights/v5/get-started#key" rel="noopener noreferrer"&gt;Optional: Set up an API Key&lt;/a&gt; section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8s451tjo3aaaoh9b5ysn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8s451tjo3aaaoh9b5ysn.png" alt="Set up an API Key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the “Get a Key” button and a pop up will appear:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwx2v0twadih1yfezd2g5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwx2v0twadih1yfezd2g5.png" alt="New key pop over"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select “Create a new project” name the project “PageSpeed” and click “Next”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fex280qidyob9tdc88vi3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fex280qidyob9tdc88vi3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will then see your API Key:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftocxwrtz2uwlvyz5hr2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftocxwrtz2uwlvyz5hr2p.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make a note of this API key as we will need it later when you copy the Google Sheet.&lt;/p&gt;

&lt;p&gt;Let’s look at this next.&lt;/p&gt;

&lt;h2&gt;
  
  
  PageSpeed Tracking with Google Sheets
&lt;/h2&gt;

&lt;p&gt;Now we have set up an API key for PageSpeed Insights you can make a copy of the &lt;a href="https://docs.google.com/spreadsheets/d/149MDBLNDcKLABX0vs7XBsAi-0Yg7HbI6EgGNRI4Ez2A/edit?usp=sharing" rel="noopener noreferrer"&gt;“Track Page Speed for Free”&lt;/a&gt; Google Sheet.&lt;/p&gt;

&lt;p&gt;Open the Sheet and then choose “File” and “Make a copy…".&lt;/p&gt;

&lt;p&gt;This will make a copy of the sheet and move it to your Google Drive account.&lt;/p&gt;

&lt;p&gt;We are now only three steps away from this working for you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set the API key in the script&lt;/li&gt;
&lt;li&gt;Set up the page URLs that you want to track&lt;/li&gt;
&lt;li&gt;Create a trigger to run the script daily&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start with setting the API Key.&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Set the API key in the script
&lt;/h3&gt;

&lt;p&gt;Now that you have a copy of the Google Sheet we can change the script and add the API key that we grabbed earlier. &lt;/p&gt;

&lt;p&gt;To do this we need to open the script editor in Google Sheets. To do this choose “Tools” -&amp;gt; “Script Editor” from the Goole Sheets menu:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8chgemq3nxyjjti30j3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8chgemq3nxyjjti30j3x.png" alt="Open the Script Editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So in the script editor click on the File menu and then select “Project properties”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ds0bqbf8u05gt1dds76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ds0bqbf8u05gt1dds76.png" alt="Project Properties"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will open up a pop-up, select the “Script properties” tab and then click on the “Add row” button.&lt;/p&gt;

&lt;p&gt;Give the new row the name of “PSI_API_KEY” and the value is the API key you copied from earlier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqpgep4lw6l4fll33e7c7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqpgep4lw6l4fll33e7c7.png" alt="API Key added to the script"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once done, click save and we can move on to step 2.&lt;/p&gt;

&lt;h3&gt;
  
  
  2) Set up the page URLs that you want to track
&lt;/h3&gt;

&lt;p&gt;In the script editor, you can see the JavaScript code that runs the test. There is an array that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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'
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To track your pages you can replace these URLs with your own. Do this now and we can move on to the final step.&lt;/p&gt;

&lt;h3&gt;
  
  
  3) Create a trigger to run the script daily
&lt;/h3&gt;

&lt;p&gt;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”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzwta3641q3zggy5am9co.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzwta3641q3zggy5am9co.png" alt="Open Current Project triggers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will open up a new tab showing the triggers for the project. As this is a new project the triggers should be empty. &lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvvzmtz4ipn5udtxe4un7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvvzmtz4ipn5udtxe4un7.png" alt="Add trigger"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then click Save. &lt;/p&gt;

&lt;p&gt;Once you have finished this the script is ready to run every night. &lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up, PageSpeed Insights: Track Page Speed for Free
&lt;/h2&gt;

&lt;p&gt;You have learned a few things in this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is PageSpeed Insights?&lt;/li&gt;
&lt;li&gt;How you can use the PageSpeed Insights API&lt;/li&gt;
&lt;li&gt;How to use Google Lighthouse Performance Report&lt;/li&gt;
&lt;li&gt;How to use Google Sheets to run a custom script&lt;/li&gt;
&lt;li&gt;How to use triggers to run a script daily&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting all this together you can track the performance of key pages on your website over time. &lt;/p&gt;

&lt;p&gt;There are &lt;a href="https://pagedart.com/blog/30-ways-to-improve-your-website-performance/" rel="noopener noreferrer"&gt;many ways to improve your site performance&lt;/a&gt; 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.&lt;/p&gt;

</description>
      <category>webperf</category>
      <category>webdev</category>
      <category>pagespeed</category>
      <category>seo</category>
    </item>
  </channel>
</rss>
