This script request data from Facebook Inisght API v5.0 asynchronous and imports in a Google Sheet. Based on a multi-account set up, merging multiple accounts report into a single sheet.
These instructions will get you a copy of the project up and running on your own google sheet and Facebook App. Each file should live within a single Google Script (File > New > Script File) but can also be combined.
- Set up Facebook App on first execution. See below
- Configure the script in Config. See below
- Run the request function
request_all_reports()
to request reports. - Run one of the extract functions to export reports, this should run after the request function to let the asynchronous request complete. On average, 30s is enough delay, although depending on the number of accounts and complexity of data.
export_to_once()
allow to export reports to one sheet, clearing the old content.- other function soon
For this script to work, you'll need to set up a Facebook App adding the Marketing API product. This current set up of the script is also set up to make Oauth 2.0 request to Facebook API using gsuitedevs/apps-script-oauth2 library.
- Create Facebook App.
- Add both Marketing API and Facebook Login as products
- In Facebook Log-in Product add the following URL (1) in Valid OAuth Redirect URIs. You may also get URL by running facebook_app_setup function in Oauth file.
(1) : https://script.google.com/macros/d/{{google_script_id}}/usercallback
All settings are contained within the Config file.
- Facebook App Oauth section relates to your Facebook App.
- Add gsuitedevs/apps-script-oauth2 library to your script.
- Facebook token is automatically set by Oauth, but could be set manually for short projects.
- Set Spreadsheet information at top based on numbers of account you have.
var header
should contain all fields you wish to include, in order, as displayed in your normal report. For example, cost should be "Amount Spent (USD)".FIELDS
should still include these metrics/dimension.- Refer to Facebook Docs for all other fields including :
LEVEL
,DATE_RANGE
,FIELDS
,TIME_INCREMENT
,FILTERING
TIMERANGE
is left as reference, although not in use in the current version ofrequestFacebookReport()
. If added, would overideDATE_RANGE
. It enables more flexibility in dates, as well as being able to include Today data in the request.