Social scientists use social statistics for many purposes, including:

  • the evaluation of the quality of services available to a group or organization,
  • analyzing behaviors of groups of people in their environment and special situations,
  • determining the wants of people through statistical sampling.

Create a Drupal custom block with number of Facebook page likes and total number of twitter followers of your website.

image

To begin with, create a custom module, for more information on how to create custom drupal modules please visit this page.

We will see how to create a block with counters for total Facebook page likes and twitter followers.

The example below shows how to create a social media counter block. In your custom_module place below code.

/**

* Implements hook_block_info().

*/

function custom_module_block_info() {

return array(

'social_stats' => array(

'info' => t('Social Media Counters'),

'cache' => DRUPAL_CACHE_GLOBAL,

'status' => 0,

'weight' => 0,

'visibility' => BLOCK_VISIBILITY_LISTED,

),

);

}

/**

* Implements hook_block_view().

*/

function custom_module_block_view($delta = '') {

$block = array();

global $user;

switch ($delta) {

case 'social_stats':

$block['subject'] = t('Social Media Counters');

$block['content'] = social_status();

break;

} return $block;

}

function social_status()

{ global $user;

$content = '<div class="follows">';

$content .= '<div class="row-1">';

$content .= '<a href="www.twitter.com/yourpage" target="_blank" class="inactiveLink"><div class="twit"><span class="counter">'.variable_get('social_tweet_count','24418').'</span><div class ="counter-text"><br/> followers</div></div></a>';

$content .= '</div>';

$content .= '<div class="row-2">';

$content .= '<a href="www.facebook.com/yourpage" target="_blank" class="inactiveLink"><div class="fb"><span class="counter">'.variable_get(' social_fb_count','3560').'</span><div class ="counter-text"><br/> page likes</div></div></a>';

$content .= '</div>';

$content .= '</div>';

return $content;

}

Write a hook_cron function to update your counters for every cron run.

/**cron hook function */

function uxm_cron() {

get_tweet_count();

get_fbcount();

}

Function to get Facebook page like count.

function get_fbcount(){

$url = "www.graph[dot]facebook.com/yourpage";//Enter your facebook page url here.

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: graph[dot]facebook.com'));

$fbdata = curl_exec($ch);

curl_close($ch);

$obj = json_decode($fbdata);

if($obj)

{

$followers_count=$obj->likes;

}

if($followers_count > 0){

variable_set('social_fb_count',$followers_count);

}

return $followers_count;

}

Function to get Twitter followers count.

function get_tweet_count(){

$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //Enter your twitter token

$token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //enter secret_code

$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Enter twitter consumer key

$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Enter consumer secret

$host = 'api[dot]twitter.com';

$method = 'GET';

$path = '/1.1/statuses/user_timeline.json'; // api call path

$query = array( // query parameters

'screen_name' => 'yourpage', // enter your screen name here.

'count' => '5'

);

$oauth = array(

'oauth_consumer_key' => $consumer_key,

'oauth_token' => $token,

'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended

'oauth_timestamp' => time(),

'oauth_signature_method' => 'HMAC-SHA1',

'oauth_version' => '1.0'

);

$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting

$query = array_map("rawurlencode", $query);

$arr = array_merge($oauth, $query); // combine the values THEN sort

asort($arr); // secondary sort (value)

ksort($arr); // primary sort (key)

// http_build_query automatically encodes, but our parameters

// are already encoded, and must be by this point, so we undo

// the encoding step

$querystring = urldecode(http_build_query($arr, '', '&'));

$url = "www[dot]//$host$path";

// mash everything together for the text to hash

$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);

// same with the key

$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

// generate the hash

$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

// this time we're using a normal GET query, and we're only encoding the query params

// (without the oauth params)

$url .= "?".http_build_query($query);

$url=str_replace("&amp;","&",$url); //Patch by @Frewuill

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!

ksort($oauth); // probably not necessary, but twitter's demo does it

// also not necessary, but twitter's demo does this too

function add_quotes($str) { return '"'.$str.'"'; }

$oauth = array_map("add_quotes", $oauth);

// this is the full value of the Authorization line

$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

// if you're doing post, you need to skip the GET building above

// and instead supply query parameters to CURLOPT_POSTFIELDS

$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),

CURLOPT_HEADER => false,

CURLOPT_URL => $url,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_SSL_VERIFYPEER => false);

// do our business

$feed = curl_init();

curl_setopt_array($feed, $options);

$json = curl_exec($feed);

curl_close($feed);

$twitter_data = json_decode($json);

if($twitter_data[0]->user->followers_count >0){

variable_set('uxm_tweet_count',$twitter_data[0]->user->followers_count);

}

return $twitter_data[0]->user->followers_count;

}

Save your module file. Flush drupal cache. Goto /admin/structure/block there you will find your custom block which you have created above. Place your block in desired page regions.

You can customize and style the counters by adding CSS and icon images to make it look beautiful.

image

Contact us

LET'S DISCUSS YOUR IDEAS. 
WE'D LOVE TO HEAR FROM YOU.

CONTACT US