Subscribe via

How to Hack Google Analyticator Plugin to Display Page Views

Thaya Kareeson

Page View on Advertise PageIf you haven’t noticed, I have an advertise page on this blog that displays various OMNINOGGIN statistics for any interested advertiser to see. If you visit that page, you will also notice a little section under “View Counts” that displays the number of page views on my blog in the last 30 days. This statistics is tracked in my Google Analytics account and I use a hacked version of the Google Analyticator WordPress plugin to display it.

The reason why hacked the plugin to display the number of page views is because advertisers usually care more about the number of page views than the number of unique visitors.

In this tutorial I will show you how to hack the Google Analyticator plugin to display the number of page views instead of the unique visitors and display the count in any one of your posts or pages.

Changes to the Google Analyticator Plugin

Here are the changes I made to the Google Analyticator plugin version 5.3.1. The guys at Spiral Web Consulting can feel free to use it and integrate it into their plugin (with a link back to here I hope).

Index: google-analytics-stats-widget.php
===================================================================
--- google-analytics-stats-widget.php	(revision 151304)
+++ google-analytics-stats-widget.php	(working copy)
@@ -168,7 +168,7 @@
 
 		echo '<td style="width:auto!important;border-width:1px;border-color:#' . $font_color . ';border-style:solid;padding:0px 5px 0px 5px;text-align:right;background:#' . $inner_background_color . ';min-width:80px;*width:80px!important;"><div style="min-width:80px;">'. $visitor_count . '</div></td>';
 
-		echo '<td style="width:auto!important;padding:0px 5px 0px 5px;text-align:center;font-size:11px;">' . $line_one . '<br />' . $line_two . '</td>';
+		echo '<td style="width:auto!important;padding:0px 5px 0px 5px;text-align:center;font-size:11px;">' . $line_one . '</td>';
 
 	}
 
@@ -235,16 +235,16 @@
 		# Get the latest stats
 		$before = date('Y-m-d', strtotime('-' . $time . ' days'));
 		$yesterday = date('Y-m-d', strtotime('-1 day'));
-		$uniques = number_format($stats->getMetric('ga:visitors', $before, $yesterday));
+		$pageviews = number_format($stats->getMetric('ga:pageviews', $before, $yesterday));
 
 		# Make the array for database storage
-		$visit = serialize(array('unique'=>$uniques, 'lastcalled'=>time()));
+		$visit = serialize(array('unique'=>$pageviews, 'lastcalled'=>time()));
 
 		# Store the array
 		update_option('google_stats_visits_' . $account, $visit);
 
 		# Return the visits
-		return $uniques;
+		return $pageviews;
 	}
 
 }// END class

Displaying in Your Posts and Pages

Google Analyticator plugin be default only allows you to display this information in your sidebar widget. You will have to do some more hackery in your theme’s functions.php to be able to display this information in your posts and pages.

1. Modify functions.php

Here is the code I added to my functions.php.

function display_ga_stats($content) {
  $stats_pos = strpos($content, '##google_analyticator_statistics##');
  if($stats_pos) {
    ob_start();
    $google_stat_options = array('title'=>'', 'account'=>'ga:7875704', 'timeFrame'=>'30', 'pageBg'=>'f6f6f6', 'widgetBg'=>'999', 'innerBg'=
>'fff', 'font'=>'333', 'line1'=>'Views in the last 30 days', 'line2'=>'');
    the_widget('GoogleStatsWidget', $google_stat_options);
    $ga_stats = ob_get_contents();
    ob_end_clean();
    $content = str_replace('##google_analyticator_statistics##', $ga_stats, $content);
  }
  return $content;
}
add_filter('the_content','display_ga_stats');

2. Insert ##google_analyticator_statistics## into your posts

Now all you have to do to display this information on your posts and pages is to include the text “##google_analyticator_statistics##” anywhere you want to display in your posts and/or pages. Enjoy!

Save and Share
StumbleUpon
Reddit

Currently experiencing problems with Intense Debate commenting system. Please make sure your browser's Javascript is enabled and try again later.