How To Get Statistics For A Facebook “Like” Button And Shared URLs

The last article showed how to add a Facbook “Like” button to your website and observing the click event on the button to track and/or do something else with JavaScript when a user clicks on the “Like” button.

Now you might want to get some information on the URL via some automated processes like cronjobs etc. You can simply use FQL – the Facebook Query Language – for that purpose. FQL is similar to SQL but doesn’t support all of the features.

Using FQL

Here is some FQL to get some statistics for a link:

SELECT  like_count, total_count, share_count, click_count from link_stat  where  url="http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/"

You can simply use a Facebook API client SDK or access the data directly via the URL even in your browser:

https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22

This will return XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
 <link_stat>
 <like_count>4</like_count>
 <total_count>5</total_count>
 <share_count>1</share_count>
 <click_count>0</click_count>
 </link_stat>
</fql_query_response>

This means you can also use something like cURL or even PHP’s file_get_contents() method to obtain this information and parse it with SimpleXML, DOM or even regular expressions.

Facebook doesn’t seem to define any specific API call limit but there seems to be a limit to the number of calls per day.

Pitfalls

The Facebook API doesn’t support “LIKE” queries in FQL so you cannot find out how many pages on your website were liked directly – only by querying like described above for every single URL on your website.

I hope this short article is useful for you. If you like it why not just click on the “Like” button below? :)

Be Sociable, Share!
June 29th, 2010 at 10:59 am

Hi Sascha, that is very helpful information.

I have a question:

Are we limited to the count only?

Is there a way to know who (facebookID) liked what (url or xid)?

July 6th, 2010 at 7:08 pm

Just saw that you already mentioned the LIKE problem.

Also with substr it is not working :(

Like this … where substr(url,0,28) = “http://www.saschakimmel.com/”

https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20substr%28url,0,28%29%20=%20%22http://www.saschakimmel.com/%22

admin
July 7th, 2010 at 5:43 am

@Guvenc: Thanks for pointing this out, I actually tested this as well but as it didn’t work I didn’t care to mention it. Sorry if that lead you to spend time on researching it yourself.

July 11th, 2010 at 3:27 pm

thank you, I was looking for something to show who likes, but I got useful link, that is the count of sharing and Like. \ /0/
I’ll search for the fans list though ;)
Thanks a lot.

Mike E
August 22nd, 2010 at 6:00 am

Hi Sascha – I have found your article incredibly helpful – thank you so much.

Now I am sorry if this is a dumb question:

When I view the URL you supplied

https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22

I can see the results fine.

However – when I have tried to access it from scrips I get a blank result. EG I have tried simpleXML, curl, file_get_contents. I dont get errors as such… just seems like there is nothing retrurned. I have tested other XMLs, and they all load (although all are http rather than https). Is there something silly and simple I may have overlooked?

Thanks in advance

Mike

admin
August 22nd, 2010 at 6:14 am

@Mike: Have you tried using http instead of https? It works as well. The only reason I see why it wouldn’t work might be a lack of (Open)SSL support in your PHP version or SSL certificate problems (especially with curl). However you should see an error or a warning. Have you made sure that errors are displayed?

ini_set('display_errors', true);
error_reporting(E_ALL);
Mike E
August 23rd, 2010 at 6:32 am

Hi Sascha: Thank you so much!!! That was the silly thing I overlooked. http worked just fine. I haven’t checked my open ssl support yet – but its likely off (new php installation).

Thanks again for the article, and your quick response!

Lukas
September 21st, 2010 at 6:23 pm

This is great. Thank you. I am only wondering: What do the metrics mean? Can you help with that?

My first thoughts were:

- “like” count = How many people clicked on “like” (?)
- “share” count = How many people shared the url in facebook (where? In messages or just on their wall?).

