2
votes

I'm using "v1" of the "youtubeAnalytics" service to get view counts for YouTube videos. I can get view data for a specific video by using "filters" parameter, but I'm not sure how to get data for multiple videos in one call. Here's what I'm currently doing to get views for one video:

YOUTUBE_ANALYTICS_API_SERVICE_NAME = "youtubeAnalytics"
YOUTUBE_ANALYTICS_API_VERSION = "v1"

youtube_id = item.get_youtube_id()
youtube_analytics = get_yt_service(credential, YOUTUBE_ANALYTICS_API_SERVICE_NAME, YOUTUBE_ANALYTICS_API_VERSION)
result = youtube_analytics.reports().query(ids="channel=={0}".format(get_username(credential)),
                                           metrics="views",
                                           dimensions='day',
                                           filters='video=={0}'.format(youtube_id),
                                           start_date=start_date,
                                           end_date=end_date,
                                           sort='-views'
                                          ).execute()

To be clear, I'm using the Google API client and "get_yt_service" is a wrapper for the build function:

from apiclient.discovery import build
2

2 Answers

2
votes

As a rebuttal to Jeff's answer:

You can in fact query on more than one video at a time. Your filter would look like:

video==id1,id2,id3,...,idn;

With this being said, the data returned will be aggregated! In some cases, like views, it will be a sum (views of id1 + views of id2 + ... + views of idn). It will not tell you the views for each video id separately.

However, it is unclear to me (and maybe someone will come along and clarify this) how other metrics are aggregated. For example, I tried getting the averageViewDuration for two videos. My results were as follows:

id1: 1.8937 averageViewDuration
id2: 45.24135 averageViewDuration
both: 3.09112 averageViewDuration

This is clearly not a sum, nor a simple average of the two values. Perhaps it's weighted somehow, but it's not clear from the documentation how this data is aggregated for each possible metric. In fact, there's very little in the documentation at all about filtering on more than one video id in the same call.

I'm currently in communications with a YouTube representative, so I will append any new information about this to my answer in the future.

UPDATE

You can in fact retrieve per video data for more than one video in one analytics API call. Use the dimension "video". For example, if you want the views for video ID "a" and "b" from 1/1/15 to 1/31/15, your API request would look like

GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=contentOwner%3D%3DOwnerName&start-date=2015-01-01&end-date=2015-01-31&metrics=views&dimensions=video&filters=video%3D%3Da%2b%3B&sort=-views&start-index=1&key={YOUR_API_KEY}

Warning: There is a cap to how many distinct Video IDs you can query on per minute, which is different from your QPS (queries per second).

1
votes

It's not possible to get data for multiple video ids in a single call. You can get channel-level data in certain types of reports, but not data for just a subset of video ids.

You need to make multiple Analytics API calls, one for each video id. You can make a few requests simultaneously in separate threads and get fairly good throughput. I've put together some sample code (in Ruby, but the concepts also apply to Python) for doing this for all the videos in a given channel: https://code.google.com/p/youtube-api-samples/source/browse/#git%2Fanalytics-dump