Facebook Page insights API to retrieve different insight metrics using python

Does anyone know any good Facebook Graph API Python SDK to retrieve insight metrics such as:

  1. Lifetime Post Total Reach
  2. Lifetime Post organic reach
  3. Lifetime Post Paid Reach
  4. Lifetime Post Total Impressions.

and many more, because I couldn't find any good python SDK with well-written documentation.


Solution 1:

Use the Facebook SDK Available for Python

I'll advise you to use the facebook-sdk module available. It has great documentation, and I'm sure you'll find all the GraphAPI Options available there.

https://pypi.python.org/pypi/facebook-sdk

pip install facebook-sdk

Thanks!

Solution 2:

First of all, you have to apply for read_insight permissions to retrieve all of these insight parameters. After that use, the python SDK mentioned by @monkfromearth and the code to get insights using SDK is:

field = "insights.metric(page_fans, page_impressions,page_impressions_unique," \
                    "page_impressions_organic_unique,page_impressions_organic,page_engaged_users)." \
                    "since({}).until({}).period(day)"

In the since and until parameters mentioned in field parameter, here you will provide the start date and date to retrieve data between otherwise Facebook Graph API will provide you data of last 93 days.

api = GraphAPI(access_token=page_access_token, version='2.10')
response = api.get_object(id=page_id, fields=[field])

In this step, you will initiate the GraphAPI object of Facebook Python SDK and then call the get_object method with parameters page_id from which you want the insights to be retrieved and fields parameter to retrieve insights fields from the page. In response, you will get the desired insights attributes you had mentioned in the field parameter.