0

I am trying to use Google AppsScript to sort all my YouTube videos in a sheet by the amount of revenue they earned in the last month. However I keep getting an error when I set the 'dimensions' to video:

Error:{  
   "error":{  
      "errors":[  
         {  
            "domain":"global",
            "reason":"badRequest",
            "message":"The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v1/available_reports for a list of supported queries."
         }
      ],
      "code":400,
      "message":"The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v1/available_reports for a list of supported queries."
   }
}(line 53,
file "Code",
project "YoutubeAnalytics")

Here is my code:

var analyticsResponse = YouTubeAnalytics.reportsQuery('channel==' + channelId,
    oneMonthAgoFormatted,
    todayFormatted,
    'views', 

{
    dimensions:
    'video',
    maxResults:
    5,
    sort:
    '-views'
});

If I simply change 'video' to 'day' or '7DayTotals' it works as expected, as these are also example dimensions listed here: https://developers.google.com/youtube/analytics/v1/dimsmets/dims

(Interestingly, and a possible hint, the 'gender' dimension does not work either and throws the same error as above')

I suspect, from looking at similar questions on StackOverflow, that the issue might be that maxResults must be declared, and for some reason mine isn't working. Even when I set the dimensions to 'day' and get an error-free report, the maxResults are never limited to the integer I assign it. It will instead give 30 results since I have a 30 day range and am giving it a 'day' dimension.

Any help would be greatly appreciated, thanks.

Janis S.
  • 2,526
  • 22
  • 32
IndieProgrammer
  • 131
  • 2
  • 13
  • Just want to add a link to the specific documentation for using the video dimension: https://developers.google.com/youtube/analytics/v1/channel_reports#top-videos-reports I feel like I'm following all the rules here.. (Note in AppsScript max-results is typed maxResults) – IndieProgrammer Oct 05 '17 at 18:17

2 Answers2

0

I think this badRequest error is happening because in the dimensions field, instead of placing a valid videoID, you instead placed the literal 'video' word. Check the documentation:

video (core dimension)

The ID of a YouTube video. In the YouTube Data API, this is the value of a video resource's id property. This is a core dimension and is subject to the Deprecation Policy.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Are you sure that is not the correct usage? It appears other answers on SO have successfully run similar code, see: https://stackoverflow.com/questions/12982845/unable-to-retrieve-video-metrics-using-youtube-analytics-api?rq=1 If that is not how the 'video' dimension should be used, what modifications should I make if I want a list of ad revenue sorted by video, instead of day? I don't just want details of 1 video, I want an entire dimension to list the data requested for all videos. Thanks! – IndieProgrammer Oct 05 '17 at 16:36
0

Okay. I was correct in assuming that they didn't like me using video as a dimension because maxResults was not working.

The correct way to format maxResults inside of AppsScript is: 'max-results': '5'

So the completed, working, line of code is:

    var analyticsResponse = YouTubeAnalytics.reportsQuery('channel==' + channelId,
    oneMonthAgoFormatted,
    todayFormatted,
    'views', 

{
    dimensions: 'video',
    'max-results': '5',
    sort: '-views'
});
IndieProgrammer
  • 131
  • 2
  • 13