Automating SEO Site Speed Analysis Using the PageSpeed API

Whether your site is 5 or 50,000 pages, the way your site loads in content and backend code can be one of the most significant factors in your site performing strongly in organic search. Google over the past few years has been continuing to emphasize the importance of page speed in SEO ranking, going so far as to have entire sections of the Webmaster Blog associated with site speed. Between mobile’s continual influence in organic search (and the many, MANY variables mobile faces with loading content) and site complexity in their builds continue to grow, having a streamlined method for understanding your site’s loading capabilities.

Thankfully, Google has a plethora of methods for understanding how your site stands in load time and speed. On the quick and easy end is the frontend facing, web-hosted PageSpeed Insights. Many SEOs already live and breath by this site (myself included). Simply plug in the URL you want to check and get back all the metrics you are looking for across both mobile and desktop. But, going through the tedious process of copying and pasting URLs into the site, waiting for the test to be conducted and then recording the results you want onto a separate file is very time-consuming. On the other hand, Google Analytics provides site speed monitoring capabilities built into the platform to showcase the entirety of a site. But, for the many sites online who are utilizing other web analytics monitoring platforms or for SEOs who want to conduct competitive analysis on other site’s speed performance, this option is out of the question. So, how can we scale what Google has already provided without having to radically change technology?

Google’s PageSpeed Insights API is an amazing, easy to use, and scalable method for automating the site speed testing process for any site. Utilizing a REST framework, the API is designed to be flexible for all use case scenarios whether it is for your site initiatives or for conducting competitive analysis. Using Python (though this API works across many other programming languages), we’ll highlight how to leverage the API over multiple URLs in an automated fashion.

Authenticating the PageSpeed Insights API

Once you create the project, go to the API Library and search for “PageSpeed Insights API.” Click “Enable API” which should generate the API key you’ll need to start.

Using the API

Working with the User Interface

When using the interface, the only coding you will have to do is inputting your key into the code itself. You can do so on line 13.

Once you save that into the code, you can run the whole script and run through the interface to do your testing. You’ll have the option to either submit an Excel file (with the first cell labeled “URL”) or via copying and pasting URLs into the program.

The site speed testing will happen in the background while you are free to do other tasks or just stare at your screen being proud of bringing some new automation to your job! Once the script is finished running, you will be prompted to save a .xlsx file containing the data. Your output should look something like this. The file will have three different speed metrics across desktop and mobile. For more information about each of these metrics, refer to the PageSpeed Insights documentation.

Without the User Interface

Constructing the URLs/Making the API Call

The URL building is done in a function using Urllib, which encodes out the URL into a REST-friendly format while also allowing for flexibility. Once the URL is developed, you can call using requests to receive the nested JSON output file.

Retrieving the Data from the JSON File

Every successful API call outputs a JSON file constructed something like this.

You can find a wealth of data within this output, from how the test loaded into the results. While my script takes only three site speed metrics, there are over 40 metrics which come with each call including DOM sizing, every web resource on the tested page, and speed optimization recommendations. Navigating the JSON file can be intensive, but very rewarding for the inquisitive SEO who wants to maximize their data. An example of how I have navigated the data output can be seen below. Starting with my main output (“Response”), I had dug into the specific data points needed to pull out the numeric values given by the API.

Conclusion

SEO Manager for @reprisedigital | SEO/SEM/Linguistic Marketing | Writer | Programmer | Start-up Enthusiast | Sneakerhead

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store