Search API Connector Documentation

Print

Import Instagram Data to Google Sheets

In this guide, we’ll walk through how to pull Instagram Insights data data directly into Google Sheets, using the API Connector add-on for Sheets. We’ll be connecting using API Connector’s built-in OAuth2 integration for Instagram Insights (this is a paid feature, please install API Connector for a free trial or upgrade to access).

Note that to use the Instagram Insights API, you must have an Instagram Business account that is connected to a Facebook Page, and you must be an admin of that Facebook page. If you need help, this guide provides information on converting your personal Instagram account to a business account.

CONTENTS

BEFORE YOU BEGIN

Click here to install the API Connector add-on from the Google Marketplace.

PART 1: CONNECT TO THE INSTAGRAM INSIGHTS API

If you haven’t connected to the Instagram Insights API before, you’ll first need to initiate the connection.

  1. Open up Google Sheets and click Add-ons > API Connector > Manage Connections.
  2. In the list of available connections, find Instagram Insights and click Connect.
    instagram-img2
  3. You will be directed to Facebook and asked to choose which Instagram accounts and Facebook Pages you’d like to use with API Connector. If you are an admin of multiple accounts, make sure to choose the Instagram account and Facebook page that you’ve previously linked together. Click Done.
    instagram-img3
  4. You’ll then be returned to your Google Sheet, and can verify that your Instagram Insights connection is active in the Connections screen.
    instagram-img4

PART 2: CREATE YOUR INSTAGRAM API REQUEST URL

For our first request, we’ll get some basic information about your Instagram account.

  • API root: https://graph.facebook.com
  • Endpoint: /me/accounts (you can optionally include an API version number here. API Connector will automatically route requests to the most recent API version, even if you manually enter a lower version)

Putting it all together, we get the full API Request URL.

https://graph.facebook.com/me/accounts

PART 3: PULL INSTAGRAM API DATA INTO SHEETS

We can now enter our values into API Connector and start importing Instagram data into Google Sheets.

  1. Back in the Create Request interface, enter the Request URL we just created above.
    instagram-img1
  2. You don’t need any headers so just leave that section blank.
  3. Under Authentication, choose Instagram Insights from the dropdown menu.
    instagram-img5
  4. Create a new tab and click ‘Set current’ to use that tab as your data destination.
  5. Name your request and click Run. A moment later you’ll see some information about your Instagram account populate your sheet. Pay special attention to the value located in the data » id field as you’ll need it in the next request.
    instagram-img6
  6. Now let’s use that ID to get your Instagram Business account ID, because we’ll need that for all requests to the /insights endpoint, which is what we’re here for. In a new request, create an API URL like this, substituting in your own data ID.
    https://graph.facebook.com/YOUR_DATA_ID?fields=instagram_business_account
  7. Just as you did before, choose Instagram Insights from the Authentication menu (you’ll need to do this for all requests to the Instagram API) and click Run.

    You should now see your Instagram Business account ID printed into the sheet. Keep this handy, we’ll use this ID to get all our Instagram stats!
    instagram-img7

PART 4: EXAMPLE API URLS

You can access the Instagram Insights documentation here, but if you just want to get started, you can try the following example URLs, substituting in your own business account ID.

ACCOUNT/USER METRICS

  • Key metrics about an account (id, followers count, following count, etc)
    https://graph.facebook.com/v11.0/YOUR_BUSINESS_ACCOUNT_ID?fields=id,ig_id,followers_count,follows_count,media_count,name,profile_picture_url,username
    instagram-img10
  • Impressions and reach for your Instagram account, for the past 28 days. (Note that the Instagram Graph API returns 2 sets of values, one for the period ending today, and one for the period ending yesterday.)
    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=impressions,reach&period=days_28
    instagram-img8
  • Users who have viewed your Instagram business account in the last day.
    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=profile_views&period=day
  • A breakdown of cities where your Instagram followers are located
    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=audience_city&period=lifetime
    instagram-img9
  • Count of new followers from the prior day
    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=follower_count&period=day
  • Metrics by day for a date range (the “since” and “until” parameters contain the start and end dates)
    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/insights?metric=impressions,reach,profile_views&period=day&since=1577836800&until=1580428800
