I'm using this super-handy script to get Google PageSpeed Insights metrics into a Google spreadsheet (thanks to the author):
var pageSpeedApiKey = 'AIzaSyDRgiUgu9Y3xVur0Xf0TdUmxgOYfltLFW4';
var pageSpeedMonitorUrl = 'http://www.sauder.ubc.ca';
function monitor() {
  var mobile = callPageSpeed('mobile');
  var desktop = callPageSpeed('desktop');
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('GooglePageSpeedInsightsData');
  sheet.appendRow([
                   Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd'),
                   mobile.score,
                   desktop.score
                  ]);
    // more available, i.e. desktop.pageStats.numberResources
}
function callPageSpeed(strategy) {
  var pageSpeedUrl = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=' + pageSpeedMonitorUrl + '&key=' + pageSpeedApiKey + '&strategy=' + strategy;
  var response = UrlFetchApp.fetch(pageSpeedUrl);
  var json = response.getContentText();
  return JSON.parse(json);
}
I'm using another script that adds a blank row on row 3 to my sheets to keep all my data in reverse chronological order as new data comes in.
So instead of appending to the last row, I need it inserted into "row 3", or to "the first empty row"!
You can insert a new row at row 3, and then get the range for row 3 and set the values:
sheet.insertRows(3, 1);//shift all rows down by one from row 3
sheet.getRange(3,1,1,3)//(start row, start column, number of rows, number of columns
   .setValues([[
               Utilities.formatDate(new Date(), 'GMT', 'yyyy-MM-dd'),
               mobile.score,
               desktop.score
              ]]);
Apps Script Documentation for insertRows()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With