We'll be using the new Instagram API within the Facebook Marketing API reference. Read all about the Instagram Graph API here.

By setting up a daily trigger in your Sheet, this script will import the number of comments, likes, media, follows, followers and engagement from your Instagram business profile each day. As a result, you'll be able to add nice looking Instagram growth curves to your business reports.

To get started, make a copy of the above Google Sheet and open your script editor by going to "Tools" -> "Script editor".

Step 1 - Create a Facebook access token

To get an access token, you first have the create a Facebook app. You can do this right here.

From there on head over to the Graph API explorer to generate an access token. Make sure to give your token the following permissions:
  • instagram_basic
  • pages_show_lists

Step 2 - Increase the lifespan of the access token

Standard access tokens are only valid for around 30 minutes, which isn't too handy if you want to use this script many times.

Luckily you can quite easily extend that duration via the access token debugger.  Open the debugger, paste in your access token, and extend its expiry date. It should now last at least a few months, but it might still be good to set up a reminder somewhere.

Step 3 - Request account IDs

After you've copied your long-lived access token into the sheet we can run the script. Firstly, we need to figure out to which account you've got access. We do that with the /me/accounts request.

var apiUrlGetAccounts = '' + accessToken; 
var responseApiGetAccounts = UrlFetchApp.fetch(apiUrlGetAccounts);
var result = JSON.parse(responseApiGetAccounts.getContentText());

Step 4 - Request Instagram business account IDs

Once we have your account ID, we can find the Instagram business account ID. However, this will only work if you've already linked your Facebook and Instagram accounts together.

var apiUrl = ''+ PageID +'?fields=instagram_business_account&access_token=' + accessToken; 
var responseApiGetBusinessAccount = UrlFetchApp.fetch(apiUrl);
var resultBusinessAccount = JSON.parse(responseApiGetBusinessAccount.getContentText());

Step 5 - Request Instagram business account posts

Now that we have the Instagram business account ID, we can make a fetch for the posts.

 // get all media with a limit of 1000 post
  var apiUrl = ''+ BusinessID +'/media?limit=1000&access_token=' + accessToken; 
  var responseApiGetMedia = UrlFetchApp.fetch(apiUrl);
  var mediaResult = JSON.parse(responseApiGetMedia.getContentText());
  // define empty array to push media to
  var media = [];
  var arrayLength =;
  // push results into the array 
  for (var i=0; i<arrayLength; i++){

Step 6 - Request post comments and likes

Once we have all the post IDs, we're just one step away from fetching the comments and like on those posts.

// define empty array to insert media details
var commentsCount = 0;
var like_count = 0;
//loop through all posts and get details  
for (var i=0; i<media.length; i++){
  //define post
  var MediaID = media[i];   
  //get details for that post
  var apiUrl = ''+ MediaID +'?fields=comments_count,like_count&access_token=' + accessToken; 
  var responseApiComments = UrlFetchApp.fetch(apiUrl);
  var result = JSON.parse(responseApiComments.getContentText());
  //push post details to the array
  commentsCount = commentsCount + result.comments_count;
  like_count = like_count + result.like_count; 

Step 7 - Fetch total media, follows and followers 

Using a very similar fetch as above, we can get the total number of followers and follows.

// get all instagram business account details
var apiUrl = ''+ BusinessID +'?fields=media_count,follows_count,followers_count,username&access_token=' + accessToken; 
var responseApiGetMedia = UrlFetchApp.fetch(apiUrl);
var BusinessIDResult = JSON.parse(responseApiGetMedia.getContentText());
// define empty array to push media to
var media_count = BusinessIDResult.media_count;
var follows_count = BusinessIDResult.follows_count;
var followers_count = BusinessIDResult.followers_count;

Step 8 - Calculate engagement and insert all values

The last step, now that we've fetched all values, is to calculate the engagement. We do this using a custom formula that I created, so feel free to use your own function. Once we've done that we can insert all the values inside the sheet.

var engagement = (commentsCount + like_count) / followers_count;
var dateToday = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
//get number of values in row
var rowNum = sheet.getRange("D1:D").getValues();
rowNum = rowNum.filter(String).length;

//to start at the 3rd row
rowNum = rowNum + 2
//append to last row in range
sheet.getRange(rowNum, 4,1,1).setValue(dateToday);
sheet.getRange(rowNum, 5,1,1).setValue(commentsCount);
sheet.getRange(rowNum, 6,1,1).setValue(like_count);
sheet.getRange(rowNum, 7,1,1).setValue(media_count);
sheet.getRange(rowNum, 8,1,1).setValue(follows_count);
sheet.getRange(rowNum, 9,1,1).setValue(followers_count);
sheet.getRange(rowNum, 10,1,1).setValue(engagement);

The real automation comes into play once you've set up a daily trigger. That way the data gets added each day. To do this, simply this the stopwatch icon inside the script editor and create a daily trigger. 

Let me know if this was useful & let's connect on     or