pagespeed-insightsgoogle-pagespeed-insights-api

PageSpeed Insights API V5


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?


Solution

  • 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