我正在尝试将Google Pagespeed数据(例如加载时间,屏幕上的第一个字节等)推送到Google表格中。我正在修改第三方代码。
[我成功地推送了诸如页面速度得分,请求总数,页面总大小等信息
function monitor() {
var desktop = callPageSpeed('desktop');
var mobile = callPageSpeed('mobile');
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('vivo');
var date = new Date();
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
var year = date.getFullYear();
var currentDate = day + '/' + month + '/' + year;
sheet.appendRow([
currentDate,
desktop.responseCode, //status da página
desktop.score,//nota
desktop.pageStats.numberResources, //Number of HTTP resources loaded by the page.
desktop.pageStats.cssResponseBytes, //Number of uncompressed response bytes for CSS resources on the page.
desktop.pageStats.javascriptResponseBytes, //Number of uncompressed response bytes for JS resources on the page.
desktop.pageStats.htmlResponseBytes, //Number of uncompressed response bytes for the main HTML document and all iframes on the page.
desktop.pageStats.imageResponseBytes, //Number of response bytes for image resources on the page.
desktop.pageStats.numberJsResources, //Number of JavaScript resources referenced by the page.
desktop.pageStats.numberCssResources, //Number of CSS resources referenced by the page.
mobile.responseCode, //status da página
mobile.score,//nota
mobile.pageStats.numberResources, //Number of HTTP resources loaded by the page.
mobile.pageStats.cssResponseBytes, //Number of uncompressed response bytes for CSS resources on the page.
mobile.pageStats.javascriptResponseBytes, //Number of uncompressed response bytes for JS resources on the page.
mobile.pageStats.htmlResponseBytes, //Number of uncompressed response bytes for the main HTML document and all iframes on the page.
mobile.pageStats.imageResponseBytes, //Number of response bytes for image resources on the page.
mobile.pageStats.numberJsResources, //Number of JavaScript resources referenced by the page.
mobile.pageStats.numberCssResources, //Number of CSS resources referenced by the page.
]);
}
function callPageSpeed(strategy) {
var pageSpeedUrl = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=https://www.vivo.com.br/&key=AIzaSyDs2BaThMhH3ZHARdUFftcdvEM7OC-WcFo&strategy=' + strategy ;
Logger.log(pageSpeedUrl);
var response = UrlFetchApp.fetch(pageSpeedUrl);
var json = response.getContentText();
return JSON.parse(json);
}
预期结果是将所有这些数据推送到Google表格中,并在每天将运行该代码的Google脚本上设置触发器,并添加新行。
Google pagespeed见解API:https://developers.google.com/speed/docs/insights/v5/get-started
我正在修改的代码:https://ithoughthecamewithyou.com/post/automate-google-pagespeed-insights-with-apps-script
此代码可能已被弃用,如果有人知道如何更新它,我将非常高兴。