Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading Facebook ads statistics in background (no web browser)

I am developing a backoffice server application wich is supposed to email our customers with ads campaign statitistics, where camapaigns are bundled from various providers, one of them being Facebook.

Now the problem is how to download specific ads campaigns from Facebook. Basically what I need is a table for specific campaign with values for impressions and clicks for each day from requested range.

One problem is that the application is background server process, running periodically without user interactions, so I suspect there could be a problem with authentication.

From what I have read so far, I believe I am supposed to

  • register my application on facebook
  • apply for Ads API access (?)
  • use either legacy REST API to get statistics, or perhaps adsstatistics from graph api (but I believe that the format there is not divided for particular days).

How am I supposed to authenticate in this case?

Any suggestion as to what is the right solution here?

(Note: In the past, I was doing similiar for Google, where I have successfully used http://code.google.com/intl/cs/apis/adwords/docs/guides/reporting.html - this is just for reference about what I need to achieve).

like image 956
Mirek Fidler Avatar asked Jan 04 '12 09:01

Mirek Fidler


1 Answers

See this page for authentication once you have Ads API access: http://developers.facebook.com/docs/authentication/

In the end you'll need an access_token which is specific to your application and the Facebook account you are accessing.

Here's our process:

  1. Log into Facebook account that contains the ads data
  2. Paste this into that browser's address bar: https://www.facebook.com/dialog/oauth?client_id={your_application_id}&scope=ads_management,offline_access,read_insights&redirect_uri={your website}
  3. You should be taken to a page that allows you to authorize your application for that Facebook account (green Allow button)
  4. Copy the authorization code that appears after code= in the redirected url
  5. Paste this into that browser's address bar (you may not have a client_secret with your application, if you don't try this without the client_secret): https://graph.facebook.com/oauth/access_token?client_id={your_application_id}&redirect_uri={your website}&client_secret={your application secret}&code={code you got from step 4}
  6. You should be taken to a page containing the access_token

I do not think you need to store the Authorization code since the access_token shouldn't expire (if you requested offline_access) unless the log in info is changed for the Facebook Account.

Stats Retrieval For stats retrieval, I would not use legacy REST API since Facebook will depreciate it. Graph API does allow stats retrieval by day, use:

GET https://graph.facebook.com/stats/{starttime}/{endtime}/stats?ids={campaign_id}&access_token=...

starttime and endtime can be in YYYY-MM-DD HH:MM:SS format or as a unix (epoch?) time

like image 140
John Pickard Avatar answered Nov 10 '22 12:11

John Pickard