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.
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:
curl-F 'level=campaign'-F 'fields=\[\]'-F 'access\_token=<ACCESS\_TOKEN>'https://graph.facebook.com/v2.5/<CAMPAIGN_ID>/insightscurl -G-d 'access\_token=<ACCESS\_TOKEN>'https://graph.facebook.com/v2.5/1000002curl -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:
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.
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 the 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 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 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:
- DOUBLE PRECISION
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:
About Amazon Redshift
Amazon Redshift is one of the most popular data warehousing solutions which is part of the Amazon Web Services (AWS) ecosystem. It is a petabyte-scale, fully managed data warehouse as a service solution that runs on the cloud. It is SQL based and you can communicate with it as you would do with PostgreSQL, actually you can use the same driver although it would be better to use the drivers recommended by Amazon. You can connect either through JDBC or ODBC connections.
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.
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:
copy listingfrom '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.
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.