How to automatically monitor Instagram Hashtags for free with a Google Sheets Script

Thomas Strosse
5 min readNov 12, 2018
“woman sitting beside man on table using laptop inside room” by rawpixel on Unsplash

Earlier this year I published my most-applauded-for piece of content on Medium yet:

Today I’m sharing something that might also add some value to your Instagram life: a script that tracks certain hashtags on a daily basis.

With this script, I’m just tracking a small amount of “metrics” or values:

  • Current date
  • Hashtag name
  • Media count
  • Most recent post date
  • URL to hashtag

I only took a handful of values to show, because when it comes to hashtags there aren’t that many metrics available.
You could figure out how to calculate engagement for certain hashtags. But with the personalization that’s being applied to the top posts and the speed by which certain hashtags get new posts, these results can’t be seen as the universal truth.

Part 1: Setting up the script. Let’s get to work!

Now that we’ve touched on the data being provided, let’s get to work and start building out or spreadsheet and Google Script.

Open up a new Google Sheet and add the following headings to the columns on the first row (1):

  • Date
  • Hashtag
  • Media count
  • Most recent post date
  • URL to hashtag
I should look like this (we’ll get to the data soon, I promise!)
  • Next, in the menu open Tools -> Script
  • Copy the script and change two variables:
    - sheetName (change the values between the “”, this should be the name of the sheet you want to import the data to). By default, the sheet’s name is “Sheet1”.
You’ll find the sheet name at the bottom of the spreadsheet

- hashtagArray : enter every hashtag you want to monitor.

Change these values

A small warning though: Google Sheets only allows for a maximum execution time of 2 minutes per script. So be sure not to add too many hashtags.

function MultipleHashtags() {
// the name of the sheet within your document, by default this is: "sheet1".
var sheetName = "Sheet1";
//The hashtags you want to track
var hashtagArray = ["hashtag1","hashtag2","hashtag3","hashtag4"];

for(var i = 0; i < hashtagArray.length; i++) { insertData(sheetName, hashtagArray[i]); };

// Get Date field filled
function insertData(sheetName, Hashtag) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
sheet.appendRow([Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"), PostTagName(Hashtag), getPostCount(Hashtag), getMostRecentDate(Hashtag), getURL(Hashtag)]);
var range = sheet.getRange("A2:H")
range.sort([{column: 1, ascending: false}]);
Utilities.sleep(250);
}

//Return Hashtag Name
function PostTagName(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
return JSON.parse(response).graphql.hashtag.name;
}

//get Media Count for Hashtag
function getPostCount(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
return JSON.parse(response).graphql.hashtag.edge_hashtag_to_media.count;

}

//Get Most Recent Post Date
function getMostRecentDate(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag+"/?__a=1";
var response = UrlFetchApp.fetch(url).getContentText();
var mostRecent = parseInt(JSON.parse(response).graphql.hashtag.edge_hashtag_to_media.edges[0].node.taken_at_timestamp);
//return mostRecent;
var postTimeArray = convertTimestamp(mostRecent);
var postDate = postTimeArray[0];
var postTime = postTimeArray[2];
return postDate;
}

//save URL for Hashtag
function getURL(tag) {
var url = "https://www.instagram.com/explore/tags/"+tag;
return url
}

}


/**
/ CONVERT IG TO READABLE TIMESTAMP
*/
function convertTimestamp(timestamp) {
var allValues = [];
var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday", "Saturday"];
var d = new Date(timestamp * 1000), // Convert the passed timestamp to milliseconds
yyyy = d.getFullYear(),
mm = ('0' + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
dd = ('0' + d.getDate()).slice(-2), // Add leading 0.
hh = d.getHours(),
h = hh,
min = ('0' + d.getMinutes()).slice(-2), // Add leading 0.
ampm = 'AM',
time;

var dayWord = weekday[d.getDay()];

if (hh > 12) {
h = hh - 12;
ampm = 'PM';
} else if (hh === 12) {
h = 12;
ampm = 'PM';
} else if (hh == 0) {
h = 12;
}

// ie: 2013-02-18, 8:35 AM
time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ' + ampm;
convertedDate = yyyy + '-' + mm + '-' + dd;
convertedDay = dayWord;
convertedTime = h + ':' + min + ' ' + ampm;
allValues.push(convertedDate,convertedDay,convertedTime);
return allValues;
}

Here’s the link to the script on GitHub.

  • Save the script (you’ll be prompted to enter a name).
  • Save the script (you’ll be prompted to enter a name).
  • Open Run -> Run Function -> MultipleHashtags
Run MultipleHashtags

Because of the limitations of this method, there is no historical data available. However, importing everything into Google Sheets will gradually build up the historical data for you. Just give it some time! :).

If you now switch back to the Sheet, you should see something like this:

The first results

Part 2: Adding automation

Of course, having Analytics is only as good as the regularity by which they are collected. Thanks to Google we can fully automate the execution of this script.

  • Click on Edit -> Current project’s triggers
  • Click on create a new trigger and choose a sequence for the script to collect the data. Personally, I prefer to automatically pull data every day from Midnight to 1 am:
Running the script daily at midnight.

And we’re done! The script will now add the new data every day.

Part 3: Known errors

Error: 429 truncated server

This is mainly because Instagram is refusing the connection (due to overuse, speed…), and occasionally this will make the script fail. The only ‘real’ solution I found is to try again a few minutes later.

How would you improve this sheet?

If you’ve made it this far down the post, I want to thank you!

Now comes the inevitable question: How would you further improve this sheet? What’s lacking? Are you adding in more formulas to calculate metrics? Please let me know! I’m dying to find out!

If you need help with implementing it, please don’t hesitate to reach out to me through my website.

--

--