Get Twitter and Facebook Link Statistics with JSON and jQuery

Both Twitter and Facebook have little JavaScript widgets that allow you to share a page using the respective service, displaying a running total of users who have done so. While that’s fine for most purposes, what if you just need the count, for some atypical application?

It’s not well-documented, but the two social media sites have JSON APIs for that purpose.

With a little bit of jQuery magic, you can collect the values on page load and update the DOM with the number. Here’s something I threw together for a project I was working on:

(function() {

var url = 'http://xkcd.com/792/';

jQuery.getJSON("http://urls.api.twitter.com/1/urls/count.json?url="+url+"&callback=?", function(data) {
jQuery('#socialstuff span.twcount').html(data.count);
});

jQuery.getJSON("https://graph.facebook.com/"+url+"&callback=?", function(data) {
jQuery('#socialstuff span.fbcount').html(data.shares);
});

}());

As long as you remember to include jQuery, and have the right HTML elements for the JavaScript to populate, it’s pretty much plug-and-play.

You can see it in action here.

  • http://www.gfwebsoft.com Glenn

    I didn’t quite understand. Could you please explain a bit more what this is actually counting ?

    • http://www.webmaster-source.com Matt

      The Twitter stat is the number of times a URL has been retweeted. The Facebook one is the number of times a URL has been Liked, e.g. using the Like button you see on websites.

  • http://www.longren.org/ tyler

    Thanks for this tip! I ended up modifying it to pull number of comments on a page (using Facebook comments).

    Using the method suggested by Facebook here didn’t work, all.js kept throwing errors for some reason:
    http://developers.facebook.com...../comments/

    Thanks again!

    • http://www.webmaster-source.com Matt

      Ah, that uses FBML. Make sure you have this on your page if you want to use anything shown on that guide:

      <div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>

  • http://www.elmi-rocks.de/page8.php Oyunu

    Touche. Solid arguments. Keep up the amazing effort.

  • http://heavenlygaming.com/index.php?members/fredericka.2331/ titanium engagement ring

    Hey just wanted to give you a quick heads up.

    The words in your article seem to be running off
    the screen in Ie. I’m not sure if this is a formatting issue or something to do with browser compatibility but I thought I’d post to let you know.
    The design look great though! Hope you get the issue solved soon.
    Many thanks

  • http://Dentalimplant3.Livejournal.com/642.html livejournal.com

    You are so awesome! I don’t suppose I have read through something like that before. So wonderful to discover someone with some original thoughts on this subject. Really.. many thanks for starting this up. This website is one thing that’s
    needed on the web, someone with some originality!

  • http://iwebsiterank.com/yelp.com iwebsiterank.com

    Wow that was unusual. I just wrote an very long comment but
    after I clicked submit my comment didn’t show up. Grrrr… well I’m
    not writing all that over again. Anyway, just wanted to say wonderful blog!

  • http://test.com/ Naomi

    Have you ever thought about including a little bit more than just your articles? I mean, what you say is valuable and everything. Nevertheless imagine if you added some great images or video clips to give your posts more, “pop”! Your content is excellent but with images and videos, this site could definitely be one of the greatest in its niche. Great blog!

  • http://dietaparaganharmassamuscular.wordpress.com/ dieta para perder gordura e ganhar massa muscular

    First of all I want to say great blog! I had a quick question that I’d like to ask if you don’t mind.
    I was interested to find out how you center yourself and clear your mind prior to writing.

    I’ve had difficulty clearing my thoughts in getting my ideas out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes are wasted just trying to figure out how to begin. Any suggestions or tips? Cheers!

  • http://motormavens.net/ Join the Conversation

    I think that everything said was very logical. However,
    what about this? suppose you were to create a killer headline?
    I mean, I don’t want to tell you how to run your website, but what if you added a title that makes people want more? I mean Get Twitter and Facebook Link Statistics with JSON and jQuery is kinda plain. You should look at Yahoo’s front page and note how they write news titles to get people to open the links.
    You might add a video or a related pic or two to grab people interested about everything’ve written. In my opinion, it would make your posts a little bit more interesting.

  • http://scsluts.com woman seeks woman

    My partner and I absolutely love your blog and find most of your post’s to be exactly what I’m looking for.
    Would you offer guest writers to write content for you personally?

    I wouldn’t mind writing a post or elaborating on a lot of the subjects you write concerning here. Again, awesome site!

  • http://www.kfv-reutlingen.de/box_content/externlink.php?url=howtorepairabrickwall.co.uk Eugenia

    If you wish for to grow your knowledge simply keep visiting this
    web page and be updated with the most recent information posted here.
    by Cleo see my site Eugenia

  • http://turnmeup.net/DamonDt stopping snoring naturally

    The good news is that taking 100 mg of CoQ10 twice a day seems to improve heart failure–dramatically well in some
    people. And, taking that amount tends to lower high blood pressure about
    10 points. It also increases your energy level & improves
    heart health in other techniques.

  • http://www.wipeoutzits.com sites

    I was looking at some of your posts on this site and I conceive this
    site is rattling instructive! Continue posting .

  • http://skiing47guide.tumblr.com/ wine

    You can find out precisely what your current region. The primary task involved in ensuring a good time if you have
    adequate space in your area. A strawberry frozen yoghurt muted the liquid became dark and
    round, but it did not meld with the floors.

  • http://www.shutterbean.com/2012/persimmon-butter-lettuce-salad/ d’leedon review

    you are actually a excellent webmaster. The site loading pace is amazing.
    It sort of feels that you are doing any unique trick. Also, The contents are masterwork.
    you’ve done a magnificent job on this subject!

  • http://www.oooforum.org/forum/profile.phtml?mode=viewprofile&u=266700 Resources

    I am really glad to glance at this weblog posts which carries lots of helpful data, thanks for providing these kinds of information.

  • http://newexception.com/tweet-counter tweet counter

    is there any way to specify a keyword?

  • http://www.wsb.org.uk www.wsb.org.uk

    I always spent my half an hour to read this weblog’s articles all the time along with a mug of coffee.

  • http://www.jailbreakios6secrets.com/ 6.1.3 jailbreak

    Hi there are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and create
    my own. Do you need any coding knowledge to make your own blog?
    Any help would be really appreciated!

  • http://need-dollars.com i need cash but have bad credit

    WOW just what I was searching for. Came here by searching for
    christian louboutin outlet

  • http://bellyfattop.com/ bellyfattop.com

    When comparing the costs, these services are much more affordable and feasible
    than maintaining an in-house how to burn belly fat fast
    system and billing experts. Using the right codes is important for
    the job is very rewarding and challenging yet dangerous work at the same
    time very useful.

  • http://infotechresource.com/ “work from home”

    When I originally commented I clicked the “Notify me when new comments are added” checkbox
    and now each time a comment is added I get several e-mails with the same comment.

    Is there any way you can remove me from that service?

    Thank you!

    • http://www.webmaster-source.com Matt

      There should be a link to unsubscribe in any email notifications you receive.

  • http://obatkeputihanonline.com sinta maharani

    actual statistics link on facebook is very helpful in the sale obat untuk mengatasi keputihan secara herbal