API Connector Documentation
Import YouTube Analytics Data to Google Sheets
This guide will show how to pull private YouTube video data (data you need to be logged in to view) directly into Google Sheets, using the API Connector add-on for Sheets.
In general, YouTube's Public Data API is better developed and easier to work with than their Analytics API, so if you only need simple data like views, comments, and likes, I suggest checking this article instead. But if you want private video metrics for your own channel, like average watch time, views by date, and demographic breakdowns, read on for a step-by-step guide to the YouTube Analytics API.
Contents
- Before You Begin
- Part 1: Connect to the YouTube Analytics API
- Part 2: Pull YouTube Analytics API Data into Sheets
- Part 3: Create a Custom API Request
- Part 4: Handle Pagination
- Part 5: YouTube Compliance
- Part 6: API Documentation
Before You Begin
Click here to install the API Connector add-on from the Google Marketplace.
Part 1: Connect to the YouTube Analytics API
The easiest way to get started with the YouTube Analytics API is through API Connector’s built-in integration.
- Select YouTube Analytics from the drop-down list of applications
- Under Authorization, click Connect to YouTube
- You will see a couple of screens asking you to log in and approve the connection. Click Allow.
- You'll then be returned to your Google Sheet, and can verify that your YouTube connection is active.
Part 2: Pull YouTube Analytics Data into Sheets
Now that we’re connected, let’s pull some data into Sheets.
- Under Endpoint, choose whether you'd like to fetch data about your channel overall or stats for the individual videos in your channel. We'll start by getting video data.
- Fill in any required parameters. For the videos endpoint we need to populate the
start Date
,endDate
,maxResults
,metrics
, ANDsort
parameters. - Optionally set the
filters
parameter to select specific data points for the response. - Select a destination sheet, name your request, and click Run.
Part 3: Create a Custom API Request
Alternatively, you can run your own custom requests instead of using API Connector’s pre-built integration, using any of the API URLs shown in the API documentation.
To create a custom request, add your complete URL into the Request URL field, and choose YouTube from the OAuth menu. Here's an example request setup:
Example #1 - Channel Report
- Open up Google Sheets and click Extensions > API Connector > Open > Create request.
- In the request form enter the following:
- Application:
Custom
- Method:
GET
- Request URL:
https://youtubeanalytics.googleapis.com/v2/reports?dimensions=video&sort=-views&metrics=estimatedMinutesWatched,views,likes,subscribersGained&ids=channel==MINE&startDate=2022-01-01&endDate=2022-12-31&maxResults=200
- OAuth:
YouTube
- Application:
- When you select YouTube from the OAuth menu, you'll see a blue Connect button if you haven't already authorized the connection. Click through to enable the connection.
- Create a new tab and click Set current to use that tab as your data destination.
- Name your request and click Run. A moment later you’ll see statistics about your channel's videos populate your sheet.
Example #2 - Revenue Report
If you have a monetized channel and access to a Content Manager account, you can fetch additional revenue-related metrics for your YouTube account.
- Verify that you have a YouTube Content Owner account. A YouTube Content Owner is not the same as a YouTube channel. Typically, you only have a YouTube Content Owner account if you manage many different channels.
- Get your Content Owner ID.
- Get your YouTube Channel ID.
- Open up Google Sheets and click Extensions > API Connector > Open > Create request.
- In the request form enter the following, filling in your own content owner and channel IDs:
- Application:
Custom
- Method:
GET
- Request URL:
https://www.googleapis.com/youtube/analytics/v2/reports?ids=contentOwner==your_content_owner_id&start-date=2023-01-01&end-date=2023-10-31&metrics=views,estimatedAdRevenue,grossRevenue&filters=channel==your_channel_id
- OAuth:
YouTube
- Application:
- When you select YouTube from the OAuth menu, you'll see a blue Connect button if you haven't already authorized the connection. Click through to enable the connection.
- Create a new tab and click Set current to use that tab as your data destination.
- Name your request and click Run. A moment later you’ll see statistics including revenue-related data populate your sheet.
Part 4: Handle Pagination
Let's get it out of the way immediately, this section doesn't contain a real way to handle pagination. You can set the maxResults
parameter to 200
, but this API doesn't support paginating through results to retrieve more than 200 records at a time. If you have more than 200 videos, you can try using the filters parameter to break your data up such that it can be retrieved in groups of less than 200 at a time, and then run those request URLs separately.
Part 5: YouTube Compliance
To comply with the terms of YouTube's API, requests are subject to the following conditions:
- All saved requests to YouTube will refresh every 30 days, even if you have not set up a scheduled refresh.
- All YouTube requests will show only the metric name rather than the full path in the header row.
Relevant Terms:
“API Clients may store all other types of Authorized Data not identified in section (III.E.4.b) for as long as is necessary for the purposes of the specific consent granted by an active user and for no longer than 30 calendar days. After 30 calendar days, the API Client must either delete or refresh the stored data”. (link)
"Your API Clients must not (i) replace API Data with similar, independently calculated data, or (ii) access or use API Data to create new or derived data or metrics." (link)
Part 6: API Documentation & Resources
Many sample queries are provided in YouTube's list of sample requests. In addition, they provide an API Explorer tool that simplifies the process of testing and constructing queries.
Related documentation:
- The YouTube Analytics API (the subject of this article) allows you to query private statistics about your channel, like video watch times and demographic breakdowns. Documentation: https://developers.google.com/youtube/analytics/data_model
- The YouTube Public Data API allows you to retrieve lists of content based on search parameters. It also provides some basic statistics like views, likes, comment counts, and subscriber counts, for all public videos. Documentation: https://developers.google.com/youtube/v3/docs
- The YouTube Reporting API is focused on reporting, and provides bulk reports of YouTube analytics data for your own channel, including detailed metrics and ad performance reports. Unlike the Analytics API, it doesn't provide filtering and sorting parameters, as these reports are designed to be downloaded and stored in a data warehouse, where you can then query the data yourself. (This table provides a summary of the differences between YouTube's Reporting and Analytics APIs). Documentation: https://developers.google.com/youtube/reporting/v1/reports
First of all, thank you so much for this amazing tool!
I am, however, running into some trouble with pagination for the Youtube analytics API using the "video" dimension (whole request is below). I simply want to retrieve data for all videos on my channel. Do you know what settings I should use in order to get pagination working?
https://youtubeanalytics.googleapis.com/v2/reports?dimensions=video&metrics=views,estimatedMinutesWatched,likes,dislikes,averageViewPercentage,averageViewDuration&sort=-views&startDate=2014-01-01&endDate=2061-12-31&maxResults=200&ids=channel==MINE
Kind regards.
The docs show the startIndex / maxResults parameters, but it seems like these still don't let you paginate beyond 200 total results. I'll update the article to reflect that.
Absolutely amazing article! hi there thanks for sharing this piece of information with us, I was looking for something like this as importing the data is an important part of analyzing a video's performance. Thanks again for sharing it with us. you really eased my task.
Hi - this is the tool I've been looking for! But I can't seem to get more than 1 dimension at a time to work. I would like to get view counts by video by day (actually, I'd like by Country as well, but let's just get two working for now!)
This is what I have:
https://youtubeanalytics.googleapis.com/v2/reports?dimensions=day&metrics=estimatedMinutesWatched,views,likes,subscribersGained&ids=channel==MINE&startDate=2020-01-01&endDate=2021-12-31
The above works fine.
https://youtubeanalytics.googleapis.com/v2/reports?dimensions=day,video&metrics=estimatedMinutesWatched,views,likes,subscribersGained&ids=channel==MINE&startDate=2020-01-01&endDate=2021-12-31
But if I add in video it errors. in the docs 'video' seems to be a valid dimension, but it doesn't work when I use it in the API. Please help! 😉
Good question. Unfortunately the YouTube API is really outdated and doesn't provide a lot of data points that people would obviously be interested in, for example video views per day. As far as I can tell YouTube doesn't permit this combination. It's not clearly written in their docs, but their examples don't show this combination and I see other people asking this same question online (e.g. here, here)
So I think you can only get this by pulling dates one by one, like this:
https://youtubeanalytics.googleapis.com/v2/reports?dimensions=video&sort=-views&metrics=estimatedMinutesWatched,views,likes,subscribersGained&ids=channel==MINE&startDate=2021-05-23&endDate=2021-05-23&maxResults=200
Then you would need to repeat that for each day. Obviously that's not ideal, but you could list these one after the other or reference cells in the sheet to make this a bit more convenient (info).
Hi Mark,
I found a workarround for the problem. You need to create a second table with a list of all video IDs (Maybe use a more general request with the API Connector) and then add a filter parameter to your first request like so:
&filters=video==+++!videoIDsA2:A+++
The information between the
+++
is the column with the Video IDs so the Connector makes new requests for each video in the list.Hope this helps a bit.
Thanks so much for your reply!
Well, at least it's not me doing something wrong...but it is surprising that (what I consider to be) the primary Use Case isn't supported by the API...
Maybe I can get something like Postman to run it multiple times with changing dates?
Yeah it's pretty weird, their API doesn't support key use cases and has strange limits. You can definitely run it in Postman by manually changing the dates and re-running, but I'm not sure if Postman has an automatic feature to cycle through requests like API Connector does.
Hi, I would like to get the past 7 days worth of stats. Is there a way that I can set the start date as dynamically 7 days ago? Or some other kind of work around?
Sure, you can add your dates into a cell with a formula like
=today()-1
, and then use those cells in your request.Hi Ana. I'm trying this again.
On a sheet "QuerySheet"
B2 is 22 Jan 2022 through formula:
=text(today()-6,"yyyy-mm-dd")
C2 is 28 Jan 2022 through formula:
=text(today(),"yyyy-mm-dd")
I'm using this API URL:
https://youtubeanalytics.googleapis.com/v2/reports?metrics=views,comments,likes&ids=channel==MINE&startDate=+++QuerySheet!B2+++&endDate=+++QuerySheet!C2+++&maxResults=200
This works and gives me back 21 for the views which is only stat I want for now.
But... when I look at the analytics on the site, it tells me there were 61 views in the same period.
If I change C2 to "=text(today()-1,"yyyy-mm-dd")" I get 25. This makes it 27 Jan 2022.
YouTube Analytics says there were 27 views on 27th.
What do you think?
Thanks for your help so far.
I just tested and it looks like the API has a 2 day latency. That's definitely longer than I expected so I don't know if it's always that delayed, but I'm getting 0 views for yesterday and the day before, and accurate view counts for every day before that. So you'll need to set your formula to
today()-2
or, depending on your timezone, maybe eventoday()-3
to match the data shown in the interface.Hi, I'm trying to use the API, but it's hanging on the first step when I click "Connect" been about 10 mins, how long should this take to being up the permissions sheet?
Hey there, this issue is related to being logged into Google with multiple accounts. To resolve, please log out of all your Google accounts and then log back in first with the account you use with API Connector, or log in with a new profile or incognito window.
Did anyone try fetching channel information like subscribers, uploads & video views? Would be interesting for competitive research. There must be a public API as well?
Sure, you can get all that data through YouTube's Public Data API, this article explains how to access it: Import YouTube Public Data to Google Sheets
Why annotationImpressions and annotationClickThroughRate keep showing zero (0) but it has values against each video?
I can't think of any reason they would show zero if they have data. YouTube Analytics can have a long latency period (2-3 days) before fresh data appears, are these metrics populated when you fetch data from an earlier time period?
Hi
Is there a way to get impressions and clicks by videos?
If you're getting private video data via the YouTube Analytics API select the "Get video performance reports" endpoint and then set the "metrics" parameter to
views
andcardClicks
.If you're fetching data for public videos (i.e. not your own videos), metrics are more limited, but you can select the "/videos" endpoint and set the "part" parameter to
statistics
for views, likes, and comments.