/ Marketing

How to track Instagram Stats after April 5th 2018 using google spreadsheets

tl;dr copy this spreadsheet to your workspace: https://bit.ly/2HnCvPh goto Tools>Script Editor and paste this code: https://bit.ly/2J0OwHs. Replace the username and add a trigger to run the script once a day. Tell a friend about it. Oh, if you want to get your historical data from socialblade, read on.

After the recent interest in the "Cambride Analytica" affair, facebook took some drastic measures to win back the public vote. Most (if not all) professional Social Media Marketing services used the facebook API to track follower and following count - as these endpoints were recently deprecated there is currently no service that provides instagram tracking.

A couple weeks ago I incidentally switched from using socialblade.com for tracking different Instagram accounts to using google spreadsheets with a script based on the one by Nick Boyce. This allows me to build custom charts and integrate the data with other tools I'm currently using.

image alt text

Shortly after the API endpoints were deprecated, the public JSON endpoint (instagram.com/username/?__a=1") was removed as well, breaking aforementioned script. Fortunately the JSON data is buried in instagram.com/username and a lengthy regular expression allows us to keep using the script with some minor modifications (at least for now).

Setting up the script

To get the script working for you, copy my template https://bit.ly/2HnCvPh to your workspace. Open Tools>Script Editor, delete the example code and paste this:

*// the name of the sheet within your document*
var sheetName = "FollowerCount";
*// the name of the Instagram account you want to track*
var instagramAccountName = "ACCOUNTNAMEYOUWANTTOTRACK";

function insertFollowerCount() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(this.sheetName);
  accountdata = getInstagramData(this.instagramAccountName);
  sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), accountdata.followerCount, accountdata.followCount, accountdata.mediaCount]);  

function getInstagramData(username) {
  var r = new RegExp('<script type="text\/javascript">' + 
                   '([^{]+?({.*profile_pic_url.*})[^}]+?)' +
  var url = "https://www.instagram.com/" + username
  var source = UrlFetchApp.fetch(url).getContentText();
  var jsonStr = source.match(r)[2];
  var data = JSON.parse(jsonStr);
  console.log('data', data);
  var oldVariantOfData = data['entry_data']['ProfilePage'][0];
  console.log('oldVariantOfData', oldVariantOfData);
  return {
    followerCount : oldVariantOfData.graphql.user.edge_followed_by.count,
    followCount : oldVariantOfData.graphql.user.edge_follow.count, 
    mediaCount : oldVariantOfData.graphql.user.edge_owner_to_timeline_media.count

All you need to do is to replace the account name with the one you want to track. Now you can add a trigger by clicking the little watch icon selecting a time and "insertFollowerCount" as function to be executed.

Now you can keep tracking your accounts, but what about the past? If your account was tracked by socialblade, there is a way of extracting their data.

Extracting Socialblade data

image alt text

Open the relevant socialblade page like https://socialblade.com/instagram/user/linkedin/monthly and open the source code by right clicking and selecting "View page source". Using ctrl/cmd + f to search for csv which stands for "comma separated value"

image alt text

Copy from "Date... to the tailing comma. Paste to a new spreadsheet. Select the cell and goto "Data>Split text to columns" - use custom method and choose " + " (space plus space without quotation marks) then clean up the data using search-replacing to delete "\n" as well as the quotation marks.

Now the data needs to be transposed so that we can start using it to build some beautiful graphs that impress our bosses (and so we can use the script to append new data):

image alt text

Now column "A" can be split by comma and voila we have all the historical data at our fingertips. Repeat these steps with the other two CSV data fields in the socialblade page source code to get the "Followed" and "Media" count. Thats basically what I did for the accounts I monitor.

Please drop me a line if you would like me to go into some more detail, liked this post or have something to add.

Damian Bast

How to track Instagram Stats after April 5th 2018 using google spreadsheets
Share this

Subscribe to Damian Bast Business & Personal Development Blog