Automate the export of Facebook Insights data

Jimmy C picture Jimmy C · Sep 7, 2011 · Viewed 17k times · Source

I'm looking for a way of programmatically exporting Facebook insights data for my pages, in a way that I can automate it. Specifically, I'd like to create a scheduled task that runs daily, and that can save a CSV or Excel file of a page's insights data using a Facebook API. I would then have an ETL job that puts that data into a database.

I checked out the oData service for Excel, which appears to be broken. Does anyone know of a way to programmatically automate the export of insights data for Facebook pages?

Answer

Julien L picture Julien L · Sep 28, 2011

It's possible and not too complicated once you know how to access the insights.

Here is how I proceed:

  • Login the user with the offline_access and read_insights. read_insights allows me to access the insights for all the pages and applications the user is admin of. offline_access gives me a permanent token that I can use to update the insights without having to wait for the user to login.
  • Retrieve the list of pages and applications the user is admin of, and store those in database.
  • When I want to get the insights for a page or application, I don't query FQL, I query the Graph API: First I calculate how many queries to graph.facebook.com/[object_id]/insights are necessary, according to the date range chosen. Then I generate a query to use with the Batch API (http://developers.facebook.com/docs/reference/api/batch/). That allows me to get all the data for all the available insights, for all the days in the date range, in only one query.
  • I parse the rather huge json object obtained (which weight a few Mb, be aware of that) and store everything in database.
  • Now that you have all the insights parsed and stored in database, you're just a few SQL queries away from manipulating the data the way you want, like displaying charts, or exporting in CSV or Excel format.

I have the code already made (and published as a temporarily free tool on www.social-insights.net), so exporting to excel would be quite fast and easy.

Let me know if I can help you with that. It can be done before the week-end.