smnandre / pagespeed-api
PageSpeed Insight PHP Api Client π Analyse web pages for performances metrics, core web vitals...
Fund package maintenance!
smnandre
Requires
- php: >=8.3
- symfony/http-client: ^7.0
- webmozart/assert: ^1.11
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.56
- phpunit/phpunit: ^11.2
README
This PHP library offers an effortless way to leverage Google's PageSpeed Insights API.
Analyze your web pages for performance metrics, get detailed reports, and optimize your site with ease. π
Installation
composer require smnandre/pagespeed-api
Usage
Initialize the API
use PageSpeed\Api\PageSpeedApi; $pageSpeedApi = new PageSpeedApi(); // or with API key (optional) $pageSpeedApi = new PageSpeedApi('YOUR_API_KEY');
Run analysis
// Analyze a page $analysis = $pageSpeedApi->analyse('https://example.com/'); // ...with a specific strategy (mobile or desktop) $analysis = $pageSpeedApi->analyse('https://example.com/', 'mobile'); // ...with a specific locale (e.g., fr_FR) $analysis = $pageSpeedApi->analyse('https://example.com/', locale: 'fr_FR'); // ...with a specific category (performance, accessibility, best-practices, seo) $analysis = $pageSpeedApi->analyse('https://example.com/', categories: 'performance');
Parameters
Audit Scores
use PageSpeed\Api\PageSpeedApi; $pageSpeedApi = new PageSpeedApi(); $analysis = $pageSpeedApi->analyse('https://www.example.com'); $scores = $analysis->getAuditScores(); // array ( // 'performance' => 100, // 'accessibility' => 88, // 'best-practices' => 100, // 'seo' => 90, // )
Audit categories
Score Evaluation
Core Web Vitals
Loading Metrics
use PageSpeed\Api\PageSpeedApi; $pageSpeedApi = new PageSpeedApi(); $analysis = $pageSpeedApi->analyse('https://www.example.com'); $metrics = $analysis->getLoadingMetrics(); // array ( // 'CUMULATIVE_LAYOUT_SHIFT_SCORE' => 'FAST', // 'EXPERIMENTAL_TIME_TO_FIRST_BYTE' => 'AVERAGE', // 'FIRST_CONTENTFUL_PAINT_MS' => 'FAST', // 'FIRST_INPUT_DELAY_MS' => 'FAST', // 'INTERACTION_TO_NEXT_PAINT' => 'FAST', // 'LARGEST_CONTENTFUL_PAINT_MS' => 'FAST', // )
Main Metrics
Contributing
Contributions are welcome! If you would like to contribute, please fork the repository and submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for more information.