Skip to content

Instantly share code, notes, and snippets.

@jice-lavocat
Created September 10, 2015 08:56
Show Gist options
  • Save jice-lavocat/717ebc00283d4552b4b1 to your computer and use it in GitHub Desktop.
Save jice-lavocat/717ebc00283d4552b4b1 to your computer and use it in GitHub Desktop.
Getting Fb, Twitter and G+ count with JS (in 2015)
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<script src="https://apis.google.com/js/client:plusone.js" type="text/javascript"></script>
<script src="social_count.js"></script>
<h1>Social count</h1>
<script type="text/javascript">
$(document).ready(function($){
var url = window.location.href;
var iddivTwit="#twitter<?php echo $randid; ?>";
var iddivFb="#facebook<?php echo $randid; ?>";
var iddivGp="#plusones<?php echo $randid; ?>";
getfbcount(url,iddivFb);
gettwcount(url,iddivTwit);
getgpcount(url,iddivGp);
});
</script>
function getfbcount(url,iddivFb){
var fblikes;
fql = "SELECT url, normalized_url, share_count, like_count, comment_count, total_count, commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url = '"+url+"'";
apifql="https://api.facebook.com/method/fql.query?format=json&query="+encodeURIComponent(fql);
fblikes = 0;
$(iddivFb).text(fblikes);
$.getJSON(apifql, function(data){
fblikes = data[0].total_count;
$(iddivFb).text(fblikes);
});
}
function gettwcount(url,iddiv){
var tweets;
tweets=0;
$(iddiv).text(tweets);
$.getJSON('http://urls.api.twitter.com/1/urls/count.json?url='+url+"&callback=?", function(data){
tweets=data.count;
$(iddiv).text(tweets);
});
}
function getgpcount(url,iddivGp){
var params = {
nolog: true,
id: url,
source: "widget",
userId: "@viewer",
groupId: "@self"
};
gapi.load('client', function() {
console.log('gapi.client loaded.');
gapi.client.setApiKey('AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ');
gapi.client.rpcRequest('pos.plusones.get', 'v1', params).execute(function(resp) {
console.log('count:', resp.result.metadata.globalCounts.count);
$(iddivGp).text(resp.result.metadata.globalCounts.count);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment