Last active
July 17, 2018 20:22
-
-
Save zahhar/75951294455d062dc8288363d6f1865e to your computer and use it in GitHub Desktop.
Google Spreadsheet script for Instagram metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Instagram') | |
.addItem('Update metrics', 'appendInstagramMetrics') | |
.addToUi(); | |
} | |
function appendInstagramMetrics() { | |
var today = Utilities.formatDate(new Date(), "GMT+2", "dd.MM.yyyy HH:mm"); | |
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(function (s) { | |
s.appendRow([today].concat(getInstagramMetricsForUser(s.getName()))); | |
var lastCell = s.getRange(s.getLastRow(), s.getLastColumn()); | |
lastCell.setFormulaR1C1("=(R[0]C[-3]-R[-1]C[-3])"); | |
}); | |
} | |
function getInstagramMetricsForUser(username) { | |
var url = 'https://www.instagram.com/'+username+'/'; | |
var page = UrlFetchApp.fetch(url).getContentText(); | |
var html = HtmlService.createHtmlOutput(page).getContent(); | |
var regExp = new RegExp(/<meta property=\"og:description\" content=\"([\d,]*) Followers, ([\d,]*) Following, ([\d,]*) Posts(.*)\" \/>/); | |
var matches = regExp.exec(html); | |
matches.forEach(function (m, i) { | |
matches[i] = m.replace(",",""); | |
}); | |
return [matches[1],matches[2],matches[3]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How-to
After some idle time you should see each sheet will populate automatically with number of Followers, Following and Posts particular account had at the specific date and time, and calculated Follower Delta between calls.
Enjoy!