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
The authentication process is straightforward. All you need is an account on Google Developer Console (which you can get via this link). From your account, start a new project from the homepage.
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
Once you have your credentials, you will be able to construct your first API call and begin experimenting with automated site speed testing. Grab a list of URLs that you want to test (either from Screaming Frog, an XML sitemap or wherever works best for you) and we can begin. For ease of access, I have coded the API into a simple UI that allows for ease of access for those who are not familiar with coding. I will be providing instructions for using the API either with or without the UI.
Working with the User Interface
The UI wrapper for the PageSpeed Insights API uses EasyGUI, an easy to use interface which relies on a linear series of interactions rather than a more experiential software application. While the backing is simple, the ability to use the API without having to hardcode your inputs/output paths has its benefits in terms of streamlining the process.
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
For those who want additional metrics from PageSpeed, or want to just tinker around with the source code I’ve written out, there are two parts to working with the API which I would emphasize.
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
The PageSpeed API is one of my absolute favorite offerings by Google. With the ability to make multiple data queries and retrieve so much information about either your own or a competitors site in an automated fashion, it has become a major asset in my technical SEO toolbelt. For those who want to make it a part of their own, the link to the file is on my GitHub. As you begin running your automated speed tests, feel free to reach out to me personally on my LinkedIn or Twitter for any questions about the script. But overall, happy speed testing!