Import Facebook Page Data to Google Sheets

Facebook Pages serve as a business presence for many companies, displaying product info, reviews, posts, and more. In this guide, we’ll walk through how to pull Facebook fan page data from the Facebook API directly into Google Sheets, using the API Connector add-on for Google Sheets.

Note that the process for getting Facebook access tokens isn’t difficult, but it is quite long and convoluted as there are several kinds of tokens: User Access Tokens, App Access Tokens, and Page Access Tokens. Further, tokens are valid for different lengths of time, and Page Access Tokens are dependent on User Access Tokens. There are many different approaches to retrieve these tokens, but the following method should be fairly straightforward.

PART 1: GET YOUR FACEBOOK API USER ACCESS TOKEN

  1. Begin by navigating to https://developers.facebook.com/, and click My Apps.
    fb-page-img1
  2. Click + New App
    fb-page-img2
  3. Facebook will ask you how you’re using your app. Choose “For Everything Else”.
    fb-page-img3
  4. Enter an App Display Name. You can call your app anything, as long as it doesn’t include any terms related to Facebook’s trademarks. We’ll just call our app Pages for Sheets. Enter a contact email address and click Create App ID.
    fb-page-img4
  5. You’ll be directed into your Developer dashboard. You don’t need to do anything in this dashboard. Instead, open up the Facebook Graph API Explorer
  6. Under ‘Facebook App’, select the app you just created. Click Get User Access Token from the drop-down menu.
    fb-page-img5
  7. A modal will appear, prompting you to login. Click Continue.
    fb-page-img6
  8. Under ‘permissions’, you should now see ‘public_profile‘, meaning your token provides access to public information about yourself. Click the dropdown menu and add the permissions ‘pages_read_engagement‘ (located under Events Groups Pages) and ‘read_insights‘ (located under ‘Other’).
    fb-page-img7
  9. Now click Generate Access token.
    fb-page-img8
  10. You’ll see a couple modals prompting you to continue and to allow your app to access your Facebook Page and App insights, just click through these until you see a notification that the linking is complete.
    fb-page-img9
  11. You should now see your new access token in the Graph API Explorer. However it’s a short-lived access token and only lasts for 1 hour. We can extend it by clicking the small blue icon next to it, and then clicking Open in Access Token Tool:
    fb-page-img10
  12. At the bottom of this page, click Extend Access Token.
    fb-page-img11
  13. This will produce a long-lived User token. Click Debug to pull up a screen where you can copy it to your clipboard.
    fb-page-img12

PART 2: GET YOUR PERMANENT PAGE TOKEN

  1. Return to the Facebook Graph API Explorer. Paste the long-lived User token you just got into into the “Access Token” field.
    fb-page-img13
  2. Now in the URL field, erase what’s already there and type in YOUR_PAGE_ID?fields=access_token. You can find your Page ID by navigating to your Facebook page, clicking About, and scrolling down until you see ‘Page ID’. Click Submit.
    fb-page-img14
  3. You’ll now see a new token returned. This is your permanent Page token! Finally!! That means you’ll never have to go through the above steps again, so copy it down and keep it safe.
    fb-page-img15
  4. You can verify that this token is permanent by copying and pasting it into the Access Token Debugger. Click Debug, and make sure you see the following:
    • an App ID. You should your app listed at the top. If this is missing, you missed clicking through some of the required modals earlier on and need to go through these steps again 🙁
    • Type = Page (not User). You’ll need a page token to get access to all the page data.
    • Expires = Never. Even though it says Data Access Expires in 3 months, the permissions we’ve requested are explicitly exempted from this (documented here).
      fb-page-img16

PART 3: CREATE YOUR API REQUEST URL

We’ll first follow the Facebook documentation to access the latest posts from your Page.

  • API Root: https://graph.facebook.com/vVERSION_NUMBER
  • Endpoint: /YOUR_PAGE_ID/feed
  • Query Strings: ?access_token=YOUR_PAGE_ACCESS_TOKEN&limit=100

The “limit” parameter is optional but can be used to return more records (otherwise the default is 25). Putting it together, we get the full API Request URL:

https://graph.facebook.com/v8.0/507738226062106/feed?access_token=EAA...&limit=100

Just substitute in your own Page ID and Page Access Token.

PART 4: PULL FACEBOOK PAGE DATA INTO GOOGLE SHEETS

We can now enter all our values into API Connector and import Facebook Page Post data into Google Sheets.

  1. Open up Google Sheets and click Add-ons > API Connector > Create New API Request.
  2. In the Create Request interface, enter the Request URL we just created. For convenience, I’ve included my token by referencing it in a cell. Leave the Headers selection blank, set a data destination, and click Run. A moment later you should see your most recent posts populate your Google Sheet.
  3. Pay attention to the data » id field as you can use that to associate views, likes, and other metrics to the post titles.

PART 5: MORE EXAMPLE API URLs

You can check the page insights and page feed documentation for full information on setting up your queries,  but if you just want to jump in, you can play around with the URLs you enter in the API URL path field. Try the following examples to get an idea of how it works.

