Gather Post Stats from Twitter and Facebook
by c.bavota | Posted in Tutorials | 19 comments
If you’ve included any social buttons on your site, such as the Facebook like button, you can usually see how many people have clicked on it. It’s all done through JavaScript but you can also retrieve that number using PHP. Facebook has the Graph API and Twitter offers a similar API.
Let’s do a quick test to see how each API works in regards to gathering your page statistics.
http://graph.facebook.com/http://bavotasan.com/2011/style-select-box-using-only-css/
The above returns:
{
"id": "http://bavotasan.com/2011/style-select-box-using-only-css/",
"shares": 69
}
If you are using Facebook comments, you will also see a comments variable in the returned JSON code.
Twitter is quite similar:
http://urls.api.twitter.com/1/urls/count.json?url=http://bavotasan.com/2011/style-select-box-using-only-css/
That will return:
{
"count":47,
"url":"http:\/\/bavotasan.com\/2011\/style-select-box-using-only-css\/"
}
Here’s a small function you can add into your WordPress functions.php file so that your page stats are gathered and stored.
add_action( 'wp', 'bavotasan_gather_post_stats' );
/**
* Gather and store post stats
*
* @uses wp_remote_fopen() To fetch remote URL
*
* @author c.bavota
*/
function bavotasan_gather_post_stats() {
if ( is_single() ) {
$post_id = get_the_ID();
$transientkey = 'post_stats_' . (int) $post_id;
if ( false === get_transient( $transientkey ) ) {
$post_url = get_permalink();
$stat = array();
$twitter_url = 'http://urls.api.twitter.com/1/urls/count.json?url=' . $post_url;
$twitter_stat = json_decode( wp_remote_fopen( $twitter_url ) );
$stat['twitter'] = empty( $twitter_stat->count ) ? '0' : (int) $twitter_stat->count;
update_post_meta( $post_id, '_twitter_count', $stat['twitter']);
$facebook_url = 'http://graph.facebook.com/' . $post_url;
$facebook_stat = json_decode( wp_remote_fopen( $facebook_url ) );
$stat['facebook'] = empty( $facebook_stat->shares ) ? '0' : (int) $facebook_stat->shares;
update_post_meta( $post_id, '_facebook_shares', $stat['facebook']);
set_transient( $transientkey, $stat, 60 * 60 * 1 ); // one hour
}
}
}
When someone visits your page, the function will run and store a transient containing your page stats. The transient will be refreshed every hour, as long as someone revisits your page. It will also create two custom fields that contain your Twitter count and Facebook shares. With those two values stored, you can easily create a list of your top posts according to the number of shares or Tweets.



thansk so much ! can i copy ?
That’s a good idea!! Can auto post in blogspot?? WordPress can???
Why my count is 0
http://urls.api.twitter.com/1/urls/count.json?url=http://topiclaw.com
Help me!! THanks
Your count is zero because no one has tweeted that exact URL.
wow cool snippets
nice work buddy. thanks
We created this just last week to rate the designs listed on our website. The number of tweets, likes,Google+es, Pins .. all are taking together and give the item a social ranking. The function will be live by the end of the month. Stats are reset each month. For us its a week to late but thanks for sharing! A lot of people can benefit from it.
Perfect, Thanks a lot, I’ve been searching for this for hours now, messing around with the Facebook API. Would be great to get the Google+ stats that way, too.
Function from Webdesign_nl sounds interesting! Great way to rate our rental companies.
Awesome stuff once again – do you mind if people copy this code?
Go right ahead. That’s the whole point of sharing it.
Thank you for sharing, im also gonna use this.
Thanks for sharing! I’m not a big fan of the API the social networks themself provide. Sharedcount.com has a better API, they bundle all networks together. http://api.sharedcount.com/?url=http%3A%2F%2Fbavotasan.com
I was debating to switch my facebook like button the the one with all the statistic displays but I could not get it too look good on my website. Now I can track stats without changing my look. thank you
The theme looks gorgeous! Thanks for sharing. I’m an upcoming webdeveloper and I enjoy your posts but I still have a lot to learn!
This function is great. But we do not only have facebook and twitter.If you can integrate other social networks as well as google plus, MySpace… It would be better if can develop this into a module that here we can easily customize social networks such as add, remove, configure…
Wow excellent, didn’t know this was possible at all. I did a quick search for a Google +1 json api url but could not find it. Any ideas?
However, it is possible to get the LinkedIn shares at this url: http://www.linkedin.com/countserv/count/share?url=
Hope it helps and thanks for sharing.
Is this script free to use for my website?
Yup. Go right ahead.