fbRads: Easy access to Facebook’s Marketing API from R

If you're a CARD.com customer, there's a good chance you discovered us through one of our ads on Facebook. In this post, we dig deeper into how software developers create, scale and automate their ads using Facebook's Marketing API. If you've never thought about how ads show up on Facebook, check out this introductory video, and if the word "API" means nothing to you, here are few ways to explain one.

Facebook announced the general availability of its Marketing API at the F8 2015 conference in March, making it possible for developers around the world to build their own advertising solutions. Inspired by this move, we decided to open-source the related codebase, developed internally in R, to encourage data-driven marketers to harness Facebook's rich APIs. The R package has been released with the name fbRads.

This package acts as a convenient wrapper around the Facebook Marketing API endpoints, so that you can quickly and efficiently develop R scripts and applications to create, monitor and potentially optimize Facebook ads. The source code is hosted on GitHub with AGPL license.

This post provides a short introduction on how to install and load the package with a quick demo on some of the covered features. To be able to access the API, you have to create a developer account and register a free application, which looks something like:

Note you app ID and secret, then we can generate an access token with the httr package:

Now save this token in a secure place, as this will be required every time you want to interact with the Facebook API. Make sure you do not push this secret token to a git repository!

Let’s first install the package from GitHub with the devtools package and initialize a connection to the Facebook API with the token saved above in the tkn variable:

The fbRads package comes with many functions to interact with the API, all name starting with the fb prefix:

The above list contains helpers to create, read and update ads, ad sets and campaigns, and some other functions to interact with custom and lookalike audiences, also to query or estimate performance metrics. Now we will focus on this latter, and let’s see how many Facebook users can be reached if we are targeting an ad at the R users:

So there are around 1.6 million Facebook users all around the world interested in the R programming language. This is pretty impressive, although somewhat large number! What about some other programming languages?

So we searched for programming language-related keywords, and stored the returned list as a data.frame and printed the top 10 results above. It seems that there are many programmers on Facebook.

Besides such basic search queries, we can also built some more complex queries to target for example the all the R, Python and Java programmers living in the USA:

Please note that the above logical operator for the 3 programmer groups is OR, so this 1.7 million Facebook profiles includes all the R, Python and Java programmers living in the USA. To query the same 3 groups with an AND logical operator, we have to pass the interest groups separately as described in the related Facebook docs:

And using the above approach, we could query all combinations of the overlaps between two programming languages to visualize these 1.7 million Facebook profiles on a Venn diagram via the limma package:

With the few steps above, we have harnessed Facebook's data to glean some insights about usage stats for programming languages, but this is just the beginning. With some cool ideas, a few good algorithms and fbRads, you might just be able to create your own new ads platform!

Author: Gergely Daroczi, Lead R Developer