Reputation: 133
I'm trying to get a list of top performing Facebook posts that belong to a page using the JavaScript Insights API. However, I can't seem to find a way to do this with just one API call. I've poured through the documentation here: https://developers.facebook.com/docs/graph-api/reference/v2.4/insights
Unless I am missing something, there doesn't seem to be a way to simply retrieve a list of popular posts and include engagement/likes/shares/comments/etc in the results. You can get generic stats like total engagement for all posts on a page, but nothing that breaks it down by post.
The only thing that I can think of is using the Graph API to get a list of a page's posts.
/[page-Id]/posts
And then use the post-Id from those results to retrieve insights for that particular post.
/[post-Id]/insights
This would potentially result in me making a ton of API calls to get what i'm looking for.
I'm hoping there is a better way. Any Suggestions?
Upvotes: 1
Views: 5082
Reputation: 133
I figured out a solution to this issue using batched graph API calls as documented here: https://developers.facebook.com/docs/graph-api/making-multiple-requests
In my case I needed to get impressions, engagement, likes, comments, and shares for each post from a Facebook page. To achieve this I made a call similar to this.
var pageName = 'YourFacebookPage',
yourToken = 'YourAccessToken';
FB.api('/', 'POST', {
access_token: yourToken,
batch:
[
{
method: 'GET',
name: 'get-posts',
relative_url: pageName + '/posts'
},
{
method: 'GET',
relative_url: '?ids={result=get-posts:$.data.*.id}'
},
{
method: 'GET',
relative_url: pageName + '/insights/post_impressions/lifetime?ids={result=get-posts:$.data.*.id}'
},
{
method: 'GET',
relative_url: pageName + '/insights/post_engaged_users/lifetime?ids={result=get-posts:$.data.*.id}'
},
{
method: 'GET',
relative_url: 'likes?ids={result=get-posts:$.data.*.id}'
},
{
method: 'GET',
relative_url: 'comments?ids={result=get-posts:$.data.*.id}'
},
{
method: 'GET',
relative_url: 'sharedposts?ids={result=get-posts:$.data.*.id}'
}
]
}, function (response) {
console.log(JSON.parse(response[1].body), 'post details');
console.log(JSON.parse(response[2].body), 'impressions');
console.log(JSON.parse(response[3].body), 'engagement');
console.log(JSON.parse(response[4].body), 'likes');
console.log(JSON.parse(response[5].body), 'comments');
console.log(JSON.parse(response[6].body), 'shares');
}
);
Unfortunately, Facebook does not combine the responses. Each call comes back as a separate property of the response, so you'll need to combine the different metrics in to one object before displaying in your UI.
This worked for me, hopefully it helps others with the same issue.
cheers.
Upvotes: 7
Reputation: 63
Unfortunely I haven't find a simple way to get this top posts from a page without having to do it manually. I'm using FQL:
select post_id, comment_info.comment_count, like_info.like_count, share_info.share_count from stream where source_id={Page ID} AND author_id = {Page ID}
You can play around with the FQL directly on the Graph API Explorer and if you need the page ID just do a request for /page-name and that should return the page name and it's page ID.
After that you need to save the results and sort them with some logic and adjust to your needs.
Good luck mate.
Upvotes: 1