Integrate Google PageSpeed Insights API

1374

Do you know, Google provides a free PageSpeed Insights API to analyze websites? It gives free access to performance monitoring of web pages and returns data with suggestions for how to improve.

Using this API, we can build any system that analyzes the given URLs and displays the grading of the websites. It provides a lot of information (more than you expect 😉 ). So, we need to extract the data that we need from the response.

In this article, we will integrate Google PageSpeed Insights API in PHP and extract some of the data for testing. Without delay, let’s write some code.

First of all, we need get the API URL where we will send the data using GET method. We can to pass different optional parameters according to our need. We will call the below API URL.

https://www.googleapis.com/pagespeedonline/v5/runPagespeed

We need to pass url and other optional parameters. Here, url means the URL of website we need to analyze. The list of accepted parameters are as follows:

  • url is the website url we need to analyze
  • category indicates the information of categories we need. The accepted values are accessibility, best-practices, performance, pwa and seo.
  • strategy is the medium of analysis. Accepted value are desktop and mobile.
  • You can also set other parameters such as locale, utm_campaign and utm_source.

Note: We can pass API key to execute multiple requests at once.

Let’s execute the code using CURL. In the below code, we simply set the new URL with parameters and send request to the API route. The response is stored in $data variable.

The response from the API is pretty large. We need to extract the information we need. For this, we will first convert the json object to array.

Most of our data is stored in audits, categories, and categoryGroups keys. So, we can get the desired values giving the correct key path.

To store all our required information, let’s create an array named $response. Below is a complete sample of how to access and store API response information.

Read More Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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