Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the FB marketing api, there is an option to export_format: 'csv', how do I download the csv file?

I'm looking to download a CSV from my api call to FB ads, I've set it in the parameters as per the code below, the call works fine but I only get a paginated JSON as a response. I'm not quite sure how to access the CSV report, the response doenst give me any report ID or download link. How can I download this CSV?

Using nodejs facebook-nodejs-ads-sdk image showing the parameter information 'export_format' Documentation: https://developers.facebook.com/docs/marketing-api/reference/adgroup/insights/


var getReport = function(){
  console.log("API for FB report has been called")
  let ads_insights;
  let ads_insights_id;

  const logApiCallResult = (apiCallName, data) => {
    console.log(apiCallName);
    if (showDebugingInfo) {
      console.log('Data:' + JSON.stringify(data));
    }
  };

  const fields = [
    'ad_name',
    'impressions',
    'reach',
    'spend'
  ];

  const params = {
    'export_format': 'csv',
    'export_name': new Date() + 'fb-download',
    'level' : 'ad',
    'filtering' : [],
    'breakdowns' : [],
    'time_range' : {'since':'2018-01-22','until':'2018-01-23'}
  };

  new AdAccount(ad_account_id).getInsights(
    fields,
    params).then((result) => {
    logApiCallResult('ads_insights api call complete.', result);
    console.log("##########" + result + "#####")
    ads_insights_id = result[0].id;
  }).catch((error) => {
    console.log(error);
  });
}
like image 509
Daniel Mcauley Avatar asked Jan 23 '18 21:01

Daniel Mcauley


People also ask

How do I extract data from a Facebook ad?

Extract data from Facebook Ads. You can pull your data from 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.

Does Facebook ads have an API?

The Marketing API is an HTTP-based API that you can use to query data, create and manage ads, and perform a wide variety of other tasks. The Marketing API can programmatically access Facebook's advertising platform and optimize your business operations.


1 Answers

Exporting actual report files doesn't seem to be a part of the official Facebook Graph API.

My solution was to make a GET request to the following endpoint:

https://www.facebook.com/ads/ads_insights/export_report/?report_run_id=<REPORT_RUN_ID>&name=<REPORT_FILE_NAME>&format=csv&access_token=<FACEBOOK_API_KEY> 

Please note that this endpoint will output a file with columns that constantly change without warning, so its not good for automated data collection. The only way to export standardized reports is in the JSON format.

The docs for this endpoint are sort of hard to find: located here.

like image 120
Dario Macieira Avatar answered Oct 06 '22 18:10

Dario Macieira