Tracking Twitter Follower Counts in Auditor

A little friendly competition in the office to see who has the most followers requires some tracking, and what better than to use the Auditor. So I started work on using the helper functions, and some code I had to grab from the twitter APIs.

To follow this you’ll need the Auditor at v1.6.2 and the logger plugin activated. Using a helper class in the latest version I created a routine that ran daily, and logged the number of followers a twitter user had. ICIT_Log_Cron_Task lets me quickly create a routine to regularly log things and pass in data.

Here’s the most basic example of a regular logging task:

class TJN_Log_Stuff extends ICIT_Log_Cron_Task {
	public function cron_task() {
		// log things
	}
}

This is how you would use it:

// grab the main logger container, and use it to create our logger cron task
$auditlogger = ICITAuditLogger::get_logger();
$custom_cron_task = new TJN_Log_Stuff(
	$auditlogger->primarylog, // log to the primary default log
	'daily', // once a day
	'log_stuff' // using this unique ID to generate the cron hook
);

Here’s the full twitter follower cron task:

// ICIT_Log_Cron_Task can be used to log things at regular intervals, simply extend it and override the cron_task method with your own logging code
class TJN_Log_Twitter extends ICIT_Log_Cron_Task {
	public function cron_task() {
		// if we have data and it isnt empty
		if ( is_array( $this->data ) && !empty( $this->data ) ) {
			// our data here is an array of account names, so for each account
			foreach ( $this->data as $account ) {
				// ask twitter for general account information
				$twitter_followers = wp_remote_get( 'http://api.twitter.com/1/statuses/user_timeline.xml?count=2&screen_name='.$account );
				if ( ! is_wp_error( $twitter_followers ) ) { // if it's not an error then
					// break apart the XML response
					$xml = new SimpleXmlElement( $twitter_followers['body'], LIBXML_NOCDATA );
					// extract the follower count field
					$count = $xml->status->user->followers_count;
					$count = intval( $count ); // ensure it's treated as a number not a string else it'll be saved as a string

					// the helper object stores a reference to the logging object provided to it
					// Here we pass in null for the user ID and time, it'll auto-figure those out
					// we'll also pass in the twitter account as the log entry subject
					$this->log->add( 'twitter_count', $count, null, null, $account ); 
				}
			}
		}
	}
}
// grab the main logger container, and use it to create our logger cron task
$auditlogger = ICITAuditLogger::get_logger();
$twitter_cron = new TJN_Log_Twitter(
	$auditlogger->primarylog, // log to the primary default log
	'daily', // once a day
	'log_twitter_follower_count', // using this unique ID to generate the cron hook
	array( 'tarendai', 'sanchothefat', 'lauriechandler', 'davecoveney', 'anthony_casey' ) // and passing this data for use later on
);

Once I plugged in the twitter accounts, and activated the plugin it went off logging, but I needed a way to view the data. The Auditor already gives us an overview page, filled with charts, so I added my twitter follower chart there using the helper APIs:

// Lets put the follower counts on the overview page in a chart!
add_action( 'icit_audit_log_add_overview_widgets', 'twitter_follower_chart', 100 );

// $overview_id is the ID of the overview page. Ofcourse you could use the admin_init hook
// and put the chart on your admin dashboard instead
function twitter_follower_chart( $overview_id ) {
	// we need to pass in a list of lines to show on our chart. Each account needs its own data line
	$lines = array();

	// here's our list of twitter users again
	$accounts = array( 'tarendai', 'sanchothefat', 'lauriechandler', 'davecoveney', 'anthony_casey' );

	// so for each account
	foreach ( $accounts  as $account ) {
		// add to the list of lines an array of parameters
		// all the parameters for ICIT_Log_Query are supported and passed through, the rest are graph specific
		$lines[] = array(
			'type'   => 'twitter_count', // log event type
			'subject' => $account, // the subject, which we're storing our twitter ID in
			'legend_label' => $account, // this appears in the legend box
			'name_plural' => $account, // for future use
			'name'   => $account, // used to identify the line in JS behind the scenes
			'colour_label' => $account, // the colour of the line will be generated based on this
			'continuous' => true, // if this is false, days that dont have a follower count will be set to zero, and you'll get a spikey graph
			'collect'  => false // if we dont set this to false, we'll be told how many times the log entry occurred, not value of the log entry
			);
	}
	// create our chart widget
	$widget = new ICIT_Audit_Overview_Multiple_Line_Chart_Widget( 'overview_twitter', 'Twitter Followers', 'side' );

	// tell it what lines it's going to display
	$widget->setDataArgs( $lines );

	// add it to the overview page
	$widget->add_to( $overview_id, 'side' );
}

Giving us a chart with lines tracking each twitter user.

With some modifications we can put this on the dashboard too:

  • Change the action hook from icit_audit_log_add_overview_widgets to wp_dashboard_setup
  • Tabs currently only work on the Auditor pages, so add $widget->show_tabs = false; to the function
  • Change the final line to $widget->add_to( 'dashboard', 'normal' );

Giving us a dashboard widget that looks like this:

Screen Shot 2013-05-10 at 12.01.00

As you can see twitter follower counts are normally a longterm data source, you don’t immediately see a trend emerging, but there are a lot of other things that can be logged, ideas include:

  • Google Analytics Daily Traffic figures
  • Facebook friend counts
  • Total Likes & retweets
  • Total number of users
  • Up time
  • etc..

Here’s a full code sample that you can install as a plugin and modify:

https://gist.github.com/Tarendai/5510127

css.php