I have a script that I run to pull in PageSpeed Insights into Sheets. The issue I am running into is a constant error of "TypeError: Cannot read property "metrics" from undefined. (line 59, file "Code")". Since I am not pulling anything from Metrics i delete it and the error just moves down the list. Here is the part of the code that is throwing up this error.
function getPageSpeedData (url) {
var url = url;
var source = UrlFetchApp.fetch(pageSpeedApi, {muteHttpExceptions: true}).getContentText();
var data = JSON.parse(source);
var metrics = data.loadingExperience.metrics;
var lighthouse = data.lighthouseResult.audits;
var categories = data.lighthouseResult.categories;
Weird thing though is it works very rarely without modifying the code.