Tuesday, April 26, 2011

Who "Likes" You Baby

received the following email from zhang na:

Hi, i met a trouble , i want to get the value
The total number of people who have liked your Page
but i try all the function , cannot get the lastest data , i used graph.facebook.com/appid//insights/page_fans/lifetime, but i get the old three days data
can you give me an example code ?

Here is my response:

Hi zhang na,

There is an easier way to get the count of the number of people who have liked your page, simply do a "GET" call to the Graph API with the id of the page. Facebook will return the current number of likes along with other public information. Unlike insights, no access token or permissions are necessary to access public information. The code below assumes you have included the Facebook JavaScript SDK on your web page:

function GetLikeCount() {
 var pageId = "19292868552";
 FB.api("/" + pageId, function (data) {
  var count = 0;
  if (data && data.likes) {
   count = data.likes;
  alert("Page " + pageId + ", likes = " + count);

I am always happy to answer questions, assuming of course, that I know the answer. Please post questions in the comments and I will answer as soon as I am able.