Facebook Ads to Snowflake

This page provides you with instructions on how to extract data from Facebook Ads and load it into Snowflake. (If this manual process sounds onerous, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

What is Facebook Ads?

Facebook Ads are ads served up to Facebook users based on their activity, demographic information, device use information, advertising and marketing partner-supplied information, and off-Facebook activity. The platform includes reporting tools that let you see what impact your ads have.

What is Snowflake?

Snowflake is a cloud-based data warehouse implemented as a managed service. It runs on the Amazon Web Services architecture using EC2 and S3 instances. Snowflake is designed to be fast, flexible, and easy to work with. For instance, for query processing, Snowflake creates virtual warehouses that run on separate compute clusters, so querying one virtual warehouse doesn't slow down the others.

Getting data out of Facebook

Data can be retrieved programmatically via the Facebook Ads Insights API, which is available to anyone who uses the platform. Don't confuse this with the API Facebook offers for placing and managing ads, which is not relevant to our purposes.

By following the process in the API documentation, you can make calls to the Ads Insights API to retrieve your data. You'll have access to endpoints such as impressions, clickthrough rates, and CPC, all broken out by time period.

Sample Facebook Ads data

Below is an example of what that response might look like when you query the Ads Insights API.

{
   "data": [
      {
         "impressions": "1862555",
         "adset_name": "My ad set",
         "cost_per_action_type": [
            {
               "action_carousel_card_name": "My Carousel Card 1",
               "action_type": "app_custom_event.fb_mobile_activate_app",
               "value": 0.093347346315861
            },
            {
               "action_carousel_card_name": "My Carousel Card 2",
               "action_type": "app_custom_event.fb_mobile_activate_app",
               "value": 0.38324089579301
            },
            ...
         ],
      }
   ]
}

Preparing data for Snowflake

Depending on the structure that you data is in, you may need to prepare it for loading. Take a look at the supported data types for Snowflake and make sure that the data you've got will map neatly to them. If you have a lot of data, you should compress it. Gzip, bzip2, Brotli, Zstandard v0.8 and deflate/raw deflate compression types are all supported.

One important thing to note here is that you don't need to define a schema in advance when loading JSON data into Snowflake. Onward to loading!

Loading data into Snowflake

There is a good reference for this step in the Data Loading Overview section of the Snowflake documentation. If there isn’t much data that you’re trying to load, then you might be able to use the data loading wizard in the Snowflake web UI. Chances are, the limitations on that tool will make it a non-starter as a reliable ETL solution. There two main steps to getting data into Snowflake:

  • Use the PUT command to stage files
  • Use the COPY INTO table command to load prepared data into the awaiting table from the prior step.

For the COPY step, you’ll have the option of copying from your local drive, or from Amazon S3. One of Snowflakes’ slick features lets you to make a virtual warehouse that will power the insertion process.

Keeping Facebook Ads data up to date

You now have a script that loads Facebook Ads data into your data warehouse. It's time to start thinking about how to keep the data up to date. Undoubtedly you'll have plenty of new campaigns to analyze as your business grows. The best way to keep data up to date is to write your script so that it can identify new and updated data. You can do this by building out logic with keys that auto-increment, such as updated_at or created_at. After you've built in this functionality, you can set up your script as a cron job or continuous loop to grab new data as it appears.

Other data warehouse options

Snowflake is great, but sometimes you need to optimize for different things when you're choosing a data warehouse. Some folks choose to go with Amazon Redshift, Google BigQuery, or PostgreSQL, which are RDBMSes that use similar SQL syntax. If you're interested in seeing the relevant steps for loading data into one of these platforms, check out To Redshift, To BigQuery, and To Postgres.

Easier and faster alternatives

If all this sounds a bit overwhelming, don’t be alarmed. If you have all the skills necessary to go through this process, chances are building and maintaining a script like this isn’t a very high-leverage use of your time.

Thankfully, products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your Facebook Ads data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Snowflake data warehouse.