Categories
Code

Get Social Share Counts of a URL with Google Scripts

Would you like to know how popular is particular web page on social networks? Google Apps Script can help.

You can create a spreadsheet with a list of web page URLs and then use this getSocialCounts() function to retrieve the social sharing count of each of these URLs.

It gets shared counts for Twitter, Facebook, StumbleUpon and LinkedIn.

function testSocialShares() {
  var url = "http://www.labnol.org/";
  getSocialCounts(url);
}

/* Credit: https://gist.github.com/tomcritchlow/904203 */
function getSocialCounts(url) {
  
  var json, response;
  
  var json = UrlFetchApp.fetch("http://graph.facebook.com/"+url);
  var response = Utilities.jsonParse(json.getContentText());
  Logger.log("Facebook Likes :: " + response.likes);
  
  json = UrlFetchApp.fetch("https://graph.facebook.com/fql?q=SELECT%20share_count%20FROM%20link_stat%20WHERE%20url%20=%22" + url + "%22");
  response = Utilities.jsonParse(json.getContentText());
  Logger.log("Facebook Shares :: " + response.data[0].share_count);
  
  json = UrlFetchApp.fetch("http://urls.api.twitter.com/1/urls/count.json?url="+url);
  response = Utilities.jsonParse(json.getContentText());
  Logger.log("Twitter Shares :: " + response.count);
  
  json = UrlFetchApp.fetch("http://www.linkedin.com/countserv/count/share?format=json&url="+url);
  response = Utilities.jsonParse(json.getContentText());
  Logger.log("LinkedIn Shares :: " + response.count);
  
  json = UrlFetchApp.fetch("http://www.stumbleupon.com/services/1.01/badge.getinfo?url="+url);
  response = Utilities.jsonParse(json.getContentText());
  Logger.log("SU :: " + response.result.views);

 
}
Categories
Code

Facebook Blue Background in CSS

Our Facebook Page just touched the magical 150k mark and, to celebrate, I created a little page in HTML & CSS that has the Facebook Like button in the absolute center while the page background is filled with a blue gradient, very similar to the color scheme used in the official Facebook logo.

Facebook Like button on Blue Background
Facebook Like button on Blue Background

Here’s the code for styling the page:


.. and here’s the actual HTML that puts the LIKE button in the center of the blue Facebook page.


  
Categories
Code

Retrieve Facebook Likes Count with PHP

You can get the total number of likes and share count of any web URL with a little bit of PHP and FQL (Facebook Query Language).

You don’t have to fiddle with the Facebook SDK or the API and this trick works for both Facebook pages as well as any external web page that has the like button or has been shared on Facebook.


function facebook_count($url){

    // Query in FQL
    $fql  = "SELECT share_count, like_count, comment_count ";
    $fql .= " FROM link_stat WHERE url = '$url'";

    $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);

    // Facebook Response is in JSON
    $response = file_get_contents($fqlURL);
    return json_decode($response);

}

$fb = facebook_count('https://www.facebook.com/digital.inspiration');

// facebook share count
echo $fb[0]->share_count;

// facebook like count
echo $fb[0]->like_count;

// facebook comment count
echo $fb[0]->comment_count;