Topic: facebook like button updating my internal like

I am implementing a facebook like button on my rails 3.1 app but would like to keep track of the amount of facebook likes. This is helpful for me as it will allow me to sort my images by the most facebook likes. I am using the simpler way of iframe and facebook like plugin. My code for my helper is

def facebook_like
        content_tag :iframe, nil, :src => "http://www.facebook.com/plugins/like.php?href=#{CGI::escape(request.url)}&layout=standard&show_faces=true&width=450&action=like&font=arial&colorscheme=light&height=80", :scrolling => 'no', :frameborder => '0', :allowtransparency => true, :id => :facebook_like
end

For my model, I have a personalize like attribute. What would be the best way to increase the like attribute whenever someone click on the facebook like.

I also know that facebook has a graph.facebook.com that will tell me how many shares there are for that object. Will that be relevant in updating my own internal like?

Re: facebook like button updating my internal like

I haven't had a need for a Facebook like button before, but I googled around a bit and found this stackexchange question: http://stackoverflow.com/questions/6542 … s-in-rails

The poster is using an FB javascript plugin instead of the php one, which may work better.

As far as retrieving the number of likes for a page/object, I'm not sure. You might try the fb_graph gem for interfacing with graph.facebook.com. It looks promising.

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: facebook like button updating my internal like

Thanks, will see if that works! Apparently graph.facebook.com returns an ajax/json object though so if i do graph.facebook.com/id=www.mysite.com it will return the number of shares on my site. I am just wondering what is the best way to implement the update feature. Should I update my internal likes whenever someone views my image page?

Re: facebook like button updating my internal like

If I were you, I wouldn't maintain a separate count of likes in your database. I think that's what you were asking, but that could get into problems of discrepancies between the count facebook has and your count of likes.

To update the likes a page has on Facebook, just use the js plugin provided by Facebook. Then, whenever someone clicks the like button, the count is incremented and updated on Facebook. Views of the page aren't likes. If you want to keep track of page views, you can use something like google analytics to keep track of that.

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com

Re: facebook like button updating my internal like

Is there a way for me to get access to stats on google analytics so that I can sort by the most viewed. That's actually what I want to implement

Re: facebook like button updating my internal like

If you are wanting to implement a "Most Popular Images" type of thing on your application, maintaining a counter in the db shouldn't be too hard. I don't know of how to do it with Google Analytics. You could do it with the graph.facebook if you'd like as well. For the database way, you would have a method that increments by one each time the show action is called and stores that value in the db. I'm sorry I can't give you any code for that, I'm just learning Rails.

ruby.railstutorial.org is a great tutorial for building a rails app. In Chapter 10, he shows how to arrange items by date created. The concept would be the same for arranging by number of page-hits.

If you do it that way, you'll also want to make sure that people can't alter the counter in order to get their image on the homepage or something like that. I'm not sure how to implement that correctly. Maybe someone more knowledgeable here could jump in.

Last edited by gorrillamcd (2012-03-30 17:24:48)

Change is a vector. You can have all the change in the world, but if it doesn't go in the right direction, what good is it? | techonamission.com