chickensmitten
chickensmitten

Reputation: 495

How to get user posts Insights from Facebook API Koala Gem

Hi I am wondering how to get user posts insights from Facebook API Koala Gem.

I only found solutions that works for facebook page posts but not user posts.

I used the code below for user posts but it just returns empty array.

@graph.get_connections('me', 'insights', metric: 'page_impressions', period: 'now')

UPDATE

user = Authentication.where(user_id: current_user.id, provider: "facebook").first
oauth_access_token = user.token
@graph = Koala::Facebook::API.new(oauth_access_token)
@posts = @graph.get_connection('me', 'posts',{ fields: ['id', 'message', 'link', 'name', 'description', "likes.summary(true)", "shares", "comments.summary(true)"]})

The code above works fine, but when I try to get post insights, it returns empty array.

Upvotes: 3

Views: 1892

Answers (3)

Luffy
Luffy

Reputation: 21

Here you can see it is only for pages post
/{post-id}/insights (where this is a Page post)

Upvotes: 0

Keon Cummings
Keon Cummings

Reputation: 1811

If your using omniauth-facebook gem you just have to make sure you have the right permissions in your scope and you can use the original query.

config/initializers/omniauth.rb

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, {id}, {secret},
  :scope => 'email,manage_pages,read_stream,read_insights'
end

Also, you can get post insights for a page via koala. This worked for me.

m = Koala::Facebook::API.new(User.find(5).oauth_token)
m = m.get_connections('me', 'accounts')
m = m.first['access_token']
@post_graph = Koala::Facebook::API.new(m)
@feed = @post_graph.get_connection('me', 'feed')
@postid = @feed.first['id']
@post_data = @post_graph.get_connections(@postid, 'likes', since: "2015-05-17", until: "2015-07-17")

https://github.com/arsduo/koala/wiki/Acting-as-a-Page

Upvotes: 2

Răducanu Ionuţ
Răducanu Ionuţ

Reputation: 430

https://developers.facebook.com/docs/graph-api/reference/v2.3/post

If you check here at 'Edges' you can see that /insights are available only for Pages.

'/insights Insights for this post (only for Pages).'

I hope I am right and helped you.

Upvotes: 1

Related Questions