Working with results | lighthouse-php | Spatie

 SPATIE

  Lighthouse PHP
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Lighthouse-php](https://spatie.be/docs/lighthouse-php/v2)  Usage  Working with results

 Version   v2

 Other versions for crawler [v2](https://spatie.be/docs/lighthouse-php/v2)

- [ Introduction ](https://spatie.be/docs/lighthouse-php/v2/introduction)
- [ Support us ](https://spatie.be/docs/lighthouse-php/v2/support-us)
- [ Requirements ](https://spatie.be/docs/lighthouse-php/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/lighthouse-php/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/lighthouse-php/v2/questions-issues)
- [ Changelog ](https://spatie.be/docs/lighthouse-php/v2/changelog)
- [ About us ](https://spatie.be/docs/lighthouse-php/v2/about-us)

Usage
-----

- [ Generating your first report ](https://spatie.be/docs/lighthouse-php/v2/usage/generating-your-first-report)
- [ Configuring a run ](https://spatie.be/docs/lighthouse-php/v2/usage/configuring-a-run)
- [ Working with results ](https://spatie.be/docs/lighthouse-php/v2/usage/working-with-results)
- [ Saving an HTML report ](https://spatie.be/docs/lighthouse-php/v2/usage/saving-an-html-report)
- [ Using performance budgets ](https://spatie.be/docs/lighthouse-php/v2/usage/using-performance-budgets)

 Working with results
====================

###  On this page

1. [ Getting the category scores ](#content-getting-the-category-scores)
2. [ Getting audit results ](#content-getting-audit-results)
3. [ Performance metrics convenience methods ](#content-performance-metrics-convenience-methods)
4. [ Getting the configuration used to generate the result ](#content-getting-the-configuration-used-to-generate-the-result)
5. [ Getting the raw results ](#content-getting-the-raw-results)
6. [ Getting the benchmark index ](#content-getting-the-benchmark-index)

Running Lighthouse will return an instance of `Spatie\Lighthouse\LighthouseResult`. On this page, we'll cover the various methods you can call on `LighthouseResult`

```
use Spatie\Lighthouse\Lighthouse;

// instance of `Spatie\Lighthouse\LighthouseResult`
$result = Lighthouse::url('https://example.com');
```

Getting the category scores
-----------------------------------------------------------------------------------------------------------------------------------------

You can use the `scores` method to get scores of the five categories Lighthouse runs audits for.

```
$result->scores(); // returns an array like this one:
/*
 * [
 *    'performance' => 98,
 *    'accessibility' => 83,
 *    'best-practices' => 90,
 *    'seo' => 92,
 *    'pwa' => 43,
 * ]
 */
```

Getting audit results
-----------------------------------------------------------------------------------------------------------------------

Lighthouse will run various audits on your website. You can learn more about the meaning of each audit in [the Lighthouse docs](https://developer.chrome.com/docs/lighthouse/overview).

You can get an array with names of all audits using the `auditNames()` method.

```
$allAuditNames = $result->auditNames();
```

Here's how you can get the results of an audit:

```
$result->audit('first-contentful-paint');

/*  returns an array like this one.
 *
 * [
 *     'id' => 'first-contentful-paint'
 *     'title' => 'First Contentful Paint'
 *     'score' => 0.98
 *     'scoreDisplayMode' => 'numeric'
 *     'numericValue' => 1262.95
 *     'numericUnit' => 'millisecond'
 *     'displayValue' => '1.3 s'
 * ]
 */
```

To get the results of all audits in one go, call, `audits()`

```
$result->audits();
```

Performance metrics convenience methods
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

There are a few convenience methods to get the most interesting metrics for the performance category.

You can call all these methods on `$result`. The ones ending on `inMs` return results in milliseconds.

- `firstContentfulPaintInMs()`
- `largestContentfulPaintInMs()`
- `speedIndexInMs()`
- `totalBlockingTimeInMs()`
- `timeToInteractiveInMs()`
- `cumulativeLayoutShift()`
- `totalPageSizeInBytes()`

Some of these methods have equivalent methods that return a formatted result. So instead of milliseconds they return a formatted string like `1.3 s`

- `formattedFirstContentfulPaint()`
- `formattedLargestContentfulPaint()`
- `formattedSpeedIndex()`
- `formattedTotalBlockingTime()`
- `formattedTimeToInteractive()`
- `formattedCumulativeLayoutShift()`

Getting the configuration used to generate the result
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The result will also contain the settings that were used to run Lighthouse.

```
// returns an array
$settings = $result->configSettings();
```

To get a specific config setting, you can pass a key in dot notation to `configSettings()`. Here's how you can the used browser width.

```
$width = $result->configSettings('screenEmulation.width');
```

There are also a couple of convenience methods to quickly get interesting pieces of config that you can call on `$result`:

- `formFactor()`: returns `desktop` or `mobile`
- `userAgent()`: returns the user agent that was used
- `networkThrottlingWasEnabled()`: returns a boolean
- `cpuThrottlingWasEnabled`: return a boolean

Getting the raw results
-----------------------------------------------------------------------------------------------------------------------------

To get the raw results that were returned from Lighthouse, you can use the `rawResults` method.

```
// returns an array with everything returned from Lighthouse

$result->rawResults();
```

You can a specific value from the raw results by passing a key using dot notation.

```
// returns the lighthouse version
$result->rawResults('lhr.lighthouseVersion');
```

Getting the benchmark index
-----------------------------------------------------------------------------------------------------------------------------------------

Lighthouse computes a benchmark index as a rough approximation of the host device's CPU performance.

```
$result->benchmarkIndex(); // returns an integer
```