Post Metrics (metrics about individual posts)

Get lifetime impressions, likes, and clicks for multiple Facebook posts in a single call. As mentioned above, you can use the data » id field as a key to match up metrics to the post title.

https://graph.facebook.com/v8.0/YOUR_PAGE_ID/posts?fields=insights.metric(post_impressions,post_reactions_like_total,post_clicks)&access_token=YOUR_TOKEN

Note: this request was run in “compact” mode to make it easier to read the list of metrics. The “single row” report style would display each metric in its own column.

fb-page-img18

Page Metrics (metrics about the whole page)

Retrieve most of the default metrics from your FB page’s Insights Overview summary tab

When you navigate to www.facebook.com/YOUR_PAGE/insights/, you should see an insights summary page like this

fb-page-img20

You can use the following API request URL to pull in most of these metrics to your sheet:

https://graph.facebook.com/v8.0/YOUR_PAGE_ID/insights?metric=page_total_actions,page_views_total,page_preview_total,page_fan_adds_unique,page_impressions_unique,page_post_engagements,page_video_views,page_daily_follows_unique&date_preset=yesterday&period=week&access_token=YOUR_TOKEN
fb-page-img21

Get the count of total FB page likes from the API

https://graph.facebook.com/v8.0/YOUR_PAGE_ID?fields=name,fan_count&access_token=YOUR_TOKEN

In some of the examples above, you can see parameters for ‘date_preset’ and ‘period’. ‘Date_preset’ lets you select a date range for reporting, such as today, yesterday, this_month, last_month, etc. ‘Period’ allows you to aggregate data over certain predefined periods (day, week, days_28, month, lifetime).

PART 6: HANDLE PAGINATION

  1. By default Facebook 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. The /feed and /posts endpoints have maximum limits of 100, so if you still need to retrieve additional records after that, you would page through the records with the ‘after’ parameter like this:
    Page 1: https://graph.facebook.com/v8.0/act_YOUR_PAGE_ID/feed?access_token=YOUR_ACCESS_TOKEN&limit=100
    Page 2: https://graph.facebook.com/v8.0/act_YOUR_PAGE_ID/feed?access_token=YOUR_ACCESS_TOKEN&limit=100&after=Q2c4U1pXNTBYM0Y

    The cursor value for the after parameter is printed into the response of the first results page, in the “paging » cursors » after” field. Note that Facebook provides several different methods of pagination, including cursor-based pagination, time-based pagination, and offset/limit-based pagination. This example shows cursor-based pagination, which is Facebook’s recommended method.

  2. In API Connector you can run these paged requests separately, or loop through them automatically using pagination handling (paid feature).
    fb-ads-img21

