Import Facebook Page Data to Google Sheets
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. It hardly needs to be said, Facebook Pages serve as a business presence for many companies, so we definitely want a way to grab some stats about what people are clicking and reading on these pages.
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 the most straightforward. The best part is, you only need to do this process ONCE, as the token you get will last forever!
At the end of this article, I’ve also provided a reporting template for a quick start. Just plug in your Facebook Page Token and Page ID to see metrics about your page.
- Before You Begin
- Part 1: Get your Facebook API User Access Token
- Part 2: Get your Permanent Page Token
- Part 3: Create your API Request URL
- Part 4: Pull FB API Data into Sheets
- Part 5: More Example API URLs
- Part 6: Handle Pagination
- Appendix: Facebook Page Reporting Template
BEFORE YOU BEGIN
Click here to install the API Connector add-on from the Google Marketplace.
PART 1: GET YOUR FACEBOOK API USER ACCESS TOKEN
- Begin by navigating to https://developers.facebook.com/, and click My Apps.
- Click + New App
- Facebook will ask you how you’re using your app. Choose Manage Business Integrations and click Continue.
- 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, choose your app purpose, and click Create App.
- 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
- Under ‘Facebook App’, select the app you just created. Click Get User Access Token from the drop-down menu.
- A modal will appear, prompting you to login. Click Continue.
- 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’).
- Now click Generate Access token.
- 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.
- 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:
- At the bottom of this page, click Extend Access Token.
- This will produce a long-lived User token. Click Debug to pull up a screen where you can copy it to your clipboard.
- Return to the Facebook Graph API Explorer. Paste the long-lived User token you just got into into the “Access Token” field.
- 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.
- 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.
- 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).
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:
Just substitute in your own Page ID and Page Access Token.
We can now enter all our values into API Connector and import Facebook Page Post data into Google Sheets.
- Open up Google Sheets and click Add-ons > API Connector > Open.
- In the Create tab, enter the Request URL we just created. For convenience, I’ve included my token by referencing it in a cell, but you don’t have to. 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.
- Pay attention to the
data » idfield as you can use that to associate views, likes, and other metrics to the post titles.
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.
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.
➜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
You can use the following API request URL to pull in most of these metrics to your sheet:
Get the current count of total FB page likes from the API
Get count of total FB page likes by day for a time range (the “since” and “until” parameters contain the start and end dates). You can only query 90 days at once when using the since/until parameters.
Note: this request was run in “compact” mode. The default “single row” report style would display each day’s data in its own column.
If you want to make a dynamically updating date range, add a dynamic date formula like
=today()-1to one cell, add a second cell that converts it to UNIX with the
=(A1-DATE(1970,1,1))*86400formula, and then reference that UNIX timestamp in your API request URL. Every time the date changes, your URL will update automatically.
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).
- 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:
The cursor value for the
afterparameter 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.
- In API Connector you can run these paged requests manually, or loop through them automatically using pagination handling (paid feature).
- API URL: enter your request URL as usual, making sure to include limit=5000
- Pagination type:
- Next token parameter:
- Next token field:
- Number of pages: enter the number of pages you’d like to fetch
APPENDIX: FACEBOOK PAGE REPORTING TEMPLATE
This report lets you plug in your Facebook Page ID and Page Token to create a report that contains key metrics about both your Facebook page performance and engagement with your posts. Click here to get your own copy (just click File > Make a Copy).