How to get most popular Facebook page posts using graph API Insights (JavaScript) -
i'm trying list of top performing facebook posts belong page using javascript insights api. however, can't seem find way 1 api call. i've poured through documentation here: https://developers.facebook.com/docs/graph-api/reference/v2.4/insights
unless missing something, there doesn't seem way retrieve list of popular posts , include engagement/likes/shares/comments/etc in results. can generic stats total engagement posts on page, nothing breaks down post.
the thing can think of using graph api list of page's posts.
/[page-id]/posts
and use post-id results retrieve insights particular post.
/[post-id]/insights
this potentially result in me making ton of api calls i'm looking for.
i'm hoping there better way. suggestions?
i figured out solution issue using batched graph api calls documented here: https://developers.facebook.com/docs/graph-api/making-multiple-requests
in case needed impressions, engagement, likes, comments, , shares each post facebook page. achieve made call similar 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 not combine responses. each call comes separate property of response, you'll need combine different metrics in 1 object before displaying in ui.
this worked me, helps others same issue.
cheers.
Comments
Post a Comment