2

I am trying to integrate PageSpeed Insights API in my wordpress website, so when ever customer comes, he can check his website speed using pagespeed insight. Basically i want to put a textbox with button (like this https://developers.google.com/speed/pagespeed/insights/ ) which uses google page speed insight api or function and display speed result in my website.... Is it possible? if yes how can i do this?

user3578065
  • 31
  • 1
  • 6

2 Answers2

0

I had the same senario and also wanted to integrate the Google PageSpeed Insights API in my Wordpress website. I have developed a paid Wordpress plugin as a solution. To be clear; it's not free, but if you are interessted, you can check it on the Codester marketplace.

PWijnberg
  • 81
  • 1
  • 2
  • 5
-2

Yes this is possible. You can fetch() this endpoint with the URL encoded: https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=

Edit: Of course, you could also AJAX to CURL too (or even CURL on pageload).

Here is the documentation: https://developers.google.com/speed/docs/insights/v5/get-started

I have implemented this without needing an API key, but your mileage may vary.

Here is some sample JavaScript:

fetch('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' + encodeURIComponent('https://example.com/')).then(function(response){
    return response.json(); //This returns a promise
}).then(function(json){
    if ( json && json.captchaResult === 'CAPTCHA_NOT_NEEDED' ){
        //Output the data you want to display on the front-end from the json
    }
});

The API is rate limited, so you'll probably want to cache the results for a certain period of time (I use WordPress transients for this).

GreatBlakes
  • 3,971
  • 4
  • 20
  • 28