YouTube Analytics

  • The code demonstrates how to generate a YouTube Analytics report for a specific channel.

  • It retrieves the user's channels, selects the first one, and sets a one-month date range for the report.

  • The report queries for views, likes, dislikes, and shares, broken down by day and sorted in descending order of date.

  • The resulting analytics response is then logged to the console.

Create report for a channel

 function 
  
 runYoutubeAnalyticsReport 
 () 
  
 { 
  
 // 
  
 Get 
  
 the 
  
 list 
  
 of 
  
 all 
  
 channels 
 . 
  
 var 
  
 myChannels 
  
 = 
  
 YouTube 
 . 
 Channels 
 . 
 list 
 ( 
 'id' 
 , 
  
 { 
 mine 
 : 
  
 true 
 }); 
  
 // 
  
 Pick 
  
 the 
  
 first 
  
 available 
  
 channel 
 . 
  
 var 
  
 channel 
  
 = 
  
 myChannels 
 . 
 items 
 [ 
 0 
 ]; 
  
 var 
  
 channelId 
  
 = 
  
 channel 
 . 
 id 
 ; 
  
 // 
  
 Set 
  
 the 
  
 dates 
  
 for 
  
 report 
 . 
  
 var 
  
 today 
  
 = 
  
 new 
  
 Date 
 (); 
  
 var 
  
 oneMonthAgo 
  
 = 
  
 new 
  
 Date 
 (); 
  
 oneMonthAgo 
 . 
 setMonth 
 ( 
 today 
 . 
 getMonth 
 () 
  
 - 
  
 1 
 ); 
  
 var 
  
 todayFormatted 
  
 = 
  
 Utilities 
 . 
 formatDate 
 ( 
 today 
 , 
  
 'UTC' 
 , 
  
 'yyyy-MM-dd' 
 ); 
  
 var 
  
 oneMonthAgoFormatted 
  
 = 
  
 Utilities 
 . 
 formatDate 
 ( 
 oneMonthAgo 
 , 
  
 'UTC' 
 , 
  
 'yyyy-MM-dd' 
 ); 
  
 // 
  
 See 
  
 https 
 : 
 // 
 developers 
 . 
 google 
 . 
 com 
 / 
 youtube 
 / 
 analytics 
 / 
 v1 
 / 
 reports 
  
 for 
  
 // 
  
 supported 
  
 dimensions 
  
 and 
  
 metrics 
 . 
  
 var 
  
 analyticsResponse 
  
 = 
  
 YouTubeAnalytics 
 . 
 Reports 
 . 
 query 
 ( 
  
 'channel==' 
  
 + 
  
 channelId 
 , 
  
 oneMonthAgoFormatted 
 , 
  
 todayFormatted 
 , 
  
 'views,likes,dislikes,shares' 
 , 
  
 { 
  
 dimensions 
 : 
  
 'day' 
 , 
  
 sort 
 : 
  
 '-day' 
  
 }); 
  
 console 
 . 
 log 
 ( 
 analyticsResponse 
 ); 
 } 
Design a Mobile Site
View Site in Mobile | Classic
Share by: