---
title: "How to get Google Lighthouse reports using PageSpeedInsights API and Google Apps Script"
author: "Martin Hawksey"
date: 2021-11-18T21:28:22+00:00
modified: 2021-11-18T21:28:35+00:00
canonical_url: https://pulse.appsscript.info/p/2021/11/how-to-get-google-lighthouse-reports-using-pagespeedinsights-api-and-google-apps-script/
markdown_url: https://pulse.appsscript.info/p/2021/11/how-to-get-google-lighthouse-reports-using-pagespeedinsights-api-and-google-apps-script.md
categories:
  - "Comment"
  - "Intermediate"
  - "Solutions"
  - "V8"
tags:
  - "@_md_tweets"
  - "stackoverflow"
featured_image: https://pulse.appsscript.info/files/2021/11/viewer-e1637270120592.png
description: "On StackOverflow there is a nice question/answer from Mert Dökümcü on How to send Google Lighthouse reports to Slack using PageSpeedInsights API and Google Apps Script. Find out how you can get results from PageSpeedInsights using Google Apps Script."
---

# How to get Google Lighthouse reports using PageSpeedInsights API and Google Apps Script

![How to get Google Lighthouse reports using PageSpeedInsights API and Google Apps Script](https://pulse.appsscript.info/files/2021/11/viewer-e1637270120592.png)

![Lighhouse](https://pulse.appsscript.info/files/2021/11/viewer-e1637270120592-1.png)

Image: CC-BY Google

> Lighthouse is an open-source, automated tool for improving the quality of web pages. You can run it against any web page, public or requiring authentication. It has audits for performance, accessibility, progressive web apps, SEO and more.

On StackOverflow there is a nice question/answer from Mert Dökümcü on [How to send Google Lighthouse reports to Slack using PageSpeedInsights API and Google Apps Script**](https://stackoverflow.com/a/69904022/1027723). You can read the full solution there and below is an excerpt from Mert’s post (copied here CC-BY-SA Mert Dökümcü) of the bit that really interested me, getting results from `PageSpeedInsights`.

> First and foremost, get…
> 
> 
> 
> Your `PageSpeedInsights API` key, ([here is how](https://developers.google.com/speed/docs/insights/v5/get-started#APIKey))
> 
> 
> 
> Once you have these, go to [https://script.google.com/home](https://script.google.com/home) and create a new script.
> The following code should do the trick:
> 
> 
> 
> 

```
var mobileData = fetchDataFromPSI('mobile');
var desktopData = fetchDataFromPSI('desktop');

function pageSpeedApiEndpointUrl(strategy) {
 const apiBaseUrl = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed';
 const websiteHomepageUrl = ''; // Your website
 const apikey = ''; // Your API key
 
 const apiEndpointUrl = apiBaseUrl + '?url=' + websiteHomepageUrl + '&key=' + apikey + '&strategy=' + strategy;
 return apiEndpointUrl;
}
function fetchDataFromPSI(strategy) {
 const pageSpeedEndpointUrl = pageSpeedApiEndpointUrl(strategy);
 const response = UrlFetchApp.fetch(pageSpeedEndpointUrl);
 const json = response.getContentText();
 const parsedJson = JSON.parse(json);
 
 const lighthouse = parsedJson['lighthouseResult']
 const originLoadingExperience = parsedJson['originLoadingExperience']

 const result = {
 'overall_performance': originLoadingExperience['overall_category'],
 'score': lighthouse['categories']['performance']['score']*100,
 'firstContentfulPaint': lighthouse['audits']['first-contentful-paint']['displayValue'],
 'speedIndex': lighthouse['audits']['speed-index']['displayValue'],
 'timeToInteractive': lighthouse['audits']['interactive']['displayValue'],
 'firstMeaningfulPaint': lighthouse['audits']['first-meaningful-paint']['displayValue'],
 }
 return result; 
}
```

---

*Published by [Martin Hawksey](https://pulse.appsscript.info/p/author/admin/) on [Pulse](https://pulse.appsscript.info/). Original article: [https://pulse.appsscript.info/p/2021/11/how-to-get-google-lighthouse-reports-using-pagespeedinsights-api-and-google-apps-script/](https://pulse.appsscript.info/p/2021/11/how-to-get-google-lighthouse-reports-using-pagespeedinsights-api-and-google-apps-script/)*