The Instagram Insights API requires dates to be entered as UNIX timestamps. In Sheets, you can convert a regular date in cell A1 to a UNIX timestamp with the formula =(A1-DATE(1970,1,1))*86400.

If you want to make a dynamically updating date range, add a dynamic date formula like =today()-1 to one cell, add a second cell that converts it to UNIX with the =(A1-DATE(1970,1,1))*86400 formula, and then reference that UNIX timestamp in your API request URL. Every time the date changes, your URL will update automatically.

MEDIA METRICS (metrics about posts and other content)

  • List of post IDs (use these post IDs in the following API calls). By default this will only return 25 records, so please check the section on pagination if you need to retrieve more than that.
https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/media
  • Engagement metrics for a specific post (lifetime totals only)
https://graph.facebook.com/POST_ID/insights?metric=impressions,reach,engagement,saved
  • Engagement metrics for a list of posts (lifetime totals only)

If you want to pull engagement or other metrics for a list of post IDs, you can cycle through each ID by referencing the cells containing post IDs in your request URL. For example, if your list of post IDs is in a sheet called PostIDs, you’d make a query like this:

https://graph.facebook.com/+++PostIDs!A2:A100+++/insights?metric=impressions,reach,engagement,saved
  • Likes, comments, URL, and other key metrics for a specific post
https://graph.facebook.com/v11.0/POST_ID?fields=comments_count,id,ig_id,is_comment_enabled,like_count,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username


instagram-img11

HASHTAG SEARCH

  • Find a hashtag ID:
https://graph.facebook.com/v11.0/ig_hashtag_search?user_id=YOUR_BUSINESS_ACCOUNT_ID&q=sunset

That will return the ID for your hashtag (“sunset” in this example). Once you have your hashtag ID you can use it to create the next request:

  • Search for hashtags
https://graph.facebook.com/v11.0/HASH_TAG_ID/top_media?fields=id,caption,comments_count,like_count,media_type,media_url,permalink,timestamp&user_id=YOUR_BUSINESS_ACCOUNT_ID

PART 5: HANDLE PAGINATION

  1. By default Facebook’s graph API, which includes Instagram, limits the number of results in a single response (usually to 25 records) as described here: https://developers.facebook.com/docs/graph-api/using-graph-api/#paging. To get more records, you can add the ‘limit’ parameter (e.g. &limit=100) to the end of your URL (you can try a larger limit value, I’m not sure what the maximum is).
  2. If you still have more records after that, Facebook prints the URL for the next page of results into the paging » next field.
    instagram-img12
  3. In API Connector you can loop through these pages automatically using pagination handling, like this:
    • API URL: enter your request URL as usual, making sure to include limit=100
    • Pagination type: next page URL
    • Field name: paging.next
    • Number of pages: enter the number of pages you’d like to fetch
      instagram-img13

PART 6: USEFUL LINKS

There are two very useful endpoints for Instagram analytics: /{ig-media-id} (metrics on a media object) and /{ig-user-id} (metrics on an Instagram Business or Creator Account). Each endpoint has different available metrics, as described here:

Available Media Metrics:

Available User/Account Metrics:

Available Hashtag Metrics:

Previous Import Hunter Data to Google Sheets
Next Import Intercom Data to Google Sheets

