Created
May 21, 2019 21:46
-
-
Save ceckoslab/dbe19cfd7d07914e26758af654a2ce33 to your computer and use it in GitHub Desktop.
Automated Page Speed Insights monitoring Google App Script snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var scriptProperties = PropertiesService.getScriptProperties(); | |
var pageSpeedApiKey = ''; | |
var pageSpeedMonitorUrls = [ | |
'https://www.example-url-here.de' | |
]; | |
function createHeadingIfNoteExist() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName('Sheet1'); | |
var firstCell = sheet.getRange('A1:A1').getValues()[0]; | |
if (firstCell == '') { | |
sheet.appendRow([ | |
'Date', | |
'Url', | |
'Start Render (Desktop)', | |
'Speed Index (Desktop)', | |
'Interactive (Desktop)', | |
'First Cpu Idle (Desktop)', | |
'Estimated Input Latency (Desktop)', | |
'Start Render (Mobile)', | |
'Speed Index (Mobile)', | |
'Interactive (Mobile)', | |
'First Cpu Idle (Mobile)', | |
'Estimated Input Latency (Mobile)' | |
]); | |
} | |
} | |
function monitor() { | |
createHeadingIfNoteExist(); | |
for (var i = 0; i < pageSpeedMonitorUrls.length; i++) { | |
var url = pageSpeedMonitorUrls[i]; | |
var desktop = callPageSpeed(url, 'desktop'); | |
var mobile = callPageSpeed(url, 'mobile'); | |
addRow(url, desktop, mobile); | |
} | |
} | |
function callPageSpeed(url, strategy) { | |
var pageSpeedUrl = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?' + | |
'url=' + encodeURIComponent(url) + | |
'&key=' + pageSpeedApiKey + | |
'&category=performance' + | |
'&strategy=' + strategy; | |
var response = UrlFetchApp.fetch(pageSpeedUrl); | |
var json = response.getContentText(); | |
return JSON.parse(json); | |
} | |
function addRow(url, desktop, mobile) { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName('Sheet1'); | |
sheet.appendRow([ | |
Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd'), | |
url, | |
getLighthouseMetric(desktop, 'first-contentful-paint'), | |
getLighthouseMetric(desktop, 'speed-index'), | |
getLighthouseMetric(desktop, 'interactive'), | |
getLighthouseMetric(desktop, 'first-cpu-idle'), | |
getLighthouseMetric(desktop, 'estimated-input-latency'), | |
getLighthouseMetric(mobile, 'first-contentful-paint'), | |
getLighthouseMetric(mobile, 'speed-index'), | |
getLighthouseMetric(mobile, 'interactive'), | |
getLighthouseMetric(mobile, 'first-cpu-idle'), | |
getLighthouseMetric(mobile, 'estimated-input-latency'), | |
]); | |
} | |
function getLighthouseMetric(data, metric) | |
{ | |
var value = data.lighthouseResult.audits[metric].displayValue; | |
value = value.split('ms')[0]; | |
value = value.split('s')[0]; | |
return value.trim(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment