TypeError: Cannot read property "metrics" from undefined. (line 59, file "Code")

197 views
Skip to first unread message

Victor Guerrero

unread,
Aug 6, 2019, 3:04:07 PM8/6/19
to pagespeed-insights-discuss
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 pageSpeedApi = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + url + "&strategy=desktop&key=AIzaSyC62fdejPrYL5CKmoVO3rL_ARWMk505f&prettyPrint=true";
  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.
Reply all
Reply to author
Forward
0 new messages