Last active
May 31, 2020 08:31
-
-
Save greenido/9f8882fada71adbcbed7 to your computer and use it in GitHub Desktop.
YouTube Analytics API Apps Script Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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