This article covers how to get advertising statistics with Facebook Marketing API by fbRads package using the language R.
To get started, you have to:
- Have a Facebook account set as an admin on to a given Facebook Ad Account and Facebook Business Manager
- Verified as a Facebook Individual developer
Create a New App
Go to the Facebook for Developers page: https://developers.facebook.com/apps/
On the page, log in, and click Add New App.
Type the name of your app and your contact email. The email is going to be used to send notifications, alerts, and other important information. This is why it has to be your primary email address.
The app would be created when you click the Create Add ID button.
When the app is created, Selecting platform – would be the last step before the app is ready to use.
Click Settings > Basic > Add Platform (at the bottom of the Setting page) > Select Website on the pop-up window.
Type “http://localhost:1410/ ” in the Site URL field and save changes.
Go to the Settings > Advanced tab, add http://localhost:1410/ as the “Share Redirect Domain Whitelist”.
Select the Business Account in the Business Manager section. You can get data only from the account selected.
When you’ve completed the required form fields, click the Save Changes button at the bottom
This is it. The new app is ready to use.
The App was created to make Facebook Marketing API calls which need the next parameters:
- App ID and App Secret – These are located on the App’s Basic Settings section. You have to use those in the below R script to get a token for future authentication.
- Account ID – The Advertising account ID which has the set of data you want to play with. You can find the list of the account ID’s owned by your business here:
https://business.facebook.com/home/accounts?business_id=‘YOUR BUSINESS ID’
To see your advertising account in a particular Business, go to Business settings > Accounts > Ad accounts.
Note: to be sure that your new app is connected to the Business and advertising accounts properly, click Business settings > Accounts > Apps. Select the app you created and check the Connected Assets section. There have to be advertising accounts you want to analyze, if there are not, make the changes.
And you’re done!
Users can get their personal Facebook token in the method outlined below.
Authentication via web browser requires the httr package to be installed.
## install httr package
Copy and paste the secret key (along with the name of your app) into an R script file and pass them along to oauth_app().
#Load required libraries
#The code running only once, so you don't have to store parameters into variables.
app <- oauth_app('facebook', 'App ID', 'App Secret')
Sys.setenv('HTTR_SERVER_PORT' = '1410/')
tkn <- oauth2.0_token(oauth_endpoints('facebook'), app, scope = 'business_management',
type = 'application/x-www-form-urlencoded', cache = FALSE)
tkn <- jsonlite::fromJSON(names(tkn$credentials))$access_token
#This will create a rds file under your working directory.
A browser pop-up window should appear. Click to approve (must be signed in to facebook.com). Authentication complete on the Facebook side. Please close this page and return to R.
Once the app linked to your Facebook account you have to see “Authentication complete.” notification in RStudio. You have a needed token for further API calls.
NOTE: you can unlink any app from your Facebook account here: https://www.facebook.com/settings?tab=business_tools
You got access to Facebook Marketing API with around 3 lines of code and it’s time to check if everything works properly.
I propose you get a list of all your ad campaigns and groups along with the ID and status or a list of all your active ads with the function fbad_list_ad() and code below:
fbad_list_ad(fields = c('name', 'effective_status'))
fbad_list_ad(statuses = 'ACTIVE', fields = 'name')
Using the API
The instruction above explains how to make the first step and start using Facebook Marketing API. There is a lot of information about the API itself here: developers.facebook.com/docs/marketing-apis. I am working hard to share more useful information and use cases with the API to make your life more convenient.
[…] The code load required libraries and initializes a connection with the Facebook Marketing API. You can find more details on how to make this connection happen in the tutorial. […]