Search API Connector Documentation

Print

Import YouTube Analytics Data to Google Sheets

premium

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

Click here to install the API Connector add-on from the Google Marketplace.

Part 1: Connect to the YouTube API

If you haven’t connected API Connector to YouTube Analytics before, you’ll first need to initiate the connection as follows:

  1. Open up Google Sheets and click Extensions > API Connector > Manage Connections.
  2. In the list of available connections, find YouTube and click Connect.
    youtube-connect
  3. You will see a couple of screens asking you to log in and approve the connection. Click Allow.
    youtube-img3
  4. You'll now be returned to your Google Sheet, and can verify that your YouTube connection is active in the Connections screen.

Part 2: Create a YouTube Analytics API Request URL

For our first request, we'll get some basic stats about your YouTube channel.

Again, the YouTube Analytics API only lets you query your own data. If you're looking for public YouTube data like view and comment counts for other videos, you'll need the YouTube Public Data API.

  • API root: https://youtubeanalytics.googleapis.com
  • Endpoint: /v2/reports
  • Parameters: ?dimensions=video&sort=-views&metrics=estimatedMinutesWatched,views,likes,subscribersGained&ids=channel==MINE&startDate=2021-01-01&endDate=2021-12-31&maxResults=200

Putting it together, we get the full API Request URL.

https://youtubeanalytics.googleapis.com/v2/reports?dimensions=video&sort=-views&metrics=estimatedMinutesWatched,views,likes,subscribersGained&ids=channel==MINE&startDate=2021-01-01&endDate=2021-12-31&maxResults=200

Part 3: Pull YouTube Analytics API Data into Sheets

Now let's grab that YouTube data.

  1. In the Create Request interface, enter the Request URL we just created.
    youtube-analytics-url
  2. Under OAuth, choose YouTube from the Connections dropdown. You should see a "Connected" badge.
    youtube-analytics-connected
  3. We don't need any headers for this API, so just leave that section blank.
  4. Create a new tab and click Set current to use that tab as your data destination.
  5. The YouTube Analytics API outputs data in a pretty inconvenient format (it returns all the field headers in their own array, then all the data separately), so let's clean it up a bit with some JMESPath. Under Output options > JMESPath, enter {Keys:columnHeaders[].name,rows:rows}
    youtube-analytics-jmespath
  6. Name your request and click Run. A moment later you’ll see some statistics about your videos populate your sheet.
    youtube-analytics-results
  7. By default, the field headers come through as numbers. To give those headers names instead, click Edit Fields to open the visual field editor. The field names are provided in the first few columns of the response output, so you can just substitute those in (and then hide those extra columns to tidy up your report).

Part 4: More Example API URLs

You can access the full YouTube Analytics API documentation here, but if you just want to get started, you can try the following URLs (one at a time).

  • Aggregated metrics for a channel
    https://youtubeanalytics.googleapis.com/v2/reports?metrics=views,comments,likes,dislikes,estimatedMinutesWatched,averageViewDuration&ids=channel==MINE&startDate=2021-01-01&endDate=2021-12-31&maxResults=200
  • Most watched videos for your channel
    https://youtubeanalytics.googleapis.com/v2/reports?dimensions=video&metrics=estimatedMinutesWatched,views,likes,subscribersGained&maxResults=10&sort=-estimatedMinutesWatched&startDate=2021-01-01&endDate=2021-12-31&maxResults=200&ids=channel==MINE
  • Metrics summarized by month
    https://youtubeanalytics.googleapis.com/v2/reports?dimensions=month&metrics=views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained&sort=month&startDate=2021-01-01&endDate=2021-12-31&ids=channel==MINE
  • Demographic breakdown
    https://youtubeanalytics.googleapis.com/v2/reports?dimensions=ageGroup,gender&metrics=viewerPercentage&startDate=2021-01-01&endDate=2021-12-31&ids=channel==MINE
  • View counts and watch time by traffic source
    https://youtubeanalytics.googleapis.com/v2/reports?dimensions=day,insightTrafficSourceType&metrics=views,estimatedMinutesWatched&sort=day&startDate=2021-01-01&endDate=2021-12-31&ids=channel==MINE

Many additional 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.

Part 5: Handle Pagination

Let's get it out of the way immediately, this section doesn't contain a real way to handle pagination. You can add maxResults=200 to the end of your API request URL, but after testing and searching through documentation, it seems that 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 into chunks such that it can be retrieved in groups of less than 200 at a time, and then run those request URLs separately.

For example, a request filtered by video ID would include filters=video==<list of video IDs>, like this:

https://youtubeanalytics.googleapis.com/v2/reports?dimensions=video&metrics=views,estimatedMinutesWatched,likes,dislikes,averageViewPercentage,averageViewDuration&sort=-views&maxResults=200&ids=channel==MINE&startDate=2020-01-01&endDate=2021-12-31&filters=video==fbY6r12Kyyo,n84bsqaer-g,ik2uk5pHyEA,XxYv3LIJtmo

Beyond that, you would need to get clever with sorting, or just use the YouTube Public Data API instead, which does support pagination. Of course, if you find any solutions I've missed, please leave a comment below.

Part 6: YouTube Compliance

To comply with the terms of YouTube's API, requests are subject to the following conditions:

  1. All saved requests to YouTube will refresh every 30 days, even if you have not set up a scheduled refresh.
  2. 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 7: API Documentation

YouTube provides several different APIs for extracting data from the platform:

  • 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

14 thoughts on “Import YouTube Analytics Data to Google Sheets”

  1. 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.

    Reply
    • 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.

      Reply
  2. 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.

    Reply
  3. 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! 😉

    Reply
    • 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).

      Reply
    • 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.

      Reply
  4. 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?

    Reply
    • 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.

      Reply
  5. 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?

    Reply
      • 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 even today()-3 to match the data shown in the interface.

  6. 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?

    Reply
    • 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.

      Reply

Leave a Comment

Jump To...