Overview
The PageSpeed client provides methods to analyze website performance using the PageSpeed Insights API v5. It fetches Lighthouse performance scores, Core Web Vitals, and actionable optimization opportunities for both mobile and desktop.Installation
Class: PageSpeedClient
getPageSpeedData()
Fetches comprehensive PageSpeed analysis for both mobile and desktop.url- Full URL to analyze (must include protocol)
- Analyzes both mobile and desktop simultaneously
- Extracts Core Web Vitals from mobile data (primary for ranking)
- Identifies top 10 performance opportunities
- Categorizes opportunities by impact (high/medium/low)
ReportGenerationError with:
400- Invalid URL format429- Rate limit exceeded (retryable)500+- API temporarily unavailable (retryable)
analyzeCoreWebVitals()
Analyzes Core Web Vitals with Google’s threshold ratings.| Metric | Good | Needs Improvement | Poor |
|---|---|---|---|
| LCP | ≤ 2500ms | 2500-4000ms | > 4000ms |
| FID | ≤ 100ms | 100-300ms | > 300ms |
| CLS | ≤ 0.1 | 0.1-0.25 | > 0.25 |
getPerformanceScore()
Lightweight method to get performance score only.url- Full URL to analyzestrategy- Device type (default: ‘mobile’)
- 90-100: Good (green)
- 50-89: Needs Improvement (orange)
- 0-49: Poor (red)
getMultiplePagesSpeeds()
Analyzes multiple URLs with rate limit handling.urls- Array of URLs to analyzemaxConcurrent- Max concurrent requests (default: 3)
- Processes URLs in chunks to avoid rate limiting
- 2-second delay between chunks
- Returns partial results if some URLs fail
Static Methods
isValidUrl()
Validates URL format for PageSpeed testing.formatUrl()
Formats URL for PageSpeed testing (adds https if missing).Core Web Vitals Explained
Largest Contentful Paint (LCP)
What it measures: Time until the largest content element is visible. Good threshold: ≤ 2.5 seconds Common issues:- Slow server response times
- Render-blocking JavaScript and CSS
- Large, unoptimized images
- Client-side rendering
First Input Delay (FID)
What it measures: Time from user interaction to browser response. Good threshold: ≤ 100 milliseconds Note: FID requires real user interaction. PageSpeed uses Total Blocking Time (TBT) as a proxy metric. Common issues:- Heavy JavaScript execution
- Long tasks blocking main thread
- Large, unoptimized scripts
Cumulative Layout Shift (CLS)
What it measures: Visual stability - how much content shifts unexpectedly. Good threshold: ≤ 0.1 Common issues:- Images without dimensions
- Ads, embeds, iframes without reserved space
- Dynamically injected content
- Web fonts causing FOIT/FOUT
Performance Opportunities
Opportunity Impact Categories
Common Opportunities
High Impact:- Remove unused JavaScript (typically 1-3s savings)
- Eliminate render-blocking resources (1-2s savings)
- Reduce server response time (1-2s savings)
- Serve images in next-gen formats (500-1000ms)
- Enable text compression (300-800ms)
- Minify CSS/JavaScript (300-600ms)
- Use efficient cache policies (< 300ms)
- Preconnect to required origins (< 300ms)
Error Handling
400- Invalid URL format (useformatUrl()helper)429- Rate limit exceeded (wait and retry)500- Lighthouse analysis failed (retry or check URL)
Rate Limiting
PageSpeed Insights API quotas:- Requests per day: 25,000 per API key
- Requests per 100 seconds: 400 per API key
- Requests per minute: 240 per API key
- Limit concurrent requests to 3 using
getMultiplePagesSpeeds() - Add 2-second delays between batches
- Cache results for 24 hours (performance data changes slowly)
- Use
getPerformanceScore()for lighter checks
Authentication
PageSpeed Insights API uses API key authentication (not OAuth). Configuration:- Visit Google Cloud Console
- Enable PageSpeed Insights API
- Create credentials → API key
- Restrict key to PageSpeed Insights API
Complete Usage Example
Performance Score Breakdown
Lighthouse Performance score is weighted:| Metric | Weight |
|---|---|
| First Contentful Paint | 10% |
| Speed Index | 10% |
| Largest Contentful Paint | 25% |
| Total Blocking Time | 30% |
| Cumulative Layout Shift | 25% |