Load data from Facebook Ads to SQL Data Warehouse

Blendo Team

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

 

We will see how to access and extract data from Facebook Ads through its API and how to load it into SQL Data Warehouse. 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.

About Microsoft Azure SQL Data Warehouse

SQL Data Warehouse is the data warehousing solution that you can use if you are a user of Microsoft Azure. It’s an elastic data warehousing as a service solution, emphasizing it’s enterprise focus. It also speaks SQL like the previous two solutions and it supports querying both relational and non-relational data.  It offers a number of enterprise-class features like support for hybrid cloud installations and strong security. It’s probably the less mature solution compared to the two others though, it’s still in “Preview” mode although accessible to existing Azure subscribers.

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.

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 advertisement needs, from creating ads programmatically to see how your campaigns perform. At 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 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:

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.

Load Data from Facebook Ads to SQL Data Warehouse

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

  • 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 favourite language. Before you actually upload any data you have to create a container which is something similar as a concept to the Amazon AWS Bucket, creating a container is a straight forward 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.

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:

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 a 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 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 SQL Data Warehouse and start generating insights from your data.Want to save hours on your data management tasks? Blendo can help!