1

I want to integrate Google mobile friendly and desktop friendly api with PageSpeed Insights API V5. But I'm unable to differentiate Audit section. I tried too scenario's for differntiate but I couldn't.

How to differentiate Passed Audits, Diagnostics and Opportunities in PageSpeed Insights API V5?

2 Answers2

2

Below is the code which GoogleChrome lighthouse uses to differentiate Opportunities, Diagnostics and Passed audits which you can find at below github link.

// Opportunities
const opportunityAudits = category.auditRefs
        .filter(audit => audit.group === 'load-opportunities' && !Util.showAsPassed(audit.result))
        .sort((auditA, auditB) => this._getWastedMs(auditB) - this._getWastedMs(auditA));

// Diagnostics
const diagnosticAudits = category.auditRefs
        .filter(audit => audit.group === 'diagnostics' && !Util.showAsPassed(audit.result))
        .sort((a, b) => {
          const scoreA = a.result.scoreDisplayMode === 'informative' ? 100 : Number(a.result.score);
          const scoreB = b.result.scoreDisplayMode === 'informative' ? 100 : Number(b.result.score);
          return scoreA - scoreB;
        });

// Passed audits
const passedAudits = category.auditRefs
        .filter(audit => (audit.group === 'load-opportunities' || audit.group === 'diagnostics') &&
            Util.showAsPassed(audit.result));

Reference : https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/report/html/renderer/performance-category-renderer.js

In above code the Util.showAsPassed() method has been specified as below.

const PASS_THRESHOLD = 0.9;
const RATINGS = {
  PASS: {label: 'pass', minScore: PASS_THRESHOLD},
  AVERAGE: {label: 'average', minScore: 0.5},
  FAIL: {label: 'fail'},
  ERROR: {label: 'error'},
};

static showAsPassed(audit) {
    switch (audit.scoreDisplayMode) {
      case 'manual':
      case 'notApplicable':
        return true;
      case 'error':
      case 'informative':
        return false;
      case 'numeric':
      case 'binary':
      default:
        return Number(audit.score) >= RATINGS.PASS.minScore;
    }
  }

Reference : https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/report/html/renderer/util.js

Thanks

J-D
  • 1,575
  • 1
  • 11
  • 22
1

** Opportunities:** There is no compiled set of opportunities available in the response but we can loop through lighthouseResult and for each json inside the lighthouseReslt, take out the results with type=opportunity and it should also have details.

response = requests.get('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url='+<url>+'&strategy='+<desktop or mobile>+'&key='+<api_key>)
js = response.json()

print ("Score: ",js['lighthouseResult']['categories']['performance']['score']*100)

k = []

for m in js['lighthouseResult'] :

    try :
        for i in js['lighthouseResult'][m] :
            k.append([js['lighthouseResult'][m][i]['details'],js['lighthouseResult'][m][i]['title']])
    except :
        pass

final_opportunities = []

print (len(k))
for i in k :
    if 'overallSavingsMs' in list(i[0].keys()) :

        print (i[1],i[0]['overallSavingsMs'])
        final_opportunities.append([i[1] , i[0]['overallSavingsMs']])

Audit results can be found in : lighthouseResult.audits

The aggregate performance score : response.lighthouseResult.categories.performance.score

For a python implementation, you could refer to the following github repo: https://github.com/amartya-dev/PageSpeedAPI

Amartya Gaur
  • 665
  • 6
  • 21