Search code examples
facebookfacebook-graph-apifacebook-marketing-api

How can I get reach demographic details for ad campaigns using Facebook marketing API?


I'm building a system to automatically ingest date from the Facebook API. I can easily get campaign details such as CPC, CPM, Reach, etc. However, now I'd like to grab reach demographic information such as this one below:

Facebook campaign reach demographic

I believe that it does have something to do with Insights API, however, I can't seem to find any sort of details on the insights docs: https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group/insights/

UPDATE

I did see eventually that two possibly useful params in that list are age and gender. In my cloud function I'm already grabbing campaign insights using this:

await campaign.getInsights([
    'impressions',
    'cpc',
    'spend',
    'ctr',
    'conversions',
    'clicks',
    'cpm',
    'reach'
]);

To which I then added 'age' and 'gender' in the list of fields. However, I then got this error:

message: '(#100) age, gender are not valid for fields param. please check https://developers.facebook.com/docs/marketing-api/reference/ads-insights/ for all valid values'

This is quite odd, because I was checking their node package you can see that the fields I'm supposed to feed to the getInsights method are fields belonging to the AdsInsights class (https://github.com/facebook/facebook-nodejs-business-sdk/blob/3c0785aba14f44ba52434e66bb80ed410a6ca368/src/objects/campaign.js#L239)

And when I visit the AdsInsights file I can see both 'age' and 'gender' as part of the list of fields: https://github.com/facebook/facebook-nodejs-business-sdk/blob/3c0785aba14f44ba52434e66bb80ed410a6ca368/src/objects/ads-insights.js#L16


Solution

  • You need to add the breakdowns field to params parameter of this function rather than to the fields parameter to this function. This is taken from Facebook's example (https://github.com/facebook/facebook-nodejs-business-sdk/blob/3c0785aba14f44ba52434e66bb80ed410a6ca368/examples/ads_insights_edge_ad_campaign_insights.js)

    let fields, params;
    fields = [
      'impressions',
    ];
    params = {
      'breakdown' : 'publisher_platform', \\ Replace this with age or gender
    };
    const insightss = (new AdSet(id)).getInsights(
      fields,
      params
    );
    logApiCallResult('insightss api call complete.', insightss);