Find score inside json result of PageSpeed Insights API v5
Asked Answered
E

4

15

I'm trying the Google v5 API page speed insight but i don't find the SCORE inside the JSON result.

This is the api https://www.googleapis.com/pagespeedonline/v5/runPagespeed

In the v4 there is a ruleGroups.SPEED.score that contains an integer with the score.

Where i can find the score inside the v5?

Ebracteate answered 13/11, 2018 at 22:52 Comment(0)
H
16

I think it is the following: json.lighthouseResult.categories.performance.score

Returns a decimal with 1 as maximum. So you have to multiply with 100 to get the percentage. Works for me.. However I don't seem to get the same value every time. It fluctuates...

Hypnogenesis answered 17/11, 2018 at 22:39 Comment(4)
That could not be correct. I've checked this with a few pages and json.lighthouseResult.categories.performance.score is much higher than the score from developers.google.com/speed/pagespeed/insightsMalvia
That's the correct answer. I tested it over couple of websites and it is correct. Thank you.Assuntaassur
To deal with fluctuating scores, run the audit 5 times and take the median value. Helps to deal with outliers in audit runs.Unboned
Does anyone know how to get a unique report URL from the JSON response from the API, to view externally?Jesuitism
G
20

It is json.lighthouseResult.categories.performance.score as described by Jeroen.

You can return all possible audit categories using the following example:

  • URL: https://www.google.com/
  • Return-Fields: lighthouseResult/categories/*/score
    • * is a wildcard
  • Indentations (PrettyPrint): no
  • Strategy: Desktop
  • Categories:
    • Performance
    • Progressive Web App (PWA)
    • Best practices
    • Accessibility
    • SEO
  • Api-Key: {YOUR_API_KEY}
    With these parameters, the API URL is:

    https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fstackoverflow.com%2F&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=desktop&category=performance&category=pwa&category=best-practices&category=accessibility&category=seo&key={YOUR_API_KEY}


And the JSON-Response looks like this:
{
    "lighthouseResult": {
        "categories": {
            "performance": {
                "score":0.99
            },
            "accessibility": {
                "score":0.7
            },
            "best-practices": {
                "score":0.77
            },
            "seo": {
                "score":0.9
            },
            "pwa": {
                "score":0.56
            }
        }
    }
}
Greatuncle answered 1/2, 2019 at 9:15 Comment(2)
I am looking for showing both Strategy ( mobile and desktop ) performance score in single api hit. Is there any we can do that ?Harleyharli
It seems at the moment is only delivers the Desktop score. How can I get just the mobile ones?Commissar
H
16

I think it is the following: json.lighthouseResult.categories.performance.score

Returns a decimal with 1 as maximum. So you have to multiply with 100 to get the percentage. Works for me.. However I don't seem to get the same value every time. It fluctuates...

Hypnogenesis answered 17/11, 2018 at 22:39 Comment(4)
That could not be correct. I've checked this with a few pages and json.lighthouseResult.categories.performance.score is much higher than the score from developers.google.com/speed/pagespeed/insightsMalvia
That's the correct answer. I tested it over couple of websites and it is correct. Thank you.Assuntaassur
To deal with fluctuating scores, run the audit 5 times and take the median value. Helps to deal with outliers in audit runs.Unboned
Does anyone know how to get a unique report URL from the JSON response from the API, to view externally?Jesuitism
H
1

"I think it is the following: json.lighthouseResult.categories.performance.score

Returns a decimal with 1 as maximum. So you have to multiply with 100 to get the percentage. Works for me.. However I don't seem to get the same value every time. It fluctuates..." as Jeroen say. 100% correct

"That could not be correct. I've checked this with a few pages and json.lighthouseResult.categories.performance.score is much higher than the score from developers.google.com/speed/pagespeed/insights – Pascal Bajorat"

It is not correct because you see Desktop result not mobile result. Desktop in 90% case 99 or 100.

Try this:

URL: https://www.google.com/
Return-Fields: lighthouseResult/categories/*/score

* is a wildcard

Indentations (PrettyPrint): no
Strategy: Mobile
Categories:
  Performance
  Progressive Web App (PWA)
  Best practices
  Accessibility
  SEO

With these parameters, the API URL is:

https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fstackoverflow.com%2F&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=mobile&category=performance&category=pwa&category=best-practices&category=accessibility&category=seo

And the JSON-Response looks like this:

{
"lighthouseResult": {
    "categories": {
        "performance": {
            "score":0.87
        },
        "accessibility": {
            "score":0.7
        },
        "best-practices": {
            "score":0.77
        },
        "seo": {
            "score":0.9
        },
        "pwa": {
            "score":0.56
        }
    }
}

}

Hangnail answered 22/7, 2021 at 11:35 Comment(0)
F
0

I created simple php script based on previous answers that can help you find desktop and mobile score. Just provide your site url and API key that you can find here

<?php

$key = '';
$url = '';

$mobile = find_score( $url, 'mobile', $key );
$desctop = find_score( $url, 'desktop', $key );

echo "Mobile: " . $mobile . '; ';
echo "Desctop: " . $desctop . '; '; 

/**
 * Find PSI api score for certain device of certain url
 *
 * @param string $url
 * @param string $device Possible values: desctop, mobile
 * @param string $key
 *
 * @return string | integer
 */
function find_score( $url, $device, $key = '' ) {

    $url = 
        "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" . $url . "&category=performance&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=" . $device . "&key=" . $key;

    $init = curl_init();

    curl_setopt($init, CURLOPT_URL, $url);
    curl_setopt($init, CURLOPT_RETURNTRANSFER, true);

    $responce = curl_exec( $init );
    curl_close($init);

    $responce = json_decode( $responce );

    if ( ! empty( $responce->lighthouseResult->categories->performance->score ) ) {
        $score = $responce->lighthouseResult->categories->performance->score;
        $score = $score * 100;
    } else {
        $score = 'API Error';
    }

    return $score;
}
Fustigate answered 25/4, 2021 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.