Skip to content

Instantly share code, notes, and snippets.

@greenido
Last active May 31, 2020 08:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save greenido/9f8882fada71adbcbed7 to your computer and use it in GitHub Desktop.
Save greenido/9f8882fada71adbcbed7 to your computer and use it in GitHub Desktop.
YouTube Analytics API Apps Script Example
/**
* YouTube Analytics API Example
* Fetch views and 'estimated view time' on videos you have in your channel.
*
* @Author: Ido Green
* @Date: Aug 2014
*
*/
function getVideoEstimatedMinutesWatched(videoId) {
var myChannels = YouTube.Channels.list('id', {mine: true});
var channel = myChannels.items[0];
var channelId = channel.id;
if (channelId) {
var today = new Date();
var monthAgo = new Date();
monthAgo.setMonth(today.getMonth() - 1);
var todayFormatted = Utilities.formatDate(today, 'UTC', 'yyyy-MM-dd')
var MonthAgoFormatted = Utilities.formatDate(monthAgo12, 'UTC', 'yyyy-MM-dd');
var analyticsResponse = YouTubeAnalytics.Reports.query(
'channel==' + channelId,
MonthAgoFormatted,
todayFormatted,
'views,estimatedMinutesWatched',
{
dimensions: 'video',
filters: 'video==' + videoId
});
Logger.log("Analytics for " + videoId + ": \n " + analyticsResponse.rows)[0];
return analyticsResponse.rows[0];
}
else {
return "N/A Please check the video ID";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment