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

The goal of this tutorial is to find all the usernames of profiles who've commented on your posts. This is quite interesting so that you can not only engage more with them but also try to find similarities and understand why they engage more than other followers.

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.

var apiUrl = ''+ BusinessID +'?fields=media_count,follows_count,followers_count,media,stories&limit=1000&access_token=' + accessToken; 
var responseApiGetMedia = UrlFetchApp.fetch(apiUrl);
var mediaResult = JSON.parse(responseApiGetMedia.getContentText());

Step 6 - Request post commenters

Once we have all the post IDs, we're just one step away from fetching the commenters on that post. 

var apiUrl = ''+ MediaID +'?fields=comments_count,comments%7Busername%7D&access_token=' + accessToken; 
var responseApiComments = UrlFetchApp.fetch(apiUrl);
var result = JSON.parse(responseApiComments.getContentText());

Step 7 - Count duplicates 

The result of the above requests gives us the usernames of commenters. The last step is to count how often the same name appears and sort it from highest to lowest.

//Sort usernames
//Create empty array to count usernames
var usernamesArray = [];
var usernamesCount = [];
var current = null;
var count = 0;
//Start counter
for(var i = 0; i < usernames.length; i++)
  //Check for dubplicate names
  if(usernames[i] != current)
    if(count > 0)
    current = usernames[i];
    count = 1;
//import username and count into sheet, then use query inside sheet to get data
sheet.getRange(2, 17, usernamesArray.length, 1).setValues(usernamesArray);
sheet.getRange(2, 18, usernamesCount.length, 1).setValues(usernamesCount);

Step 8 - Sort by top commenters

To sort from the highest to lowest, I've opted for using the query function inside the sheet grouping by names and sorting by count. I'm referring to column "Q" and "R" as that's where I've inserted the results from the last fetch (e.g. column 17 & 18).

group by Q
order by sum(R) desc
label Q 'Username', sum(R) 'Comments'

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