/** * The Google Analytics Stats Widget * * Now using widget API available in WordPress 2.8 * @author Spiral Web Consulting **/ class GoogleStatsWidget extends WP_Widget { function GoogleStatsWidget($shortcode = FALSE) { $widget_ops = array('classname' => 'widget_google_stats', 'description' => __("Displays Stat Info From Google Analytics", 'google-analyticator') ); $control_ops = array('width' => 400, 'height' => 400); $this->WP_Widget('googlestats', __('Google Analytics Stats', 'google-analyticator'), $widget_ops, $control_ops); if ($shortcode) { $this->widget(); } } function widget($args = array(), $instance = array()) { extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']); $acnt = false; $timeFrame = empty($instance['timeFrame']) ? '1' : $instance['timeFrame']; $pageBg = empty($instance['pageBg']) ? 'fff' : $instance['pageBg']; $widgetBg = empty($instance['widgetBg']) ? '999' : $instance['widgetBg']; $innerBg = empty($instance['innerBg']) ? 'fff' : $instance['innerBg']; $font = empty($instance['font']) ? '333' : $instance['font']; $line1 = empty($instance['line1']) ? 'Unique' : $instance['line1']; $line2 = empty($instance['line2']) ? 'Visitors' : $instance['line2']; # Before the widget if (isset($before_widget)) { echo $before_widget; } # The title if ( $title ) echo $before_title . $title . $after_title; # Make the stats chicklet echo ''; $this->initiateBackground($pageBg, $font); $this->beginWidget($font, $widgetBg); $this->widgetInfo($this->getUniqueVisitors($acnt, $timeFrame), $line1, $line2, $innerBg, $font); $this->endWidget(); # After the widget if (isset($after_widget)) { echo $after_widget; } } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags(stripslashes($new_instance['title'])); $instance['account'] = strip_tags(stripslashes($new_instance['account'])); $instance['timeFrame'] = strip_tags(stripslashes($new_instance['timeFrame'])); $instance['pageBg'] = strip_tags(stripslashes($new_instance['pageBg'])); $instance['widgetBg'] = strip_tags(stripslashes($new_instance['widgetBg'])); $instance['innerBg'] = strip_tags(stripslashes($new_instance['innerBg'])); $instance['font'] = strip_tags(stripslashes($new_instance['font'])); $instance['line1'] = strip_tags(stripslashes($new_instance['line1'])); $instance['line2'] = strip_tags(stripslashes($new_instance['line2'])); return $instance; } function form($instance) { //Defaults $instance = wp_parse_args( (array) $instance, array('title'=>'', 'account'=>'', 'timeFrame'=>'1', 'pageBg'=>'fff', 'widgetBg'=>'999', 'innerBg'=>'fff', 'font'=>'333', 'line1'=>'Unique', 'line2'=>'Visitors') ); $title = htmlspecialchars($instance['title']); $acnt = htmlspecialchars($instance['account']); $timeFrame = htmlspecialchars($instance['timeFrame']); $pageBg = htmlspecialchars($instance['pageBg']); $widgetBg = htmlspecialchars($instance['widgetBg']); $innerBg = htmlspecialchars($instance['innerBg']); $font = htmlspecialchars($instance['font']); $line1 = htmlspecialchars($instance['line1']); $line2 = htmlspecialchars($instance['line2']); $accounts = array(); # Get the current memory limit $current_mem_limit = substr(ini_get('memory_limit'), 0, -1); # Check if this limit is less than 96M, if so, increase it if ( $current_mem_limit < 96 || $current_mem_limit == '' ) { if ( function_exists('memory_get_usage') ) @ini_set('memory_limit', '96M'); } # Get the class for interacting with the Google Analytics require_once('class.analytics.stats.php'); # Create a new Gdata call $stats = new GoogleAnalyticsStats(); # Check if Google sucessfully logged in $login = $stats->checkLogin(); if( !$login ) return false; # Get a list of accounts //$accounts = $stats->getAnalyticsAccounts(); $accounts = $stats->getSingleProfile(); # Output the options echo '

'; # Time frame echo '

'; # Page background echo '

'; # Widget background echo '

'; # Inner background echo '

'; # Font color echo '

'; # Text line 1 echo '

'; # Text line 2 echo '

'; } /** * This function is used to display the background color behind the widget. This is necessary * for the Google Analytics text to have the same background color as the page. * * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well. * @param $page_background_color - Hexadecimal value for the page background color * @return void **/ function initiateBackground($page_background_color = 'FFF', $font_color = '000') { echo '
'; echo '
'; } /** * This function starts the widget. The font color and widget background color are customizable. * * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well. * @param $widget_background_color - Hexadecimal value for the widget background color. * @return void **/ function beginWidget($font_color = '000', $widget_background_color = 'FFF') { echo ''; } /** * This function encases the text that appears on the right hand side of the widget. * Both lines of text are customizable by each individual user. * * It also displays the visitor count that was pulled from the user's Google Analytics account. * * @param $visitor_count - Number of unique visits to the site pulled from the user's Google Analytics account. * @param $line_one - First line of text displayed on the right hand side of the widget. * @param $line_two - Second line of text displayed on the right hand side of the widget. * @param $inner_background_color - Hexadecimal value for the background color that surrounds the Visitor Count. * @param $font_color - Hexadecimal value for the font color used within the Widget (does not effect "Powered By Google Analytics Text"). This effects border color as well * @return void **/ function widgetInfo($visitor_count, $line_one = 'Unique', $line_two = 'Visitors', $inner_background_color = 'FFF', $font_color = '000') { echo ''; echo ''; } /** * The function is used strictly for visual appearance. It also displays the Google Analytics text. * * @return void **/ function endWidget() { // This closes off the widget. echo '
'. $visitor_count . '
' . $line_one . '
' . $line_two . '
'; // The following is used to displayed the "Powered By Google Anayltics" text. if (get_option(key_ga_show_ad) == '1') { echo '
Powered By Google Analytics
'; } } /** * Grabs the cached value of the unique visits for the previous day * * @param account - the account to get the unique visitors from * @param time - the amount of days to get * @return void **/ function getUniqueVisitors($account, $time = 1) { // IF we have a cached version, return that, if not, continue on. if ( get_transient( 'google_stats_uniques' ) ) return get_transient( 'google_stats_uniques' ); # Get the class for interacting with the Google Analytics require_once('class.analytics.stats.php'); # Create a new Gdata call $stats = new GoogleAnalyticsStats(); # Check if Google sucessfully logged in if ( ! $stats->checkLogin() ) return false; $account = $stats->getSingleProfile(); $account = $account[0]['id']; # Set the account to the one requested $stats->setAccount($account); # Get the latest stats $before = date('Y-m-d', strtotime('-' . $time . ' days')); $yesterday = date('Y-m-d', strtotime('-1 day')); try{ $result = $stats->getMetrics('ga:visitors', $before, $yesterday); } catch(Exception $e){ print 'GA Widget - there was a service error ' . $e->getCode() . ':' . $e->getMessage(); } $uniques = number_format($result->totalsForAllResults['ga:visitors']); # Store the array set_transient( 'google_stats_uniques', $uniques, 60*60*12 ); # Return the visits return $uniques; } }// END class /** * Register Google Analytics Stat Widget. */ function GoogleStatsWidget_init() { register_widget('GoogleStatsWidget'); } add_action('widgets_init', 'GoogleStatsWidget_init');