Skip to content

Instantly share code, notes, and snippets.

@zspotter
Created August 27, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zspotter/74f8779aee70871f3a3ceb2598317c78 to your computer and use it in GitHub Desktop.
Save zspotter/74f8779aee70871f3a3ceb2598317c78 to your computer and use it in GitHub Desktop.
Scraping FB Ads metrics and breakdowns

Some notes on using the FB Marketing Insights API to build a dashboard similar to Facebook Ads Manager Reporting --

The API docs appear very thorough, but are missing an exhaustive list of fields that you'd need to build a generic reporting tool. In my first attempt, I pulled all fields from these docs, but found a lot of discrepencies when compared to FB's UI.

It's more productive to scrape the visible options from FB's own UI, which can be done like this:

  • Visit Facebook Ads Manager Reporting and open the report builder
  • Select the Breakdowns tab
  • Use this nasty JS snippet in the dev console
  • Select the Metrics tab and repeat
Object.fromEntries(
  // Selects all checkbox elements
  [...document.querySelectorAll('._920x')]
    // Maps to the React props of each component
    .map(n => n[Object.keys(n).find(k => k.startsWith('__reactProps'))].children[0].props)
    // Maps to an object entry tuple of [apiField, userLabel]
    .map(p => ([p.columnId, p.label.$2 || p.label || '<undefined>' ]))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment