How to load data from Facebook Ads to Redshift

Extract data from Facebook Ads

You can pull your data Ads from Facebook through the Ads Insights API. The Insights API provides access to analytics and reporting functionality. The way you interact with your data is by requesting reports where you define exactly the data and its granularity that you need. Facebook exposes a 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.

In this post, we’ll focus only on how to extract data your Ads data from Facebook. For further information on what else can be performed through the Facebook Ads-related APIs, you can check the Marketing API documentation. 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. The Facebook Marketing API is a RESTful web API and can also be accessed by performing requests directly to the appropriate endpoints. As a RESTful API, interacting with it can be achieved using CURL tool or 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 also use the Graph Explorer, and there’s a special Edge that you can use to request an ad’s statistics 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:

BATCHFILE
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 out of 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 analytics and reporting purposes. This richness comes with a price though many complex resources have to be handled through an also complex protocol.

Prepare your Facebook Ads data for Amazon Redshift

Amazon Redshift is built around industry-standard SQL with added functionality to manage very large data sets and high-performance analysis. So, in order to load your data into it, you will have to follow its data model which is a typical relational database model. The data you extract from your data source should be mapped into tables and columns. Where you can consider the table as a map to the resource you want to store and columns the attributes of that resource. Also, each attribute should adhere to the datatypes that are supported by Redshift, currently, the datatypes that are supported are the following:

  • SMALLINT
  • INTEGER
  • BIGINT
  • DECIMAL
  • REAL
  • DOUBLE PRECISION
  • BOOLEAN
  • CHAR
  • VARCHAR
  • DATE
  • TIMESTAMP

As your data are probably coming in a representation like JSON that supports a much smaller range of data types you have to be really careful about what data you feed into Redshift and make sure that you have mapped your types into one of the datatypes that are supported by Redshift. Designing a Schema for Redshift and mapping the data from your data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions that you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database you need to load your data on one of the data sources that are supported as input by Redshift, these are the following:

Load data from Facebook Ads to Redshift

The first step in loading your Facebook Ads data to Redshift is to put them in a source that Redshift can pull it from. As it was mentioned earlier there are three main data sources supported. To upload your data to Amazon S3 you will have to use the AWS REST API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. The first task that you have to perform is to create a bucket, you do that by executing an HTTP PUT on the Amazon AWS REST API endpoints for S3. You can do this by using a tool like CURL. Or use the libraries provided by Amazon for your favorite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation. After you have created your bucket you can start sending your data to Amazon S3, using again the same AWS REST API but by using the endpoints for Object operations. As in the Bucket case you can either access the HTTP endpoints directly or use the library of your preference. DynamoDB imports data again from S3, it adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons you can avoid it. Amazon Kinesis Firehose is the latest addition as a way to insert data into Redshift and offers a real-time streaming approach to data importing. Amazon Redshift supports two methods for loading data into it. The first one is by invoking an INSERT command. You can connect to your Amazon Redshift instance with your client, using either a JDBC or ODBC connection and then you perform an INSERT command for your data.

JAVASCRIPT
insert into category_stage values
(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');

The way you invoke the INSERT command is the same as you would do with any other SQL database, for more information you can check the INSERT examples page on the Amazon Redshift documentation. Redshift is not designed for INSERT-like operations. On the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from an Amazon DynamoDB table. When you perform COPY commands, Redshift can read multiple files simultaneously and automatically distributes the workload to the cluster nodes, and performs the load in parallel. As a command COPY is quite flexible and allows for many different ways of using it, depending on your use case. Performing a COPY on Amazon S3 is as simple as the following command:

SH
copy listing
from 's3://mybucket/data/listing/'
credentials 'aws\_access\_key\_id=;aws\_secret\_access\_key=';

For more examples of invoking a COPY command, you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance using a JDBC or ODBC connection and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.

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

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

Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use RudderStack to handle this kind of problem automatically.

RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics, and more.

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.
Don't want to go through the pain of direct integration? RudderStack's Facebook Ads integration makes it easy to send data from Facebook Ads to Amazon Redshift.