How to get the subscriber count of a YouTube channel to Google Sheets

I have a list of YouTube channels' URLs. How do I get their subscriber count to a Google Sheet?

Upvotes: 4

Views: 3064

Answers (1)

YouTube Data API v3 credentials will be required. See:

Once you get the API key:

  1. Get your URL list to a Google Sheet.

  2. Create a new Google Apps Script project. (Tools > Script Editor)

  3. Paste this code to your script file:

     var api_key='YOUR_API_KEY'
     var google_url = 'https://www.googleapis.com/youtube/v3/channels/' 
     var url_id = google_url + '?key=' + api_key + '&part=statistics' + '&id='
    
     function GET_SUBS(input) {
         /** Takes channel ID as input and returns the subscriber count */
         url_id = url_id + input
         return +ImportJSON(url_id, "/items/statistics/subscriberCount", "noHeaders")
     }
    
  4. Click on the plus icon next to the "Files" and choose "Script". Rename your new script file as "ImportJSON" and paste this code.

  5. Go back to the sheet and set up a column for channel IDs. You can use the following code to strip ID of a channel from the URL:

    =ArrayFormula(REGEXREPLACE('CHANNEL_URL',"(.*\/)(.*)$","$2"))
    

Sample table.

  1. Use the GET_SUBS() function to get the follower count.

    GET_SUBS('CHANNEL_ID')
    

Note: Currently YouTube API doesn't provide the exact subscriber count of a channel publicly. See this Stackoverflow question. So the results we have are rounded.

Upvotes: 4

Related Questions