How to load data from Facebook Ads to Google BigQuery

Blendo Team

This post helps you with loading your data from Facebook Ads to BigQuery. If you are looking to get analytics-ready data without the manual hassle you can integrate Facebook Ads to BigQuery with Blendo, so you can focus on what matters, getting value out of your business data.

What we will see:

Extract data from Facebook Ads

You can pull any 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 data you own 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:

You will need more time to read this post than integrating Facebook Ads to BigQuery.

Effortlessly Sync All Your Facebook Ads Data to BigQuery

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:

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>'<CAMPAIGN_ID>/insights curl -G -d 'access_token=<ACCESS_TOKEN>' curl -G -d 'access_token=<ACCESS_TOKEN>'

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:<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 from Facebook Ads and loading them into 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.

About Facebook Ads

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.

Prepare your Facebook Ads Data for Google BigQuery

Before you load 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:


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

About Google 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.

Load Data from Facebook Ads to Google BigQuery

If you want to load Facebook Ads data to 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 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: 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" }


Working with Curl or Postman, is good only for testing, if you would like to automate the process of loading your own data into Bigquery, you should write some code to send 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 Storage in the Cloud, you can consider a solution like Blendo.

After you have loaded any 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 Storage in the Cloud 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 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 BigQuery

So far we just scraped the surface of what you can do with BigQuery and how to load data into it. Things can get even more complicated if you want to integrate data coming from different sources.

Are you striving to achieve results right now?

Instead of writing, hosting and maintaining a flexible data infrastructure use Blendo that can handle everything automatically for you.

Blendo with one click integrates with sources or services, creates analytics-ready data and syncs your Facebook Ads to BigQuery right away.

Help your marketing and executive team take ownership of the advertisement data that live inside Facebook Ads to transform performance marketing and ROI.

Blendo is the easiest way to automate powerful data integrations.

Try Blendo free for 14 days. No credit card required.