26 thoughts on “Import Instagram Data to Google Sheets”

  1. Hey Ana,

    how can i see a day by day breakdown of the last 90 -120 days? i tried to use the “data_preset=last_90d” from FB ads account request, but it didn’t work.

    thank you so much for this post/content.
    – Leo.

    Reply
    • Hi Leo, thank you for the comment. To get data broken down by day you need to use the period=day query parameter. Then, to specify a time range, you need to use the since and until parameters, as a UNIX timestamp as described above. Unlike Facebook’s Insights API, the Instagram Insights API doesn’t support the date_preset parameter, so you need to explicitly specify the date.

      One more wrinkle: in my tests, it seems like since/until can only fetch 30 days at a time, though the documentation doesn’t state that clearly. So, in conclusion, you need to make a few different fetches to retrieve your data. If you plug in your dates and the UNIX conversion formula into a sheet called Inputs, then the following should work (just change your date inputs each time and append data into the same sheet):
      https://graph.facebook.com/v8.0/11111111111111111111/insights?metric=impressions,reach,profile_views,follower_count&period=day&since=+++Inputs!C2+++&until=+++Inputs!C3+++

      Reply
      • Hey Ana, thank you so mucho for your reply.

        I’ve been trying to get the data but i keep getting the same error message: “Request processed. No records found”. In this case it’s unlikely given that the account is al least 2 years old. i’ve reduced the date range, used the Inputs sheet as you recommended, but still can’t get the data.

        have you run into this problem in the past?

      • Hi Leo, sorry it’s still not working. I haven’t seen this error with this specific API, but I’ve definitely run into this type of problem before (I think I’ve run into every possible problem at this point :p)

        Are you able to get data from any of the requests? For troubleshooting something like this, it’s usually best to start off with very simple requests (e.g. one day, one metric) and add complexity from there. That way we can try to identify (or rule out) if it’s related to the account ID, to the metric combination, to the date range, etc. If you can share your URL or sheet, I’ll be happy to take a look as well.

  2. Hi, Ana! Is it possible to recieve aggregated engagement data from all posts?

    I use this request – https://graph.facebook.com/v8.0/ig-user-id/insights?metric=engagement&period=day

    but it shows an error:

    “metric[0] must be one of the following values: impressions, reach, follower_count, email_contacts, phone_call_clicks, text_message_clicks, get_directions_clicks, website_clicks, profile_views, audience_gender_age, audience_locale, audience_country, audience_city, online_followers”

    As i understand it works only with ig-media-id instead of ig-user-id. In this way how to create automated scheduled reports for engagement metric?

    Reply
    • Hey Michael, based on what I see here, it looks like you can only get it for individual posts, not aggregated at an account level. So I think you have to do the following:
      1) get a list of your post IDs by running this query: https://graph.facebook.com/v9.0/YOUR_ACCOUNT_ID/media

      2) now substitute in those IDs into this query: https://graph.facebook.com/v9.0/YOUR_POST_ID/insights?metric=engagement,impressions,reach

      To run through a list of IDs, you can reference a range of cells like this: https://graph.facebook.com/v9.0/+++IDs!A2:A10+++/insights? metric=engagement,impressions,reach

      Then you will have engagement metrics for all the posts on your list, and can calculate the overall rate yourself. Can you please check if that would work for you?

      Reply
      • It works, but only for a lifetime period.

        I tried to add ‘period’ or ‘since/until’ to the queries, but it shows incompatible error. It looks like there’s no way to get monthly or daily engagement because of Instagram API limitations.

        But thank you anyway for answers!

      • Bummer, I just investigated this further and confirmed your findings. The Instagram API doesn’t provide any way to slice up the media insights by time, it’s all lifetime totals only. The /{ig-user-id}/insights endpoint does provide time slicing capabilities, but you can’t get the engagement metric there.
        Sorry for the inconvenience, I’ll update the post with some of these findings.

    • Hey Eddy, I don’t have any templates for Instagram but am happy to help you set up some requests to get data. Are there specific metrics you’re looking for?

      Reply
  3. Hi!
    Thank you for your post. I’m having the following error starting Part IV

    Status
    Completed with errors
    – Server responded with an error (400) show response

    ‘show response’ = {“error”:{“message”:”(#100) Tried accessing nonexisting field (insights) on node type (Business)”,”type”:”OAuthException”,”code”:100,”fbtrace_id”:”AdvCGsGT8eVdy5vECPellEV”}}

    Reply
    • Hi Leandro, I haven’t seen this error before so I can’t say for sure, but based on that error message it sounds like Facebook’s API has a problem providing insights data for that account ID. I would double-check some of the requirements: can you please confirm that your Instagram account is a public business or a creator account, that it’s connected to a FB page that you’re the admin of, and that the Instagram account has more than 100 followers?

      Reply
  4. Hello!
    Thank you for this great resource. I’m having quite a bit of trouble with something quite simple, however. I simply want to use the API to find the total number of followers an account (in this case my own) has.

    I feel as though I am missing something very obvious, help would be very appreciated.

    Thank you in advance.

    Reply
    • Hey there, you’re not missing something obvious, they’ve just hidden it deeply in the documentation :p. You can use this request URL to get total followers:
      https://graph.facebook.com/v9.0/YOUR_BUSINESS_ACCOUNT_ID?fields=followers_count
      I’ll update the article to make that more clear.

      Reply
  5. Hi!
    I’m really new to this, so I don’t really understand: is there a way to create automated scheduled reports? I’m trying to create a graph with monthly data, but I don’t know if I should simply do it by the end of the month or I should collect data daily.

    Thanks,
    Trinidad

    Reply
    • Hi Trinidad, sure, you can check this article on scheduling to see how you’d set up your request to refresh every month. So you could run a request like https://graph.facebook.com/v10.0/1111111111111111?fields=id,ig_id,followers_count,follows_count,media_count,name,profile_picture_url,username and then save all the values at the end of each month and re-run the query for new values. You can save the data manually or automate that monthly saving process with my other (totally free) addon Archive Data.

      Also, if you’re asking about getting historical data, that’s harder as the API only provides historical data for certain metrics. So feel free to provide more detail on what exactly you’re looking for.

      Reply
  6. Hi

    I’m having trouble getting all of the post ID of an Instagram account.

    ・I used the below API URL Path, and managed to get 25 id’s but how can I get more then 25 id’s? Is their a number limitation?

    https://graph.facebook.com/YOUR_BUSINESS_ACCOUNT_ID/media

    The output sheet has 4 columns as below.

    data » id
    paging » cursors » before
    paging » cursors » after
    paging » next

    In the “paging » next” column there is a URL and the below message is inside it.

    {
    “error”: {
    “message”: “A user access token is required to request this resource.”,
    “type”: “OAuthException”,
    “code”: 102,
    “fbtrace_id”: “ANq-J-qhCojmu43CABXZ02e”
    }
    }

    Reply
    • I added a section on pagination handling to the post, please check that and let me know if that successfully pulls in more records.

      Reply
      • Hi Thanks for the reply

        I made the change and the number increased from 25 to 39..
        but there are over 700 posts on the actual account.

        Let me know what i need to do in order to get at least 200…

      • I checked your sheet and see 700+ posts as expected, can you please let me know if you’re still having issues?

  7. Hello, I am testing your connector and it looks incredible.
    Could you please help me with one thing.
    Requests of this kind https://graph.facebook.com/v11.0/111111111111111111?fields=id,media_type,media_url,owner,timestamp are ok until I add media_product_type.

    With media_product_type I receive an error:
    Server responded with an error (400) show response{“error”:{“message”:”(#12) introduce IGTV to instagram graph api requires version v10.0 or higher”,”type”:”OAuthException”,”code”:12,”fbtrace_id”:”AZWd7Ax4ws56yEzztnzfdWr”}}

    How can I update instagram graph api to latest version?

    Reply
    • I see, it looks like that metric is a new metric available only in the newest API version. However, I’m not sure why you’re seeing this error, it worked fine for me when I tried https://graph.facebook.com/v11.0/1111111111111111?fields=id,media_type,media_url,owner,timestamp,media_product_type. I’ll look into this some more to see if I can replicate it.

      Reply
    • In theory this is possible as Instagram provides a messaging API. However, the method in this article won’t let you connect to it since messaging and insights require different permissions (Facebook / Instagram approve integrations only for the specific permissions requested). I’ll look into creating an integration for messaging in the future.

      Reply

Leave a Comment

Table of Contents