Import Facebook Page Data to Google Sheets

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 > Create App.
    fb-img1
  2. Fill out your details and click Create App ID. You can call your app anything, but in this example we'll call it Google Sheets.
    fb-img2
  3. 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
  4. Click Get User Access Token from the drop-down menu
  5. fb-img3

  6. A modal will appear prompting you to select the permissions you wish to access. For this example, make sure you've at least selected "manage_pages" and "read_insights". Click Get Access Token.
    fb-img4
  7. You'll be prompted to log in and select the pages which you'd like to access via the API.
    fb-img5
  8. Click through, ignoring any prompts to "Submit for Login Review". We don't need to do that here.
    fb-img6
  9. You should now see your first access token. However it only lasts for 1 hour. We can extend it by clicking the small blue icon next to this short-lived access token, and then clicking Open in Access Token Tool.
    fb-img7
  10. At the bottom of this page, click Extend Access Token.
    fb-img8
  11. This will produce a long-lived token. Click Debug to view the whole string and copy it to your clipboard. We'll need it in a moment.
    fb-img9

PART 2: GET YOUR PERMANENT FACEBOOK API PAGE ACCESS TOKEN

  1. Return to the Facebook Graph API Explorer. Paste the long-lived token you just got into into the "Access Token" field.
    fb-img10
  2. Enter {page ID}?fields=access_token into the URL field, making sure to substitute in your own page ID where it reads {page ID}. To get your page ID, open up your Facebook page and click About on the left-hand menu. Scroll down and you will see Page ID listed. In this example it would be 507738226062106?fields=access_token, as shown in the screenshot below. Click Submit.
    fb-img16
  3. You will see your permanent Page Access Token returned in the response. Congrats! This means you won't need to go through the above process again.
    fb-img17
  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 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-img18

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/{version#}
    API Root example: https://graph.facebook.com/v5.0
  • Endpoint: /{Page ID}/feed
    Endpoint Example: /507738226062106/feed
  • Query Strings: ?access_token={Page Access Token}
    Query Strings Example: ?access_token=EAABhZAIIaUQkBAFCQN5zuq1vYgqWdecg1...

Putting it together, we get the full API Request URL:
https://graph.facebook.com/v5.0/507738226062106/feed?access_token=EAABhZAIIaUQkBAFCQN5zuq1vYgqWdecg1...
Don't forget to substitute in your own Page ID and Page Access Token.

PART 4: ENTER YOUR VALUES INTO API CONNECTOR

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. 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.

    fb-img11

PART 5: FACEBOOK API + GOOGLE SHEETS NOTES AND EXPANSIONS

  1. You can check the page insights and page feed documentation for detailed API information,  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 (one at a time) to get an idea of how it works:


    A) POST (aka PAGE FEED) METRICS
    * https://graph.facebook.com/v5.0/{page ID}/feed?access_token={Page Access Token} //use this to get a list of post names, IDs, and creation dates. The post ID will be provided in the data » id field.
    * https://graph.facebook.com/v5.0/{page ID}/posts?fields=insights.metric(post_impressions,post_reactions_like_total,post_clicks)&access_token={Page Access Token} //use this to get impressions, likes, and clicks for multiple Facebook posts in a single call. As above, the post ID will be provided in the data » id field. Each metric will be displayed in separate columns.
    fb-img14

    B) PAGE METRICS
    * https://graph.facebook.com/v5.0/{page ID}/insights?metric=page_fan_adds_unique,page_fan_adds&access_token={Page Access Token} // use this to get the count of new FB page likes from the API
    fb-img12
    * https://graph.facebook.com/v5.0/{page ID}?fields=name,fan_count&access_token={Page Access Token} //use this to get the count of total FB page likes from the API
    * https://graph.facebook.com/v5.0/{page ID}/insights?metric=page_total_actions,page_views_total,page_fan_adds_unique,page_impressions_unique,page_post_engagements&date_preset=yesterday&period=week&access_token={Page Access Token} //use this to retrieve many of the default metrics from your FB page's Insights Overview summary tab

    As always, remember to substitute in your actual Page ID and Page Access Token where it reads {page ID} and {Page Access Token}.

  2. In the final URL example 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).
  3. The full list of available Insights metrics is available here: https://developers.facebook.com/docs/graph-api/reference/v5.0/insights#availmetrics
  4. IMPORTANT SECURITY NOTE

    Anyone with Owner or Edit access to your Google Sheet can view all the information you've saved within API Connector, including API keys and other credentials. Treat these keys as passwords and limit access to your sheet accordingly.

Comments:9

  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?

    1. Hi Michael, you can do that by creating an API URL like this:
      https://graph.facebook.com/{page ID}/posts?fields=insights.metric(post_impressions,post_reactions_like_total)&access_token={page access 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.

  2. Bonjour,
    Merci cela semble bien mais ne marche pas pour moi!
    Pourrirez-vous mettre la fin du tuto à jour, car la création de l’url ne correspond plus à la réalité de ce qui est proposé.
    Merci de votre aide 🙂

    1. Hey Pascal can you please give me a little info about which part didn’t work for you? I just tried it again and didn’t have any problem.

  3. Hi, i was wondering if it is possible to set inside this one (https://graph.facebook.com/v5.0/{page ID}/posts?fields=insights.metric(post_impressions,post_reactions_like_total,post_clicks)&access_token={Page Access Token}) Also the parameter where the article ID was changed into the data message. So that the article ID will change to the first few lines of text

    1. Hi Alex, yes, you can import Facebook Ads data to Sheets with this method, using a very similar process. However, it ends up being not that convenient because, unlike for page tokens, FB doesn’t provide permanent user tokens, which you need for ad data. So if you used this method, you would need to update your user tokens every 2-3 months.
      Therefore, for FB Ad data I suggest using a tool that lets you access the API via the OAuth process instead of an API key. (You can find a few if you search the G Suite Marketplace for ‘Facebook ads” or similar). Hope that clarifies.

Leave a Reply

Your email address will not be published.