44 thoughts on “Import Facebook Page Data to Google Sheets”

  1. Is there a way to bring in insight information alongside the individual posts? So, after section 4 when you bring in all of the latest Facebook posts, is it possible to have additional columns with impressions, post likes etc?

    Reply
    • Hi Michael, you can do that by creating an API URL like this:

      https://graph.facebook.com/YOUR_PAGE_ID/posts?fields=insights.metric(post_impressions,post_reactions_like_total)&access_token=YOUR_PAGE_TOKEN

      Substitute in your own page ID and access token, and you’ll see a list of all your page posts alongside impressions and likes. You can see a list of all available metrics here: https://developers.facebook.com/docs/graph-api/reference/v3.0/insights#availmetrics

      Note that the page posts will be identified with an ID under the field data » id. To link the IDs back to a name, you can run the request described in Part 3 above, since that contains both the post ID and the post name.

      Reply
    • The regular /feed endpoint will include data for any videos you’ve posted, but if you want videos only, you can make a request to the /videos endpoint. You can add in the fields you want as described in the video endpoint documentation at https://developers.facebook.com/docs/graph-api/reference/video/.
      For example, a request to this URL

      https://graph.facebook.com/v5.0/{account}/videos?fields=title,description,embed_html,length,picture&access_token={token}

      would return the titles, descriptions, HTML, video length, and thumbnail for all your videos. There’s also a ‘content_tags’ parameter listed in the documentation, but it only returns a numeric value so I’m not sure how useful it is.

      Reply
  2. Amazing, thank you, I’ll give it a try! I’ve scheduled the data to run every hour but it’s giving me the whole output again, instead of just anything new – is it possible to change this?

    Reply
    • API Connector doesn’t check the data before printing it into the sheet, so you can either use append mode and construct your URLs so that they only pull in new data (most APIs provide some mechanism of paging where you limit data pulls by date or ID), and/or you can use Google Sheet functions like UNIQUE() to dedupe on the front end.

      Reply
  3. First off just want to say this is a brilliant add-on!

    I’m having a go at bringing in Facebook video insights, but it’s displaying in a weird way:

    Using the exmapl;e posted above bringing in video insights, it’s displaying them very strangely, almost in multiple rows rather than columns folr each video, so it’;s mixing up the data?

    Reply
    • Thanks a lot, I appreciate that : ) As for mixing up the data, I’m not exactly sure what you’re seeing. To me it looks all right (if complicated), with each metric name followed by its count in the subsequent column. You can try changing the Report Style for a different JSON-to-Sheets algorithm, or limit the number of metrics you pull in at once so that the output doesn’t get so complex. This API wasn’t designed to be consumed in Sheets, so Facebook didn’t put much thought into producing JSON that looks clean when it’s flattened out.

      Reply
  4. Hi Ana,

    Great article, thanks. Just one question. I’ve seen that Facebook requested you for some permissions that you didn’t have (manage_pages and read_insights). How it was possible to move on without this permissions?

    Thank you!

    Reply
    • Hey John, those permissions are only required if you’re developing and publishing an app for other people to use (documentation). So basically we don’t need to worry about it because we’re the only ones using our apps.

      Reply
  5. Any ideas how to convert the datetime from the first column to normal date and time columns in sheets? Facebook API spits out YYY-MM-DDTHH:MM:SS+0000 (HHs in UCT). I would love to convert to local date and time, but the only hypothetical solution I’ve come up with is pretty complicated.

    Reply
  6. The column “comments » data » from » name” is blank for me. Is there a way to get the name of the Facebook user that left the comment?

    Reply
    • I checked the documentation and it seems that the name field should contain the name of the user who left the comment. If it’s blank, that means it’s not available for some reason, I suspect it’s related to some privacy-related changes.

      Reply
    • Good question. I thought it could be achieved by including period=weekly, but that still returns a value for each day instead of simply aggregating it by week. So the following query might be easiest. It will return new likes by day, and then you can sum it on your end to get a weekly value.
      https://graph.facebook.com/v6.0/PAGE_ID/insights/?metric=page_fan_adds_unique&date_preset=this_year&period=day&access_token=TOKEN
      Btw, I suggest switching to compact mode to make the response output easier to read.

      Reply
  7. Hi thanks for do this content im very apreciate and i have a feedback for you in the step 1 part 6
    Old Permission =manage_pages now is

    pages_manage_ads

    pages_manage_metadata

    pages_read_engagement

    pages_read_user_content

    Reply
  8. Hi,

    I like the simplicity of the tool for setting it up to get the Facebook Data, I am running into a problem, and I tried a few times today.

    Request failed: DNS error:

    Thanks,
    Neil

    Reply
    • A DNS error means the server isn’t responding, but I think it’s very unlikely that Facebook’s server has a problem. Therefore, I’d double-check that you’ve entered the correct hostname. Please feel free to share your sheet or send me a screenshot of your setup if you’d like me to take a look.

      Reply
  9. Hi Ana, I need a paid service from you which is: a simple app that exports all my Facebook page likers to Google spreadsheet. Can you do that to me ? thanks

    Reply
    • Hi there, I don’t believe the Facebook API provides this data. If you check https://developers.facebook.com/docs/graph-api/reference/v7.0/object/likes, it says “Due to privacy concerns, a User or Page can only query their own likes.”
      You can still get this data from the interface by opening Facebook, navigating to your Page, clicking Settings, and then clicking the People and Other Pages tab in the left column. You can probably find some apps that automate this process through web scraping, but I haven’t looked into it.

      Reply
  10. Hi Ana, thank you very much for this article. It s so cool your step by step.

    I was just wondering… is the free API able to pull public information from other profiles than mine? I mean number of posts and number of fans…

    Thanks for sharing! 🙂

    Reply
    • Thank you for the comment, I’m glad you like the article! That’s a good question. The method above does NOT let you get data from other profiles, it’s only for pages of which you’re an admin. Public data is only available if you create an app that a) requests Page Public Content Access permission and b) successfully passes Facebook’s app review process (info).

      Reply
    • Glad you liked it! The first example in the article (https://graph.facebook.com/v7.0/507738226062106/feed?access_token=EAA...) returns post ID, title, and date, are you looking for something else?

      Reply
      • Yes. But I have an error When i’m trying to run it. The error message is below. What is wrong here? 🙁

        {“error”:{“message”:”(#100) Pages Public Content Access requires either app secret proof or an app token”,”type”:”OAuthException”,”code”:100,”fbtrace_id”:”XXXXXXXXX_XXXX”}}

      • Can you please let me know which URL are you using? The method above doesn’t let you get data from other profiles, it’s only for pages of which you’re an admin.

    • Do you mean you’re getting an error message or it says “no records found”? If it’s an error, what does the error message say? What URL are you running?
      If there are no records, then it probably means you’re querying a page with no data for that time period. In that case, please adjust your page ID and date settings to ensure data is available. You can check in the interface to verify.

      Reply
      • There is no error – when I click run there is simply nothing showing. When you mean that I’m querying a page with no data for that time period, may I know what is the default data period?

      • The default date is the past 30 days. If no data is returned, API Connector should say “records processed, no data found” at the bottom, you’re not seeing anything at all? In that case it sounds like it’s an error I’ve never heard of before. I know Google Drive is having a big outage right now, maybe it’s related. Can you please try again a bit later and let me know what happens?

Leave a Comment