Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Last active March 9, 2020 02:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mhawksey/83182dbac85b904c19c4 to your computer and use it in GitHub Desktop.
Save mhawksey/83182dbac85b904c19c4 to your computer and use it in GitHub Desktop.
Using Google Apps Script to proxy YouTube Analytics Channel report data http://mashe.hawksey.info/?p=16234
// instead of var results = YouTubeAnalytics.Reports.query(ids, start-date, end-date, metrics, optionalArgs);
var params = {"method" : "post",
"payload" : {'ids' : query.ids,
"startDate" : startDate,
"endDate": endDate,
"metrics": query.metrics,
"options": JSON.stringify(options)
}
};
var yt_data = UrlFetchApp.fetch("https://script.google.com/macros/s/YOUR_APP_ID/exec", params);
var results = JSON.parse(yt_data.getContentText());
// to enable YouTubeAnalytics you need to go Resources > Advanced services and add YouTube Analytics (remembering to activate in Developer Console)
// Run > doPost to trigger authentication
// Publish > Deploy as web app
// - Execute as 'me'
// - Anyone, even anonymously
// Add you web app url to line 10 in main
function doPost(e) {
var results = YouTubeAnalytics.Reports.query(e.parameters.ids, e.parameters.startDate, e.parameters.endDate, e.parameters.metrics, JSON.parse(e.parameters.options));
return ContentService.createTextOutput(JSON.stringify(results ))
.setMimeType(ContentService.MimeType.JSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment