Tracking multiple facebook like buttons and like boxes (using edge.create)

Facebook api provides “FB.Event.Subscribe” for subscribing to some events, in which “edge.create” and “edge.remove” are fired when user likes and unlikes a like button or likebox. (only works with xfbml version of like buttons/ like boxes)

The example provided at facebook documentation is

FB.Event.subscribe('edge.create', function(response) {
  // do something with response
});

A typical fb like button xfbml code is

<fb:like href="http://mtsandeep.com" send="true" width="450" show_faces="true" font=""></fb:like>

You can see that we can have “href”, the link we would like to get likes for. if its left empty, the current url of the page is taken as the value. We can use “edge.create” to see when a user likes the above page. but when we have more than one like buttons, we are not able to identify which like triggered. so to track the likes individually, we need to use the “href” value which is the only varying part of the like button code.

so here is the fb edge.create code

FB.Event.subscribe('edge.create', function(href) { //href used instead of response
  if(href=="http://mtsandeep.com") {
	  alert("user liked mtsandeep.com");
  }
  else {
	  alert("user liked some other button");
  }
});

You can view a small demo here

This entry was posted in facebook. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>