Skip to content

@jonathanmoore /gist:2640302
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Get the share counts from various APIs

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

GET URL:

http://cdn.api.twitter.com/1/urls/count.json?url=http://stylehatch.co

Returns:

{
    "count":528,
    "url":"http://stylehatch.co/"
}

Facebook

GET URL:

http://graph.facebook.com/?id=http://stylehatch.co

Returns:

{
   "id": "http://stylehatch.co",
   "shares": 61
}

Pinterest

GET URL:

http://api.pinterest.com/v1/urls/count.json?callback=&url=http://stylehatch.co

Result:

({"count": 0, "url": "http://stylehatch.co"})

LinkedIn

GET URL:

http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&format=json

Returns:

{
    "count":17,
    "fCnt":"17",
    "fCntPlusOne":"18",
    "url":"http:\/\/stylehatch.co"
}

Google Plus

POST URL:

https://clients6.google.com/rpc?key=YOUR_API_KEY

POST body:

[{
    "method":"pos.plusones.get",
    "id":"p",
    "params":{
        "nolog":true,
        "id":"http://stylehatch.co/",
        "source":"widget",
        "userId":"@viewer",
        "groupId":"@self"
        },
    "jsonrpc":"2.0",
    "key":"p",
    "apiVersion":"v1"
}]

Returns

[{
    "result": { 
        "kind": "pos#plusones", 
        "id": "http://stylehatch.co/", 
        "isSetByViewer": false, 
        "metadata": {
            "type": "URL", 
            "globalCounts": {
                "count": 3097.0
            }
        }
    } ,
    "id": "p"
}]

StumbledUpon

GET URL:

http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http://stylehatch.co

Result:

{
    "result":{
        "url":"http:\/\/stylehatch.co\/",
        "in_index":true,
        "publicid":"1iOLcK",
        "views":39,
        "title":"Style Hatch - Hand Crafted Digital Goods",
        "thumbnail":"http:\/\/cdn.stumble-upon.com\/mthumb\/941\/72725941.jpg",
        "thumbnail_b":"http:\/\/cdn.stumble-upon.com\/bthumb\/941\/72725941.jpg",
        "submit_link":"http:\/\/www.stumbleupon.com\/submit\/?url=http:\/\/stylehatch.co\/",
        "badge_link":"http:\/\/www.stumbleupon.com\/badge\/?url=http:\/\/stylehatch.co\/",
        "info_link":"http:\/\/www.stumbleupon.com\/url\/stylehatch.co\/"
    },
    "timestamp":1336520555,
    "success":true
}
@scottcorgan

Awesome stuff man!

But, at Sevenly, we've found a better way to count the real total shares on Facebook. It's much more complicated, but far more accurate.

Using FB Query for a specific URL only

https://graph.facebook.com/fql?q=SELECT%20like_count,%20total_count,%20share_count,%20click_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20=%20%22http://sevenly.org/FightForHer%22

At Sevenly, we use this url to get the current total shares for the unique URL for this week's campaign. This helps us include comments, shares, and likes in the count.

The Result

{
   "data": [
      {
         "like_count": 757,
         "total_count": 1587,
         "share_count": 680,
         "click_count": 0,
         "comment_count": 150
      }
   ]
}

Using the Graph API to get all domain mentions

https://graph.facebook.com/10150605670960507/insights/domain_stories/day&since=1325376000&until=1327881600&access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXsince=1327881600&until=1330387200

It's complicated because you have to to go through each page of results and add them up. BUT, this is far more accurate, as it counts the total times your domain was not only shared from the page, but shared once it made it's way onto Facebook.

The Results

{
   "data": [
      {
         "id": "10150605670960507/insights/domain_stories/day",
         "name": "domain_stories",
         "period": "day",
         "values": [
            {
               "value": 142,
               "end_time": "2012-01-30T08:00:00+0000"
            },
            {
               "value": 355,
               "end_time": "2012-01-31T08:00:00+0000"
            },
            {
               "value": 237,
               "end_time": "2012-02-01T08:00:00+0000"
            },
            {
               "value": 159,
               "end_time": "2012-02-02T08:00:00+0000"
            },
            {
               "value": 193,
               "end_time": "2012-02-03T08:00:00+0000"
            },
            {
               "value": 137,
               "end_time": "2012-02-04T08:00:00+0000"
            },
            {
               "value": 114,
               "end_time": "2012-02-05T08:00:00+0000"
            },
            {
               "value": 73,
               "end_time": "2012-02-06T08:00:00+0000"
            },
            {
               "value": 813,
               "end_time": "2012-02-07T08:00:00+0000"
            },
            {
               "value": 463,
               "end_time": "2012-02-08T08:00:00+0000"
            },
            {
               "value": 376,
               "end_time": "2012-02-09T08:00:00+0000"
            },
            {
               "value": 260,
               "end_time": "2012-02-10T08:00:00+0000"
            },
            {
               "value": 192,
               "end_time": "2012-02-11T08:00:00+0000"
            },
            {
               "value": 287,
               "end_time": "2012-02-12T08:00:00+0000"
            },
            {
               "value": 234,
               "end_time": "2012-02-13T08:00:00+0000"
            },
            {
               "value": 356,
               "end_time": "2012-02-14T08:00:00+0000"
            },
            {
               "value": 155,
               "end_time": "2012-02-15T08:00:00+0000"
            },
            {
               "value": 133,
               "end_time": "2012-02-16T08:00:00+0000"
            },
            {
               "value": 96,
               "end_time": "2012-02-17T08:00:00+0000"
            },
            {
               "value": 132,
               "end_time": "2012-02-18T08:00:00+0000"
            },
            {
               "value": 80,
               "end_time": "2012-02-19T08:00:00+0000"
            },
            {
               "value": 123,
               "end_time": "2012-02-20T08:00:00+0000"
            },
            {
               "value": 723,
               "end_time": "2012-02-21T08:00:00+0000"
            },
            {
               "value": 442,
               "end_time": "2012-02-22T08:00:00+0000"
            },
            {
               "value": 288,
               "end_time": "2012-02-23T08:00:00+0000"
            },
            {
               "value": 286,
               "end_time": "2012-02-24T08:00:00+0000"
            },
            {
               "value": 349,
               "end_time": "2012-02-25T08:00:00+0000"
            },
            {
               "value": 158,
               "end_time": "2012-02-26T08:00:00+0000"
            },
            {
               "value": 110,
               "end_time": "2012-02-27T08:00:00+0000"
            }
         ],
         "title": "Daily Total Actions",
         "description": "Daily The number of times people posted a link to your site through an action on a social plugin or through a status message or Wall post. (Total Count)"
      }
   ],
   "paging": {
      "previous": "https://graph.facebook.com/10150605670960507/insights/domain_stories/day&since=1325376000&until=1327881600&access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX?since=1325376000&until=1327881600",
      "next": "https://graph.facebook.com/10150605670960507/insights/domain_stories/day&since=1325376000&until=1327881600&access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX?since=1330387200&until=1332892800"
   }
}

AND, It's an awesome daily breakdown. Perfect for analytics junkies!

@timparker

Insanely useful - thanks!

@harshavardhana

Amazing - thanks

@wilbertsantos

hello there, there's a new way to get the different social counts, check this link http://www.upgradedtutorials.info/php/get-the-delicious-stumble-and-linkedin-shares-count-using-php/. the code was so handy and useful, you can also use it as much as you want. Sparky!

@cube3x

Excellent collection. Here is a complete and evolving list of apis to fetch social counts with examples: http://cube3x.com/get-social-share-counts-a-complete-guide/

@cube3x

This is for wordpress users. Here is a well structured and responsive plugin to display social trends on your wordpress site: http://cube3x.com/all-in-one-social/

This plugin mainly uses these social apis to show the counts with a uniform design across the browsers.

@evansims

Awesome share, thank you. I've built a PHP wrapper around this and included support for a few additional services. It's available for your forking pleasure here.

@gwing33

I'm guessing this twitter api is going to be effected by this: https://dev.twitter.com/blog/api-v1-retirement-final-dates

@boomkap

If I make a server side request from a Heroku Dyno, pinterest returns a 403 response. Anybody else have this issue?

@boomkap

Twitter ignores query strings. So for example, If I am looking for a share count for a youtube video .

http://m.youtube.com/watch?feature=relmfu&v=qZdo2_Y7Aiw

API Call

http://cdn.api.twitter.com/1/urls/count.json?url=http://m.youtube.com/watch?feature=relmfu&v=qZdo2_Y7Aiw

JSON Response

{"count":29623035,"url":"http:\/\/m.youtube.com\/watch\/"}

Makes perfect sense as anything after & unencoded will assume another parameter other than url.

So I encode the URL but even then it seems like it ignores some query params.

API Call

http://cdn.api.twitter.com/1/urls/count.json?url=http%3A%2F%2Fm.youtube.com%2Fwatch%3Ffeature%3Drelmfu%26v%3DqZdo2_Y7Aiw

JSON Response

{"count":1,"url":"http:\/\/m.youtube.com\/watch\/?v=qZdo2_Y7Aiw"}

The JSON response swallows the "feature=relmfu" parameter altogether. Facebook does not have this issue.

@jerwarren

Here's a method for getting Google+ counts without an api key:

https://plusone.google.com/_/+1/fastbutton?url=$URL_ENCODED_URL&count=true

it returns json. The following regex will get you the count: /c: ([0-9.]+) /

@sashkomatviychuk

where i can get API_KEY?

@roneesh

Hello, nyquildotorg mentioned that the https://plusone.google.com/.. url can return json, I'm trying to figure out how to make it return json but no such luck. Can someone go into a bit more detail on how to get that url to return json?

@texaswebguy

Pinterest complains that the callback= is null on your example.

@bkd27

The Facebook graph call can also work with multiple urls at once. Using the parameter “ids” instead of “id” allows a list of comma separated urls.

http://graph.facebook.com/?ids=[first url],[second url],[third urls]

The result looks returns an object with the url as the key.

http://graph.facebook.com/?ids=http://www.bharatdivya.com,http://www.bkd27.com,http://www.pluginscart.com,http://computersciencearticle.in,http://couponzocean.com

Response returned by Facebook graph api is:

{
"http://www.bharatdivya.com": {
"id": "http://www.bharatdivya.com",
"shares": 25
},
"http://www.bkd27.com": {
"id": "http://www.bkd27.com"
},
"http://www.pluginscart.com": {
"id": "http://www.pluginscart.com"
},
"http://computersciencearticle.in": {
"id": "http://computersciencearticle.in",
"shares": 12
},
"http://couponzocean.com": {
"id": "http://couponzocean.com"
}
}

@TesfayKidane

@texaswebguy , I changed callback= to callback" and it worked for me.
example : http://api.pinterest.com/v1/urls/count.json?callback%20&url=http://google.com

@the0ther

this is so good. i thank you.

@IsaiasNascimentoCaetano

This is very good.

@dsposito

Cheers to @evansims for his initial work. I recently forked and refactored his script to provide a nice OO PHP SocialWorth class. It allows you to easily query multiple URLs and set which networks you want to query.

@jivinivan

Check out the following for Google+ count.

http://share.yandex.ru/gpp.xml?url=http://google.com

@xiaojiangang

good ~!

@maxicecilia

Another option to check g+:

https://plusone.google.com/_/+1/fastbutton?url=http://google.com

Not a JSON output, but works if you need a quick check.

@hanzouti

Any idea for disqus?

@cmwinters

This has been really helpful.

Here's a solution for checking Google+ in Rails without an API key. It opens a fastbutton page and uses Nokogiri to parse it and return the share count.

https://gist.github.com/cmwinters/cc867a5be41707953d23

@aligajani

Will Guzzle make this faster than using cURL?

@patrick-mcdougle

@aligajani No, because Guzzle likely uses cURL.

@tatthien

Really helpful, it save my day.

@0Neji

This is great! I'm having a little trouble getting the Google+ count however.

A linked to article which has an API key put in already works fine but when I substitute that for my own API Key - I get a 0 share count return for the same URL.

I've enabled the Google+ API in my Developers Console, what else am I missing?

@mcsheffrey

@0Neji I think the G+ method is deprecated. This worked for me http://stackoverflow.com/a/21866500

@hyip

Here is another solution. All counters including G+ works correctly. http://stackoverflow.com/a/28000630

@dsulli99

People like you make the Internet a better place.

@Timrael

Great guide. I made a ruby gem according this info:
https://github.com/Timrael/social_shares
I hope it will help you <3

@Cocosoft

Any ideas for XING?

@dandv

Hey @jonathanmoore, are you still around to update this?

  • The LinkedIn API doesn't work if called directly from the client; you need a JSONP callback.
  • Google Plus without authentication is possible:

    function GooglePlus(url) {
    // bypass same-origin restriction
      jQuery.getJSON('http://anyorigin.com/get?callback=?&url=' + encodeURIComponent('https://plusone.google.com/_/+1/fastbutton?url=http://' + url), function (data){
      alert(data.contents.match(/{c: (\d+)/)[1]);
    });
    }
@gemblue

Thanks.. but i always get (#4) Application request limit reached on facebook graph api.

@noodlebreak

Is there a way to just get the share count by not sending multiple requests with http or https and www in Twitter and Pinterest?

Because I've checked and confirmed, that Twitter and Pinterest indeed differentiate between http,https and www links.

So, for a link like this: abc.com/xyz/00,
I'd have to check for

http://abc.com/xyz/00

https://abc.com/xyz/00

http://www.abc.com/xyz/00

https://www.abc.com/xyz/00

Because users on my site could either access it as http or https and with or without www.

@cqcn1991

Very good, and thanks a lot. And I want to ask, is it possible to get the Pocket marked number?

@rafaelcavalcante

"NetworkError: 415 Unsupported Media Type". I can't even test lol

@Paki2013

Hi,
Any one can help me please....!!
i need a code for

Get details of users/visitors who share our URL on Facebook, Twitter.

i need a list of peoples who share our link on facebook and twitter.

@radugroza

Cool stuff man!

Anyone figured out / found how to get share count from Xing ?

@onigetoc

Maybe you want to get stars and watch for Github?
https://api.github.com/repos/twbs/bootstrap

@kentbrew

For faster results from Pinterest, please use widgets.pinterest.com and not api.pinterest.com. Like so:

http://widgets.pinterest.com/v1/urls/count.json?url=http%3A%2F%2Flaughingsquid.com

Encoding your URL parameter is a good habit to learn; if you need to send one with a question mark in it, unpredictable things may happen.

Callback is not a required parameter; if you omit, your reply will look like this:

receiveCount({"url":"http://laughingsquid.com","count":13})

As noted previously, URLs are not normalized for count.json. You'll get a much higher count from laughingsquid by appending a forward-slash to the URL, like so:

http://widgets.pinterest.com/v1/urls/count.json?url=http%3A%2F%2Flaughingsquid.com

@Keberdle

Google plus do not need KEY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.