How to load data from the Facebook Ads to Snowflake

Extract data from Facebook Ads

You can pull your 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 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.

In this post we’ll focus only on how to extract Facebook Ads data, 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:

  • Apache HttpClient for Java
  • Spray-client for Scala
  • Hyper for Rust
  • Ruby rest-client
  • Python http-client

As with 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:

SH
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:

MARKDOWN
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 a data warehouse repository you own. 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.

Facebook Ads Data Preparation for Snowflake

The first step, before you start ingesting each of your data into a Snowflake data warehouse instance, is to have a well-defined schema of your own data.

Data in Snowflake is organized around tables with a well-defined set of columns with each one having a specific data type.

Snowflake supports a rich set of data types. It is worth mentioning that a number of semi-structured data types are also supported. With Snowflake, is possible to load directly data in JSON, Avro, ORC, Parquet, or XML format. Hierarchical data is treated as a first-class citizen, similar to what Google BigQuery offers.

There is also one notable common data type that is not supported by Snowflake. LOB or large object data type is not supported, instead, you should use a BINARY or VARCHAR type instead. But these types are not that useful for data warehouse use cases.

A typical strategy for loading data out of Facebook Ads to Snowflake is to create a schema where you will map each API endpoint to a table.

Each key inside the Facebook Ads API endpoint response should be mapped to a column of that table and you should ensure the right conversion to a Snowflake data type.

Of course, you will need to ensure that as any data types from the Facebook Ads API might change, you will adapt your database tables accordingly, there’s no such thing as automatic data typecasting.

After you have a complete and well-defined data model or schema for Snowflake, you can move forward and start loading data into the database.

Load data from Facebook Ads to Snowflake

Usually, data is loaded into Snowflake in a bulk way, using the COPY INTO command. Files containing any data, usually in JSON format, are stored in a local file system or in Amazon S3 buckets. Then a COPY INTO command is invoked on the Snowflake instance and data is copied into each data warehouse.

The files can be pushed into Snowflake using the PUT command, into a staging environment before the COPY command is invoked.

Another alternative is to upload every data directly into a service like Amazon S3 from where Snowflake can access data directly.

Finally, Snowflake offers a web interface as a data loading wizard where someone can visually set up and copy any data into a data warehouse. Just keep in mind, that the functionality of this wizard is limited compared to the rest of the methods.

Snowflake in contrast to other technologies like Redshift, does not require a data schema to be packed together with the data that will be copied. Instead, the schema is part of the query that will copy any data into the data warehouse. This simplifies the data loading process and offers more flexibility on data type management.

Updating your Facebook Ads data on Snowflake

As you will be generating more data on Facebook Ads, you will need to update your older data on Snowflake. This includes new records together with updates to older records that for any reason have been updated onFacebook Ads.

You will need to periodically check Facebook Ads for new data and repeat the process that has been described previously while updating your currently available data if needed. Updating an already existing row on a Snowflake table is achieved by creating UPDATE statements.

Snowflake has a great tutorial on the different ways of handling updates, especially using primary keys.

Another issue that you need to take care of is the identification and removal of any duplicate records on your database. Either because Facebook Ads does not have a mechanism to identify new and updated records or because of errors on each of the data pipelines you have, duplicate records might be introduced to your database.

In general, ensuring the quality of data that is inserted in your database is a big and difficult issue.

The best way to load data from Facebook Ads to Snowflake

So far we just scraped the surface of what you can do with Snowflake 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 RudderStack that can handle everything automatically for you.

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

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