A comprehensive guide to extracting Instagram data

Instagram is a social media platform that lets its users publish photos and videos. The app has over 800 million monthly active users. It is mainly used to share personal moments with friends, but there are also some business-related features that can be used for marketing purposes. The Instagram Graph API is a collection of Facebook Graph API endpoints that allow apps to access data in Instagram Professional accounts (both Business and Creator accounts).

To use Instagram API, accounts are accessed indirectly through Facebook accounts so all app users must have a Facebook account in order to interact with the API. Without further ado, let's jump right in.

Step 1: App Registration

To get started, we will first register as a Facebook developer by going to Facebook for Developers and click "Get started". After the registration, we will create our first app under the Facebook Developers. The next thing is to choose our App type, app name and email. Once we've completed the app creation flow, our app will be loaded in the App Dashboard as shown below.

Afterwards, on the dashboard we will then click on setup to add our Instagram account.

Step 2: Authentication

App user authentication is handled through access tokens. We'll be making use of Facebook access token. We can get the access token from Graph API Explorer and as well generate a long-live access token which we can use in our code.

We shouldn't also forget to add permissions which pertains to the data we want to get from Instagram.

This is the permission we added on the Api Explorer page.

instagram_basic instagram_content_publish instagram_manage_insights

Generate Access Token

We'll then proceed to extract our data since we have the access token. The data we'd be generating here is majorly the Daily impressions Data and Daily Total Reach Data from the API.

Impression Per Day

The impression per day which is a metric under the instagram_manage_insights depicts the total number of times your content is displayed to people on Instagram, including repeat views.

Write the following lines of code to extract the data.

Python
#We import our libraries import requests import pandas as pd import json import numpy as np from datetime import datetime, timedelta #we instantiate a time variable,so we could generate date from 2021 to 2019 d0 = datetime(2019, 12,16) d1 = datetime(2021, 9,15) dt = timedelta(days = 10) #Numpy was used to calculate 10 days addition to each date and then save to a list dates = np.arange(d0, d1, dt).astype(datetime).tolist() EndTime=[] #we loop through the dates here to add ten days to each of our dates and make this our end time for i in dates: days=timedelta(days=10) vl=i+days B.append(vl) startTime=[] #we loop through the EndTime list to remove 10 days from the date and make this our Start time. for i in EndTime: b=i - timedelta(days=10) StartTime.append(b)

After we've generated both start time and end time we'll pass in URL. Then convert this individual time to UTC because for us to pass it through the API, it has to be in UTC Format.

Python
#we import the time library import time import datetime convert_EndTime=[] #we loop through the list containing this date and convert each to a UTC format for i in convert_EndTime: unixtime = time.mktime(i.timetuple()) convert_EndTime.append(unixtime) convert_startTime=[] #we loop through the list containing this date and convert each to a UTC format for i in convert_StartTime: unixtime = time.mktime(i.timetuple()) convert_startTime.append(unixtime)
Python
tot_impressions=[] #we create a function here and use the get request to obtain the data and also we pass all our parameters in def impressions(): #we loop through the starttime to pass in the values for i, j in enumerate(startTime): #the url to send to the api the_url = 'https://graph.facebook.com/v12.0/17841425263195637/insights?metric=impressions&period=days_28&since=%s&until=%s'%(j, EndTime[i]) r_response = requests.get(the_url, headers = { 'Authorization':'Bearer '+ 'EAAMtRXnqMyc...', 'Content-Type': 'application/json' } ) #we use json to load this response data reaction_response_data = json.loads(r_response.text) #we use sleep method here because the data we want to get is quite much and we don't want our code to crash time.sleep(2) #we append all data to an empty list we defined earlier tot_impressions.append(reaction_response_data) time.sleep(5)

Data Cleaning

The next step is to perform data cleaning on the data and extract all the necessary columns we need.

Python
End_time=[] Description=[] Impression_values=[] #we loop through the data and append all data we need to separate list for i in tot_impressions: for v in i['data']: Description.append(v['description']) for s in v['values']: End_time.append(s['end_time']) Impression_values.append(s['value']) import pandas as pd #We then save a data as a dataframe dataframH= {'No_of_Times_Media_Account_viewed_Daily': Impression_values,'Time':End_time} dfH = pd.DataFrame(dataframH)

Daily Total Reach Data

The Total Reach on Instagram refers to the total number of people who see your content, regardless of how many times they view it or whether they interact with it.

Write the lines of code below to extract this data.

Python
#We create our function here and initiate an empty list to save our response tot_reach=[] def reach(): #We loop through the starttime and pass it under our metric and as well the End time for i, j in enumerate(startTime): #the url to our api the_url = 'https://graph.facebook.com/v12.0/17841425263195637/insights?metric=reach,profile_views&period=day&since=%s&until=%s'%(j, EndTime[i]) #we use the GET Request method to extract our data r_response = requests.get(the_url, headers = { 'Authorization':'Bearer '+ 'EAAMtRXnqMyc...', 'Content-Type': 'application/json' } ) #we use json to load the data response reaction_response_data = json.loads(r_response.text) time.sleep(2) print('getting_data') #we append all data to an empty list tot_reach.append(reaction_response_data) time.sleep(5)

Data Cleaning

We will clean the total reach data using the following code. Afterward, separate each column individually and then combine to one DataFrame.

Python
a_bucket=[] #we loop through our response data for i in tot_reach: for k in i['data']: a_bucket.append(k) #we create the size of the data here and opened an empty list for each column we need a_size=len(a_bucket) title=[] values=[] Endtime=[] #we loop through the data using the size of the our data and then append the data to individual list created for i in range(0, a_size): title.append(a_bucket[i]['title']) values.append([m['value'] for m in a_bucket[i]['values']]) Endtime.append([m['end_time'] for m in a_bucket[i]['values']]) #we convert this data to a dataframe datafram_r = {'title': title, 'Nvalues': values,'Time':Endtime} df_R = pd.DataFrame(datafram_r) #Some columns in this dataframe contains data which are in a single list #We will make each data to be row by row format rows=[] #we will loop through the column we're interested in working with and use lambda to handle this _df= df_R.apply(lambda row: [rows.append([row['title'], nn]) for nn in row.Nvalues], axis=1) df_new_1 = pd.DataFrame(rows, columns=['title','Total_No_of_Reach_per_day_from_users']) rows_1=[] _df= df_R.apply(lambda row: [rows_1.append([row['title'], nn]) for nn in row.Time], axis=1) df_new_2 = pd.DataFrame(rows_1, columns=['title','Time']) df_new_1['Time'] = df_new_2['Time']

Conclusion

Data analytics are very useful in understanding what posts are more successful than others, identifying key influencers, and analyzing the demographics of the page's followers. These insights can be used to create effective campaigns that resonate with this audience.

Social media is constantly changing with new features coming out every year. It is important to stay on top of these changes in order to maximize your ROI on social media marketing campaigns. Thanks for reading.

Originally published on Medium. Read the original article →
Need help with your data?

Let CNDRO build the data infrastructure your business needs

From API integrations to analytics pipelines — we engineer the backend so you can focus on growth.

Book a free consultation