By Rudderstack Team

How to load data from Facebook Ads to PostgreSQL

This post helps you with loading your data from Facebook Ads to PostgreSQL. If you are looking to get analytics-ready data without the manual hassle you can integrate Facebook Ads to PostgreSQL with RudderStack, so you can focus on what matters, getting value out of your advertising data.

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

In this post we’ll focus only on how to extract data out of 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 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 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.

-F 'level=campaign'
-F 'fields=[]'
-F 'access_token=<ACCESS_TOKEN>'<CAMPAIGN_ID>/insights
curl -G
-d 'access_token=<ACCESS_TOKEN>'
curl -G
-d 'access_token=<ACCESS_TOKEN>'

Data can be returned in either xls or csv format and when the report is ready based on your request you can access it from a URL like the following:<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