Skip to content

Instantly share code, notes, and snippets.

@alonn24
Created February 17, 2021 16:31
Show Gist options
  • Save alonn24/9fbcb9ed9fb6a1c58e4ce8ebd0ab0ff9 to your computer and use it in GitHub Desktop.
Save alonn24/9fbcb9ed9fb6a1c58e4ce8ebd0ab0ff9 to your computer and use it in GitHub Desktop.
run-page-speed
import fetch from 'node-fetch';
function setUpQuery(url) {
const api = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed';
const parameters = {
url: encodeURIComponent(url)
};
let query = `${api}?`;
for (let key in parameters) {
query += `${key}=${parameters[key]}`;
}
return query;
}
async function run() {
const query = setUpQuery('https://www.bookaway.com')
const json = await fetch(query).then(x => x.json());
if (json.error) {
console.log(json.error);
return;
}
// Chrome User Experience Report
const userExperience = json.loadingExperience;
const cruxMetrics = {
"OVERALL": userExperience.overall_category,
"FCP - First Contentful Paint": userExperience.metrics.FIRST_CONTENTFUL_PAINT_MS.category,
"LCP - Largest contentful Paint": userExperience.metrics.LARGEST_CONTENTFUL_PAINT_MS.category,
"CLS - Cumulative Layout Shift": userExperience.metrics.CUMULATIVE_LAYOUT_SHIFT_SCORE.category,
"FID - First Input Delay": userExperience.metrics.FIRST_INPUT_DELAY_MS.category
};
// Lighthouse
const lighthouse = json.lighthouseResult;
const lighthouseMetrics = {
'FCP - First Contentful Paint': lighthouse.audits['first-contentful-paint'].displayValue,
'SI - Speed Index': lighthouse.audits['speed-index'].displayValue,
'LCP - Largest Contentful Paint': lighthouse.audits['largest-contentful-paint'].displayValue,
'TTI - Time To Interactive': lighthouse.audits['interactive'].displayValue,
'TBT - Total Blocking Time': lighthouse.audits['total-blocking-time'].displayValue,
'CLS - Cumulative Layout Shift': lighthouse.audits['cumulative-layout-shift'].displayValue,
'FMP - First Meaningful Paint': lighthouse.audits['first-meaningful-paint'].displayValue,
'First CPU Idle': lighthouse.audits['first-cpu-idle'].displayValue,
'EID - Estimated Input Latency': lighthouse.audits['estimated-input-latency'].displayValue
};
console.log({ cruxMetrics, lighthouseMetrics });
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment