1
votes

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?

1

1 Answers

-2
votes

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).