Skip to content

Instantly share code, notes, and snippets.

@csarigoz
Last active October 7, 2019 21:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csarigoz/76777a4743bfa8bed491 to your computer and use it in GitHub Desktop.
Save csarigoz/76777a4743bfa8bed491 to your computer and use it in GitHub Desktop.
Googlesheet Script that returns Mobile Speed & Optimization and Desktop Speed & Optimization values in six adjacent columns
/**
* Returns Mobile Pagespeed, Mobile Usability, and Desktop Pagespeed values in three adjacent columns
* Details about how to use this script on a Google Sheets spreadsheet can be found at:
* https://cagrisarigoz.com/2015/07/10/create-your-own-google-pagespeed-and-mobile-usability-tracking-googlesheet-in-5-steps/
*/
function checkAll(Url) {
//CHANGE YOUR API KEY WITH YOUR_API_KEY BELOW
var key = "YOUR_API_KEY";
var serviceUrlMobile = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url="+Url+"&strategy=mobile&key="+key;
var serviceUrlDesktop = "https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url="+Url+"&strategy=desktop&key="+key;
var array = [];
if (key == "YOUR_API_KEY")
return "Please enter your API key to the script";
var responseMobile = UrlFetchApp.fetch(serviceUrlMobile);
if(responseMobile.getResponseCode() == 200) {
var contentMobile = JSON.parse(responseMobile.getContentText());
if ( (contentMobile != null) && (contentMobile["ruleGroups"] != null) )
{
if (contentMobile["responseCode"] == 200)
{
var speedScoreMobile = contentMobile["ruleGroups"]["SPEED"]["score"];
var usabilityScoreMobile = contentMobile["ruleGroups"]["USABILITY"]["score"];
}
else
{
array.push(["Not Found!", "Not Found!", "Not Found!"]);
return array;
}
}
}
var responseDesktop = UrlFetchApp.fetch(serviceUrlDesktop);
if(responseDesktop.getResponseCode() == 200) {
var contentDesktop = JSON.parse(responseDesktop.getContentText());
if ( (contentDesktop != null) && (contentDesktop["ruleGroups"] != null) )
var speedScoreDesktop = contentDesktop["ruleGroups"]["SPEED"]["score"];
}
array.push([speedScoreMobile, usabilityScoreMobile, speedScoreDesktop]);
return array;
}
@MarcusJTWhich
Copy link

Do you have any plans to update this to use the v5 API (released in June 2018)? See https://developers.google.com/speed/docs/insights/v5/reference/

@csarigoz
Copy link
Author

csarigoz commented Dec 20, 2018

Do you have any plans to update this to use the v5 API (released in June 2018)? See https://developers.google.com/speed/docs/insights/v5/reference/

Hello there,
It seems that the v4 will be deprecated about April-May 2019 (details here). I will try to update the script before that.
Thanks for the heads up.

@SEAZ1
Copy link

SEAZ1 commented Jun 24, 2019

Great!

@lucam1234
Copy link

Hi @csarigoz,
Are you planning to update the script with the v5 of the API by any chance? That would be super helpful!
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment