2

I'm trying to get total views of each channel owned by a content owner.

I could get total views of all channels, but I want to get each channel's views.

The following is the code I tried. I set a dimensions=channel to do it, but it returned a 400 error.


from googleapiclient.discovery import build


analytics_service = build("youtubeAnalytics", "v2", credentials=credentials)

report_result = analytics_service.reports().query(
    ids= f'contentOwner=={owner_id}',
    startDate='2020-12-01',
    endDate='2021-03-30',
    dimensions='channel',  # <-- It raise the error.
    metrics='views,comments,likes,dislikes',
    filters='uploaderType==self',
).execute()

Error:

HttpError: <HttpError 400 when requesting https://youtubeanalytics.googleapis.com/v2/reports?ids=contentOwner%3D%3DkNuvRJ6GzXE5GEKbcUp7MA&startDate=2020-12-01&endDate=2021-03-30&dimensions=channel&metrics=views%2Ccomments%2Clikes%2Cdislikes&filters=uploaderType%3D%3Dself&maxResults=10&sort=-views&alt=json returned "The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v2/available_reports for a list of supported queries.". Details: "The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v2/available_reports for a list of supported queries.">

I checked docs of API, but I couldn't find a way to get it.
Please tell me if you know any good ways to do this.

Thanks.

ksmzn
  • 329
  • 1
  • 2
  • 9

1 Answers1

2

I can't specifically think of a way, but you can always derive all the channelIDs and make requests for each channel ID and get it like this.

from googleapiclient.discovery import build


analytics_service = build("youtubeAnalytics", "v2", credentials=credentials)

report_result = analytics_service.reports().query(
    ids= f'contentOwner=={owner_id}',
    startDate='2020-12-01',
    endDate='2021-03-30',
    dimensions='day',
    metrics='views,comments,likes,dislikes',
    filters='channel=={channelID};uploaderType==self',
).execute()
Shashwat
  • 462
  • 2
  • 9