Skip to content

Instantly share code, notes, and snippets.

@rviscomi
Last active May 28, 2021 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rviscomi/bea98941f05462c752bd5498f3cb5c70 to your computer and use it in GitHub Desktop.
Save rviscomi/bea98941f05462c752bd5498f3cb5c70 to your computer and use it in GitHub Desktop.
Apps Script code for syncing MDN content popularity stats to Google Sheets to be visualized in the content dashboard: bit.ly/mdn-content-dash
// Written by Rick Viscomi (@rick_Viscomi)
const MDN_JSON = 'https://raw.githubusercontent.com/mdn/content/main/files/popularities.json';
const MDN_JSON_COMMITS = 'https://api.github.com/repos/mdn/content/commits?path=files/popularities.json';
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Raw data')
function run() {
const lastCommit = getLastCommit();
const lastCommitDate = new Date(lastCommit.author.date).toLocaleDateString();
const lastEntryDate = new Date(sheet.getRange(sheet.getLastRow(), 1).getValue()).toLocaleDateString();
if (lastCommitDate == lastEntryDate) {
console.log('No recent changes. Done.');
return;
}
const popularities = fetchData();
const lastCommitMessage = lastCommit.message.split('/n')[0];
const data = Object.entries(popularities).map(([page, popularity]) => {
return [lastCommitDate, page, popularity, lastCommitMessage];
});
sheet.getRange(sheet.getLastRow() + 1, 1, data.length, data[0].length).setValues(data);
}
function fetchData() {
const response = UrlFetchApp.fetch(MDN_JSON).getContentText();
return JSON.parse(response);
}
function getLastCommit() {
const response = UrlFetchApp.fetch(MDN_JSON_COMMITS).getContentText();
const commits = JSON.parse(response);
return commits[0].commit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment