How to save Instagram follower counts to Google Sheets (using Apps Scripts)

With over 800 million monthly active users, Instagram in one social platform that every brand wants to make an impact on.

How do you record progress on this channel? One simple (and free) method is to use Apps Scripts to track follower growth on an hourly, daily or weekly basis and save the numbers to Google Sheets.

How can we get data from Instagram?

A little known feature of Instagram is that it provides data in JSON format when you append any Instagram Web URL with ?__a=1. This allows us to query Instagram data without having to submit authenticated calls to its official API.

We’ll use this feature to query our Instagram profile page (or that of a competitor) at a defined interval and record follower counts to Google Sheets.

It is possible to extend this script and also record post likes and comments but for now we’ll focus solely on follower count.

Create a Google Apps Script to save Instagram follower count to Google Sheets

  1. Open up Google Sheets and create a new spreadsheet. Rename the Sheet Followers.
  2. Navigate to Tools > Script editor….
  3. Remove any existing code and paste in the javascript below;

    var sheetName = "Followers";
    var instagramUserName = "twelvebakers";

    /*
    Get follower count for Instagram user
    */
    function getFollowerCount (username) {
    var url = "https://www.instagram.com/" + username + "/?__a=1";
    var response = UrlFetchApp.fetch(url).getContentText();
    return JSON.parse(response).user.followed_by.count;
    }

    /*
    Save Instagram follower count
    */
    function saveFollowerCount() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(this.sheetName);
    sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), getFollowerCount(this.instagramUserName)]);
    }

  4. Replace instagramUserName with the username of the Instagram account you’re going to track. In our case it’s ‘twelvebakers’.
    Our Apps Script to save Instagram follower count to Google Sheets
  5. Select the function saveFollowerCount from the dropdown menu and press Run. The first time the script runs you’ll be asked to review permissions.
  6. Review your Google spreadsheet and verify that the script has worked correctly and saved the Instagram follower count.
    Instagram follower count saved to Google Sheets
  7. Once verified we can set up a trigger to automatically run this script at an interval we define. Click the Current project’s triggers button and add a new time-driven trigger. Here you can set it to run every hour, every day or every week depending on your own reporting requirements.
    Setting up triggers in Apps Scripts to call our Instagram script at set intervals

Leave a Reply