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 = '';

jQuery.getJSON(""+url+"&callback=?", function(data) {
jQuery('#socialstuff span.twcount').html(data.count);

jQuery.getJSON(""+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.

  • Glenn

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

    • 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.

  • 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:

    Thanks again!

    • 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=""></script>

  • Oyunu

    Touche. Solid arguments. Keep up the amazing effort.

  • 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


    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!


    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!

  • 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!

  • 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!

  • 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.

  • 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!

  • 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

  • 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.

  • sites

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

  • 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.

  • 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!

  • Resources

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

  • tweet counter

    is there any way to specify a keyword?


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

  • 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!

  • i need cash but have bad credit

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


    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.

  • “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!

    • Matt

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

  • sinta maharani

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