Recently I had to analyze interactions on a Facebook page. I need to fetch all the contents from the stream and analyze user actions. Retrive interactions count foreach post can be hard because Facebook APIs are like hell: they change very fast, return a lot of errors, have understandable limits and give you many headache.

Anyway after a lot of tries I found a way to fetch quantitative informations about posts and photos on the stream. First of all you need the contents.

Get the contents

Graph endpoint is: https://graph.facebook.com/. You can fetch page data (I use the BBCNews page as example) at:

https://graph.facebook.com/bbcnews/posts?access_token=your_access_token

To get a valid access token you have different ways and Facebook let you choose many different kind of access tokens, each one with a different rate limit.

Returned data is a JSON array of elements. Each elements has a lot of properties which describe items on the timeline. The returned element included into the stream has just a subset of these properties (last comments, last likes, some counters). Here you can find text content, pictures and links. To get more data you need three more properties: id, type and object_id.

Status updates are identified by type “status”Photos are identified by type “photo” and Video by type “video”. The id field is used as identifier for the entry on the stream. The object_id instead is used to identify object inside the Facebook graph.

Actions: comments, likes and shares

Comments are returned paginated and sometimes APIs doesn’t return the entire list. To get the total count you need to specify the parameter summary=true.

https://graph.facebook.com/228735667216_10151700273382217/comments?summary=true&access_token=your_access_token

At the end of response you can find additional informations about comments feed. total_count displays the count.

"summary": {
"order": "ranked",
"total_count": 100
}

Likes are similar to comments. They have similar limitations and have a similar endpoint to retrive data with the same parameter summary=true.

https://graph.facebook.com/228735667216_10151700273382217/likes?summary=true&access_token=your_access_token

This time summary shows only total count:

"summary": {
"total_count": 949
}

Shares count can be found as part of the object detail.

https://graph.facebook.com/228735667216_10151700273382217/?access_token=your_access_token

After created and updated date you find shares property:

"shares": {
"count": 238
}

Convert the object_id

Depending on you data feed, sometimes id is not available and you have to handle the object_id. To be able to use previous methods you need to query the Facebook database using FQL looking for the story_id.

SELECT page_story_id
FROM photo
WHERE object_id = '10151700273362217'

https://api.facebook.com/method/fql.query?format=json&acces_token=#your_access_token&query=SELECT%20page_story_id%20FROM%20photo%20WHERE%20object_id%20%3D%20%2710151700273362217%27

The result is the page_story_id (the id of the post on the feed) of the object.

"data": [
{
"page_story_id": "228735667216_10151700273382217"
}
]

Now you can use this to retrieve counters and data.