- “total count = How many people clicked on “like” and shared (?) -> Makes sense in your example (4 likes + 1 share = 5), but I have pages (ex: http://www.e-fellows.net/show/detail.php/21049) with numbers like this:

like_count 22
total_count 84
share_count 30

=> 30 + 22 is not exactly 84…

- “click” count” = No idea

Do you have any more information on this?
Thanks!
Lukas

Lukas
September 21st, 2010 at 6:35 pm

Sorry for another comment. I just checked another blog posting which gives very good advice on how to interpret these stats: http://canvoo.com/blog/21028/facebook-like-button-count-inaccuracies

Here the mystery of the “total count” is being solved: total count = likes + shares + comments

Your query doesn’t extract the comments, so the total count is misleading. The article above had 22 comments, so together with the 52, that’s exactly the 84 I was looking for.

The following URL btw is a little cleaner if you want to get the URL stats:

http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.e-fellows.net/show/detail.php/21049

admin
September 22nd, 2010 at 5:35 am

Lukas, thanks for pointing this out.

October 14th, 2010 at 3:07 am

So the answer might be “no”, judging from your article, but I will ask more explicitly:

Can you use FQL to determine what are the “top ten most Liked” pages on your website for a given period of time ?

I hope the answer is yes, this would be an excellent proxy for “this is my killer content”… if not, What A Whiff On The Behalf Of Facebook !!! C’mon Facebook, this is a critical stat. If it is yes, then hallelujah facebook. Thanks !

dan
November 12th, 2010 at 11:24 pm

Hi,

I’m trying to get the same info using http://api.facebook.com/restserver.php?method=links.getStats&urls=http://danc.stagename.com/beatmaker/shared_track/26/120808811/ but all the stats are 0 event though I do have 2 people who have liked the content. I’m wondering if it’s because I have not submitted the app yet(still developing) or if there’s something else I’m missing.

I’ve also tried the solution from http://canvoo.com/blog/21028/facebook-like-button-count-inaccuracies where you get stats, esp the fan_count, by calling https://graph.facebook.com/http://danc.stagename.com/beatmaker/shared_track/26/120808811/ and all I get is the “id” field returned…

dan
November 12th, 2010 at 11:40 pm

acutally..nevermind …it was the slash at the end…

Marc
December 15th, 2010 at 11:34 pm

Your article is awesome, I haven’t found anything as good as this on the subject, I just have one problem.

Is there anyway I could get the number of comment posted through the social plugin, the fql query doesn’t return any comments for my site, even though there are comments on the page.

Thanks
Marc

Sathish
January 6th, 2011 at 8:06 am

Hello Lukas,

https://api.facebook.com/method/fql.query?query=SELECT%20share_count,click_count,%20like_count,%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.asendes.com%22

I was using this query to get the stats…everthng seems to be ok..but the click_count always returns me 0.do you have any idea?

Thanks in advance.

Sathish.

January 11th, 2011 at 9:39 pm

I have a Like button on my web page. Facebook Insights shows the Like count for the URL however there is no count shown next to the Like button on my web page. This is an example: http://this1that1whatever.com/. Does anyone have any ideas on what might be causing this misbehavior? Thanks.

February 11th, 2011 at 10:31 am

there is one facebook page also which shows detailed statistics….i forgot the url :( sorry

February 12th, 2011 at 3:06 am

To get xml stuff happening in php.
Here is my little trick.

$somexml = simplexml_file_load(‘https://api.facebook.com/method/fql.query?query=select%20%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.saschakimmel.com/2010/05/how-to-capture-clicks-on-the-facebook-like-button/%22′);

then retrieve the data like

echo $somexml->chilnode->childnode

or $childnode1 = $somexml->chilnode->childnode

eg..

$like_count = $somexml->link_stat->like_count

echo $somexml->link_stat->like_count . ‘ people like this page’; //echo’s 4 people like this page

echo $like_count . ‘ people like this page’; //echo’s 4 people like this page

$sopmexml

to quote mike below

However – when I have tried to access it from scrips I get a blank result. EG I have tried simpleXML, curl, file_get_contents. I dont get errors as such… just seems like there is nothing retrurned. I have tested other XMLs, and they all load (although all are http rather than https). Is there something silly and simple I may have overlooked?

Thanks in advance

Mike

February 17th, 2011 at 9:16 pm

Nice job!

Here is an example of how you extract the data and then sort it by likes in PHP:
http://www.sundh.com/blog/2010/11/get-likes-on-urls-from-facebook/

Pham Hoang
May 6th, 2011 at 6:24 am

Hi Sascha,

I glad to get help from you. In case, i want to get list of users who have MY FACEBOOK PAGE, so could you please help me do that?

Thanks so much!

subhadip
May 16th, 2011 at 10:26 am

Hi,

This is really helpful and just what I was looking for. Thanks!!

I have one question though. If you use the url “http://www.facebook.com/radiohead”, you will get the correct numbers, but if the url is “http://www.facebook.com/microfiches”, all the numbers are shown 0, while a visit to that URL clearly shows that this band has 85 likes.

Can you please help me in understanding why is this happening?

Thanks

martin boelsch
September 6th, 2011 at 5:21 pm

Hi is there a web page or way to see what people like most on a daily base, so similar to twitter’s trending?
thanks

Jan
December 14th, 2011 at 9:56 pm

Is there a way to know who (facebookID) liked what (url or xid)?

Sam
March 9th, 2012 at 7:35 pm

Hi,
Is there a way to get historical information or stats on it, since one user is complaining that they lost all their likes. Is there a way to find out how much they had before?

Thanks.

Luiz Henrique
March 22nd, 2012 at 3:10 am

How to get the users likers in a link? It’s possible?

Suha
May 21st, 2012 at 9:48 am

Hi ,

could you please help me I wanna make a facebook app to calculate the number of share for a link from a specific fan >>>> ( some person number of shares = ### ) is this possible ???

Thanks in advance

Tobias
November 1st, 2013 at 8:47 pm

Hi subhadip!

There is a site where you can get that statistics by only submit the url in the input field and it then show up the statistics. Go here to do that: http://facebookstats.com

When searching on that site you get correct result for http://www.facebook.com/microfiches.

Best regards
Tobias

Comments are closed.

Sascha Kimmel - Living The Web Experience Since 1996 by tricosmedia