我似乎无法弄清楚。我可以运行该脚本有时来引入Pagespeed Insights,但是最近我遇到了此错误“ TypeError:无法从未定义中读取属性”审计”。我转到API资源管理器,一切正常,但无法弄清楚为什么会发生这种情况。
function insertPageSpeedData() {
var urls = getData();
var sheet = SpreadsheetApp.openById('15DGtJb4hW2L8HIaLpreDQE8MJTZvt27Kh1rw8a2').getSheetByName('InputData');
for(var i = 0; i < urls.length; i++){
pageSpeedData = getPageSpeedData(urls[i]);
sheet.appendRow([pageSpeedData.id,
parseFloat(pageSpeedData.firstContentfulPaint, 10),
parseFloat(pageSpeedData.firstInputDelay, 10),
pageSpeedData.performanceScore,
parseFloat(pageSpeedData.firstMeaningfulPaint, 10),
parseFloat(pageSpeedData.timetoInteractive, 10)
]);
}
}
function getPageSpeedData (url) {
var url = url;
var pageSpeedApi = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + url + "&strategy=desktop&key=AIzaSyC62fdejPrYL5CKmoVO3rL_ARWMk505&prettyPrint=true";
var source = UrlFetchApp.fetch(pageSpeedApi, {muteHttpExceptions: true}).getContentText();
var data = JSON.parse(source);
var lighthouse = data.lighthouseResult.audits;
var categories = data.lighthouseResult.categories;
return {
id: data.id,
firstContentfulPaint: (lighthouse['first-contentful-paint'].displayValue != undefined ? lighthouse['first-contentful-paint'].displayValue : ' '),
firstInputDelay: (lighthouse['max-potential-fid'].displayValue != undefined ? lighthouse['max-potential-fid'].displayValue : ' '),
performanceScore: (categories['performance'].score != undefined ? categories['performance'].score : ' '),
firstMeaningfulPaint: (lighthouse['first-meaningful-paint'].displayValue != undefined ? lighthouse['first-meaningful-paint'].displayValue : ' '),
timetoInteractive: (lighthouse['interactive'].displayValue != undefined ? lighthouse['interactive'].displayValue : ' '),
}
}