How to load data from Facebook Ads to Google BigQuery

Blendo Team

Marketing is becoming heavily data-driven. How can you get all marketing data like Facebook Ads or Bing Ads or Google AdWords in one place like  Google BigQuery? This post is a guide to help you define a pipeline, to load data from Facebook Ads to Google BigQuery for further analysis. Alternatively, in order to load your data from Facebook Ads to Google BigQuery you can use an ETL as a service product like Blendo that can handle this kind of problems automatically for you.

We will see how to access and extract data out of Facebook Ads through its API and how to load it into BigQuery. This process requires from you to write the code to get the data and make sure that this process will run every time new data are generated.

What we will see:

About Facebook Ads

Load data from Facebook Ads to BigQuery

Load data from Facebook Ads to BigQuery

With Facebook Ads, you can create targeted adverts to reach different audiences and meet your business goals. These adverts can appear in different locations on Facebook, like the Newsfeed or the right column of Facebook on desktop. Just like Google AdWords, Facebook Ads is a Real Time Bidding system where advertisers compete to display their advertising material. Programmatic and instantaneous auctions are performed, similar to how financial markets operate. Among the benefits of Facebook Ads, are:

  • Reach – potentially you can reach more than 1 billion of active Facebook Users.
  • Any budget – You can start with any budget, although you have to be aware of the Real Time Bidding nature of Facebook Ads, which means that the effectiveness of your campaigns are linked to what your competitors are also willing to pay.
  • Pay-per-click – advertisers pay only for ads that have been clicked by the user.

About Google BigQuery

Load data from Facebook Ads to BigQuery

Load data from Facebook Ads to BigQuery

BigQuery is the data warehousing solution of Google. It’s part of the Google Cloud Platform and it also speaks SQL like Redshift does. Queries are executed against append-only tables using the processing power of Google’s infrastructure. It is also fully managed and is offered as a service over the cloud. You can interact with it through its web UI, using a command line tool while a variety of client libraries exist so you can interact with it through your application.

Extract data from Facebook Ads

You can pull your data out of Facebook Ads through the Ads Insights API. The Insights API provides access to analytics and reporting functionality and the way you interact with your data is by requesting reports where you define exactly the data and its granularity that you need. As in the case of Google, Facebook also exposes a very rich set of APIs that you can use for every aspect of your advertisement needs, from creating ads programmatically to see how your campaigns perform. At this post we’ll focus only on how to extract data out of Facebook Ads, for further information on what else can be performed through the Facebook Ads related APIs, you can check the documentation of the Marketing API.

Before you start doing anything, have a read on how to activate and manage your developer account. And make sure that you understand the security related concepts of the Facebook Marketing API. In general, access to the API happens mainly through the SDKs that Facebook offers. Officially, SDKs for PHP and Python are supported, while there are also a number of community supported SDKs for languages like R, JavaScript and Ruby. You can also find more if you do some research on places like GitHub. The Facebook Marketing API is a RESTful web API and thus can also be access by performing requests directly to the appropriate endpoints. As a RESTful API, interacting with it an be achieved by using tools like CURL or Postman or by using http clients for your favorite language or framework. A few suggestions:

As everything in Facebook, Ads and their statistics are part of the Graph API, which you can interact with also using the Graph Explorer, and there’s a special Edge that you can use to request ad’s statistics, it’s the insights edge. Insights can be access from the following list of edges:

Load data from Facebook Ads to BigQuery

The response from each, contains information belonging to the ad object for which insights are queried.

For example, let’s assume that you would like to extract all stats related to your account. You could do this by executing the following request using CURL:

curl -F 'level=campaign' -F 'fields=[]' -F 'access_token=<ACCESS_TOKEN>' https://graph.facebook.com/v2.5/<CAMPAIGN_ID>/insights curl -G -d 'access_token=<ACCESS_TOKEN>' https://graph.facebook.com/v2.5/1000002 curl -G -d 'access_token=<ACCESS_TOKEN>' https://graph.facebook.com/v2.5/1000002/insights

Data can be returned in either xls or csv format and when the report is ready based on your request you can accessing from a URL like the following:

https://www.facebook.com/ads/ads_insights/export_report?report_run_id=<REPORT_ID>&format=<REPORT_FORMAT>&access_token=<ACCESS_TOKEN

