In today’s tutorial we show you one way of displaying a list of the most popular posts in your WordPress template or sidebar. To do this you must first set up your site to use Google Analytics and make sure you’ve placed the code snippet in your theme’s footer.php. Next install the “Google Analytics Dashboard” plugin which can be found here:
http://wordpress.org/extend/plugins/google-analytics-dashboard/
We tested this with version 2.0.3 of the Google Analytics Dashboard wordpress plugin.
<?php // options $showposts = 3; // how many posts/pages to display? $days = 30; // how many days back to calculate? // do not edit below this line $thispost = 1; $start = date('Y-m-d', (time() - (60 * 60 * 24 * $days))); $end = date('Y-m-d'); $login = new GADWidgetData(get_option('gad_auth_token'), get_option('gad_account_id')); $ga = new GALib('client', $login->auth_token, '', '', $login->account_id); $pages = $ga->pages_for_date_period($start, $end); echo "<ul>"; foreach($pages as $page) { $url = $page['value']; $title = $page['children']['value']; // ignore these page titles $ignore = array( '(not set)', 'Page Title 1', 'Page Title 2' ); if(!in_array($title, $ignore)){ // list any strings you would like to remove from the titles $remove = array( " | Company Name", " « Company Name" ); $newtitle = str_replace($remove, "", $title); echo '<li><a href="' . $url . '" rel="nofollow">' . $newtitle . '</a></li>'; $thispost++; } if($thispost > $showposts) break; } echo "</ul>"; ?>


