Analyzing and Managing Facebook Ads from R

Wrapper functions around the Facebook Marketing 'API' to create, read, update and delete custom audiences, images, campaigns, ad sets, ads and related content.

This R package includes wrapper functions around the Facebook Marketing API to create, read, update and delete custom audiences, images, campaigns, adsets, ads and related content.

Vignette and more detailed documentation is coming soon, until then please see the slides presented at useR! 2015, EARL 2015, the Los Angeles R Users Group and at a Domino Webinar.

Creating a Facebook App & connect with OAuth token

To be able to use this package, you will have to create a Facebook App and authorize it to mange your Facebook ads. Basic steps to create an app with Development access level letting you manage up to 5 Facebook ad accounts:

  1. Create new application at with "basic setup".

  2. Fill in a unique Display Name (eg "app_testing_foobar_42"), set the category to eg "Business". Click on "Create App ID" & pass the captcha test.

  3. In "Settings/Basic", click "Add Platform" add create "Website" platform with the URL of http://localhost:1410/ and "localhost" as the "App Domain". Click "Save Changes".

  4. In the "Settings/Advanced" tab, add http://localhost:1410/ as the "Valid OAuth redirect URIs". Click "Save Changes".

  5. Note your "App ID" and "App Secret" on your dashboard, and use those in the below R script to get a token for future authentication:

    app <- oauth_app('facebook', 'your app id', 'your app secret')
    Sys.setenv('HTTR_SERVER_PORT' = '1410/')
    tkn <- oauth2.0_token(
        oauth_endpoints('facebook'), app, scope = 'ads_management',
        type  = 'application/x-www-form-urlencoded', cache = FALSE)
    tkn <- tkn$credentials$access_token
  6. Please note the above last step: we store the token as a string. Now you can save that token in a safe place and start using fbRads, eg:

    fbad_init(accountid = accountid, token = tkn, version = '2.5')
  7. And list all your ads along with the ad name and status or eg filter for the active ads:

    fbad_list_ad(fields = c('name', 'effective_status'))
    fbad_list_ad(statuses = 'ACTIVE', fields = 'name')

Using the package

This package makes your life more convenient when it comes to interacting with the Facebook Marketing API, but unfortunately, it cannot save you the time to get familiar with the actual API. To be able to interact with Facebook, you have to learn about how the API works etc -- see the documentation at

This package was developed and being maintained at, licensed under AGPL-3.


fbRads 0.2 (2016-04-04)

Documentation cleanup and first CRAN release with support for Facebook Marketing API v2.4 and v2.5.

fbRads 0.1 (2015-02-15)

Internal R scripts were published publicicly as an open-source R package after Facebook made it's Marketing API generally available at the F8 2015 conference in March 2015.

Supported API versions in the development verison of the package hosted on GitHub at

  • initial support with v2.2 to search keywords (2015-02-15)
  • add people to custom audiences (2015-04-29)
  • create and share custom and lookalike audiences (2015-05-08)
  • get reach estimates (2015-06-05)
  • update to v2.3 without any major impact on available functionality (2015-06-18)
  • create and read images, creatives, ads, adsets and campaigns (2015-06-18)
  • first steps with the Insights API access (2015-07-08)
  • full support for the adreportstats API endpoint (2015-07-09)
  • drop requiring the "fbacc" argument in fbRads functions (2015-07-21)
  • update to v2.4 (2015-07-22)
  • batch query to read ad attributes (2015-07-28)
  • update ads, adsets and campaigns (2015-07-29)
  • support for the adstatistics API endpoint (2015-08-11)
  • batched listing of ads, adsets and campaigns (2015-08-12)
  • improved support for the Insights API (2015-09-28, 2015-10-06, 2015-10-20)
  • remove members from custom audience (2016-01-19)
  • support for v2.5 (2016-01-19, 2016-03-01, 2016-03-02)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.2 by Gergely Daroczi, 5 years ago

Browse source code at

Authors: Ajaykumar Gopal <[email protected]> , Gergely Daroczi <[email protected]>

Documentation:   PDF Manual  

Task views: Web Technologies and Services

AGPL-3 license

Imports RCurl, jsonlite, digest, futile.logger, bit64, plyr, data.table

See at CRAN