Get real time streams of your Facebook Ads stats

It’s also possible to create a real time data infrastructure for fetching data from Facebook Ads and loading them into your data warehouse repository. You can do that by subscribing to real time updates to receive API updates with webhooks. With the proper infrastructure you can have an almost real time feed of data into your repository and ensure that it will always be up to date with the latest data.

Facebook Ads exposes a very rich API which offers you the opportunity to get very granular data about your accounting activities and use it for analytic and reporting purposes. This richness comes with a price though, a large number of complex resources that have to be handled through an also complex protocol.

Prepare your Facebook Ads Data for Google BigQuery

Before you load your data into BigQuery, you should make sure that it is presented in a format supported by it, so for example if the API you pull data returns XML you have to first transform it into a serialization that BigQuery understands. Currently two data formats are supported:

You also need to make sure that the data types you are using are the ones supported by BigQuery, which are the following:

  • STRING
  • INTEGER
  • FLOAT
  • BOOLEAN
  • RECORD
  • TIMESTAMP

for more information please check the Preparing Data for BigQuery page on the documentation.

 

Load Data from Facebook Ads to Google BigQuery

If you want to load Facebook Ads data to Google BigQuery, you have to use one of the following supported data sources.

  1. Google Cloud Storage
  2. Sent data directly to BigQuery with a POST request
  3. Google Cloud Datastore Backup
  4. Streaming insert
  5. App Engine log files
  6. Cloud Storage logs

From the above list of sources, 5 and 6 are not applicable in our case.

For Google Cloud Storage, you first have to load your data into it, there are a few options on how to do this, for example you can use the console directly as it is described here and do not forget to follow the best practices. Another option is to post your data through the JSON API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. In it’s simplest case it’s just a matter of one HTTP POST request using a tool like CURL or Postman. It should look like the following example.

POST /upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject HTTP/1.1 Host: www.googleapis.com Content-Type: application/text Content-Length: number_of_bytes_in_file Authorization: Bearer your_auth_token your Facebook Ads data

and if everything went ok, you should get something like the following as a response from the server:

HTTP/1.1 200 Content-Type: application/json { "name": "myObject" }

Load data from facebook ads to google bigquery

 

Working with Curl or Postman, is good only for testing, if you would like to automate the process of loading your data into Google Bigquery, you should write some code to send your data to Google Cloud Storage. In case you are developing on the Google App Engine you can use the library that is available for the languages that are supported by it:

  1. Python
  2. Java
  3. PHP
  4. Go

If you are using one of the above languages and you are not coding for the Google App Engine, you can use it to access the Cloud Storage from your environment. Interacting such a feature rich product like Google Cloud Storage can become quite complicated depending on your use case, for more details on the different options that exist you can check Google Cloud Storage documentation. If you are looking for a less engaged and more neutral way of using Cloud Storage, you can consider a solution like Blendo.

After you have loaded your data into Google Cloud Storage, you have to create a Load Job for BigQuery to actually load the data into it, this Job should point to the source data in Cloud Storage that have to be imported, this happens by providing source URIs that point to the appropriate objects.

The previous method described, used a POST request to the Google Cloud Storage API for storing the data there and then load it into BigQuery. Another way to go is to do a direct HTTP POST request to BigQuery with the data you would like to query. This approach is similar to how we loaded the data to Google Cloud Storage through the JSON API, but it uses the appropriate end-points of BigQuery to load the data there directly. The way to interact with it is quite similar, for more information can be found on the Google BigQuery API Reference and on the page that describes how to load data into BigQuery using POST. You can interact with it using the HTTP client library of the language or framework of your choice, a few options are:

The best way to load data from Facebook Ads to Google BigQuery and possible alternatives

So far we just scraped the surface of what can be done with Google BigQuery and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from and the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources.

A possible alternative, instead of writing, hosting and maintaining a flexible data infrastructure, is to use an ETL as a service product like Blendo that can handle this kind of problems automatically for you.

Blendo integrates with multiple sources or services like databases, CRM, email campaigns, analytics and more. Quickly and safely move all your data from Facebook Ads into your data warehouse and start generating insights from your data.