Skip to content

Instantly share code, notes, and snippets.

@IronistM
Created April 8, 2015 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IronistM/9204f957d914b3e83b0a to your computer and use it in GitHub Desktop.
Save IronistM/9204f957d914b3e83b0a to your computer and use it in GitHub Desktop.
function pageSpeed(url) {
url = url || 'http://ctrlq.org/';
var APIkey = 'XYZ'; // Get the API key from Google Dev Console
var strategy = 'desktop'; // 'desktop' or 'mobile'
var api = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='
+ url + '&key=' + APIkey + '&strategy=' + strategy;
var response = UrlFetchApp.fetch(api, {muteHttpExceptions: true });
var parsedResult = JSON.parse(response.getContentText());
var pageSpeedResults = {};
pageSpeedResults['score'] = parsedResult.score;
var pageStats = ['numberResources', 'numberHosts', 'totalRequestBytes',
'numberStaticResources', 'htmlResponseBytes', 'cssResponseBytes',
'imageResponseBytes', 'javascriptResponseBytes',
'otherResponseBytes', 'flashResponseBytes','textResponseBytes',
'numberJsResources', 'numberCssResources'];
for (var i=0; i < pageStats.length; i++) {
pageSpeedResults[pageStats[i]] = parsedResult.pageStats[pageStats[i]];
}
return(pageSpeedResults);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment