Skip to content

Instantly share code, notes, and snippets.

@keithcollins
Last active December 17, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithcollins/5572850 to your computer and use it in GitHub Desktop.
Save keithcollins/5572850 to your computer and use it in GitHub Desktop.
Twitter and Facebook stats via AJAX/jQuery
# the permalink var is an array created before this. it takes the permalink from each WordPress post as they load.
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
$(document).ready(function () {
var fb_likes = 0,
tweets = 0,
a = 1,
b = 1;
links.push("http://projectwordsworth.com");
$.each(links, function(index, permalink) {
fburl = 'https://api.facebook.com/method/fql.query?format=json&query=select%20%20total_count%20from%20link_stat%20where%20url=%22' + permalink + '%22'
turl = 'http://urls.api.twitter.com/1/urls/count.json?callback=?&url=' + permalink + '';
$.getJSON(fburl, function(data) {
fb_likes += Math.abs(data[0].total_count);
if (a == 17 && fb_likes > 0) {
//update fb count text
$('#fb-count').text(commaSeparateNumber(fb_likes));
}
a++;
});
$.getJSON(turl, function(data) {
tweets += Math.abs(data.count);
if (b == 17 && tweets > 0) {
//update twitter count text
$('#tweet-count').text(commaSeparateNumber(tweets));
}
b++;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment