public
Last active

Get the share counts from various APIs

  • Download Gist
gistfile1.md
Markdown

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
}

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!

Insanely useful - thanks!

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!

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/

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.

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.

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

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

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.

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.]+) /

where i can get API_KEY?

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?

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

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"
}
}

@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

this is so good. i thank you.

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.