Fetch your Facebook Ads data using windsor.ai API and R

[This article was first published on R Archives - Data and Marketing Attribution Modelling | Windsor.ai, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Summary

In this guide you will learn how to get your data from Facebook Ads into R. To do so, we will use windsor.ai API. Windsor.ai allows you to integrate marketing data from multiple sources (such as FacebookGoogle or Instagram Ads, among others) and then connect it to several destinations (such as Power BITableauGoogle Sheets, etc.) to further analyze it. You can also use the platform itself to produce very informative and interactive reports that will help you take the correct decisions. Moreover, windsor.ai does not have any limitation when it comes to volume of data queried by means of API call requests. All you need before starting to get your data is to create and account in windsor.ai and get an API key You have a 30 days free trial. Then, you will need to install the facebookadsR package.

LET’S PROCEED!

Get windsor.ai API key

First of all, let’s get your windsor.ai API key, in case you do not have one. This process is extremely straightforward and will only take you a few minutes. All you need to do is to sign-in here. Then, by following these four steps you will have access to your API key:

  1. Click on Facebook Ads option
  2. Sign-in with your Facebook account
  3. Click on “NEXT (DATA PREVIEW)”
  4. Copy the API key as it appears in the upper box

Now you are ready to open your R project and start extracting and loading your Facebook Ads data from windsor.ai.

ATENTION! This API key allows access to your data, so make sure you do not make it public. I recommend you to paste it in a safe space with restricted access.

Get data from Facebook Ads into R

The goal now is to outline in a couple of paragraphs and few lines of code some simple ways in which we can use the windsor.ai API and the R package facebookadsR to get your Facebook Ads data.

First of all we need to install the package

install.packages("facebookadsR", dependencies = T)

then, load the package

library(facebookadsR)

Once facebookadsR is installed and loaded, you can use the function fetch_facebookads to get your Facebook Ads data. You will need to paste your API. Then, there are several arguments of the function that control the data that is imported to your R project environment. Let’s have a look at the arguments of the function:

  • api_key: your API key as a character. Do not forget to paste your API key within quotation marks, otherwise R will detect it as an object that does not exist.
  • date_from and date_to: the period of time we are interested in. Here we will download data from the last 100 days, which is the default when no dates are specified in the function.
  • fields: columns of the data set (variables) to be imported.

Once you include your API key, the following code will download Facebook Ads marketing data regarding clicks and time spend for each campaign for the last 100 days.

my_facebookads_data <- fetch_facebookads(
api_key = "your api key", 
date_from = Sys.Date()-100, 
date_to = Sys.Date(), 
fields = c("campaign", "clicks", "spend", "impressions", "date")
)

This is how it looks:

head(my_facebookads_data)
#>            campaign clicks spend impressions       date
#> 1   retageting APAC      4  2.57         806 2022-09-28
#> 2 retargeting UK&CO      0  2.48         693 2022-09-28
#> 3   retageting APAC      5  2.39         819 2022-09-29
#> 4 retargeting UK&CO      7  2.54         689 2022-09-29
#> 5   retageting APAC      0  0.94         299 2022-09-30
#> 6 retargeting UK&CO      0  0.71         190 2022-09-30
Lets take a look at the data we just downloaded to get a better idea about the structure and type of information included.
str(my_facebookads_data) 
#> 'data.frame':    14 obs. of  5 variables:
#>  $ campaign   : chr  "retageting APAC" "retargeting UK&CO" "retageting APAC" "retargeting UK&CO" ...
#>  $ clicks     : num  4 0 5 7 0 0 4 2 3 0 ...
#>  $ spend      : num  2.57 2.48 2.39 2.54 0.94 0.71 2.59 2.12 2.43 0.13 ...
#>  $ impressions: num  806 693 819 689 299 190 682 688 822 135 ...
#>  $ date       : chr  "2022-09-28" "2022-09-28" "2022-09-29" "2022-09-29" ...

CONGRATULATIONS! Now you are ready to analyze your data and guide your marketing decision making.

The post Fetch your Facebook Ads data using windsor.ai API and R appeared first on Data and Marketing Attribution Modelling | Windsor.ai.

To leave a comment for the author, please follow the link and comment on their blog: R Archives - Data and Marketing Attribution Modelling | Windsor.ai.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)