Feeling stuck with Segment? Say 👋 to RudderStack.

SVG
Log in

Load data from Facebook Ads to SQL Data Warehouse

[@portabletext/react] Unknown block type "aboutNodeBlock", specify a component for it in the `components.types` prop
[@portabletext/react] Unknown block type "aboutNodeBlock", specify a component for it in the `components.types` prop

Extract data from Facebook Ads

You can pull your data from 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 advertising needs, from creating ads programmatically to see how your campaigns perform. In this post we’ll focus only on how to extract data from 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 accessed by performing requests directly to the appropriate endpoints. As a RESTful API, interacting with it can be achieved by using tools like CURL or Postman or by using HTTP clients for your favorite language or framework. A few suggestions:

As with everything in Facebook, Ads and their statistics are part of the Graph API, which you can interact with also using 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:

JAVASCRIPT
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 access from a URL like the following:

HTML
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.

Load Data from Facebook Ads to SQL Data Warehouse

SQL Data Warehouse support numerous options for loading data, such as:

  • PolyBase
  • Azure Data Factory
  • BCP command-line utility
  • SQL Server integration services

As we are interested in loading data from online services by using their exposed HTTP APIs, we are not going to consider the usage of BCP command-line utility or SQL server integration in this guide. We’ll consider the case of loading our data as Azure storage Blobs and then use PolyBase to load the data into SQL Data Warehouse.

Accessing these services happens through HTTP APIs, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. You can access these APIs by using a tool like CURL, Postman. Or use the libraries provided by Microsoft for your favorite language. Before you upload any data you have to create a container which is something similar to the Amazon AWS Bucket, creating a container is a straightforward operation and you can do it by following the instructions found on the Blog storage documentation from Microsoft. As an example, the following code can create a container in Node.js.

JAVASCRIPT
blobSvc.createContainerIfNotExists('mycontainer', function(error, result, response){ if(!error){ // Container exists and allows // anonymous read access to blob // content and metadata within this container } });

After the creation of the container you can start uploading data to it by using again the given SDK of your choice in a similar fashion:

JAVASCRIPT
blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob', 'test.txt', function(error, result, response){ if(!error){ // file uploaded } });

When you are done putting your data into Azure Blobs you are ready to load it into SQL Data Warehouse using PolyBase. To do that you should follow the directions in the Load with PolyBase documentation. In summary the required steps to do it, are the following:

  • create a database master key
  • create a database scoped credentials
  • create an external file format
  • create an external data source

PolyBase’s ability to transparently parallelize loads from Azure Blob Storage will make it the fastest tool for loading data. After configuring PolyBase, you can load data directly into your SQL Data Warehouse by simply creating an external table that points to your data in storage and then mapping that data to a new table within SQL Data Warehouse.

Of course, you will need to establish a recurrent process that will extract any newly created data from your service, load them in the form of Azure Blobs and initiate the PolyBase process for importing the data again into SQL Data Warehouse. One way of doing this is by using the Azure Data Factory service. In case you would like to follow this path you can read some good documentation on how to move data to and from Azure SQL Warehouse using Azure Data Factory.

The best way to load data from Facebook Ads to SQL Data Warehouse and possible alternatives

So far we just scraped the surface of what can be done with Microsoft Azure SQL Data Warehouse 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 a product like RudderStack that can handle this kind of problem automatically for you.

RudderStack 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 SQL Data Warehouse and start generating insights from your data.

Sign Up For Free And Start Sending Data

Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.