Google Analytics Dashboard is an excellent plugin for showing site analytics on your WordPress dashboard.
However, it also has an open API built-in allowing anybody to access statistics from their own code. As a result I’ve created a new of small functions for my own site. To get these to work, ensure you have Google Analytics Dashboard installed, active and you’ve authenticated yourself using the OAuth method!
The most useful is the one I use in the sidebar to display the most popular posts. I was using specific plugins which track visits independently. However, I found these to inconsistent and unreliable.
This isn’t the version I use, as I’ve had to make specific modifications to only display posts and to tidy up the page titles. However, this is the same base code just with my very specific changes removed.
function latest_posts_list( $days, $showposts, $cache_hours ) { $cache_key = 'ga_posts_' . $days . $showposts; $output = get_transient( $cache_key ); if ( !$output ) { $output = ''; if ( $days == 0 ) { $start = '2006-09-01'; } else { $start = date( 'Y-m-d', ( time() - ( 60 * 60 * 24 * $days ) ) ); } $end = date( 'Y-m-d' ); $thispost = 1; $login = new GADWidgetData( get_option( 'gad_auth_token' ), get_option( 'gad_account_id' ) ); $ga = new GALib( 'oauth', NULL, $login -> oauth_token, $login -> oauth_secret, $login -> account_id ); $pages = $ga -> pages_for_date_period( $start, $end ); foreach( $pages as $page ) { $url = $page[ 'value' ]; $title = $page[ 'children' ][ 'value' ]; $output .= '<li><a href="' . $url . '" rel="nofollow">' . $title . '</a>l'; if ( current_user_can( 'edit_posts' ) ) { $output .= ' (' . $page[ 'children' ][ 'children' ][ 'ga:pageviews' ] . ' views)'; } $output .= '</li>'; $thispost ++; if ( $thispost > $showposts ) break; } set_transient( $cache_key, $output, 3600 * $cache_hours ); } echo $output; }
Add this code to functions.php
and then edit the line $start = '2006-09-01';
to reflect the date on which your blog statistics started. Then call latest_posts_list with 3 parameters, all of which are required…
- The number of days across which to gather statistics. If you specify 0 then it will be for all time.
- The number of posts to display in the list.
- The number of hours to cache the results.
A sidebar example, with checks for function availability, would be…
<?php if ( function_exists( 'latest_posts_list' ) ) : ?> <li> <h2>Recent Popular Posts</h2> <ul> <?php latest_posts_list( 30, 5, 24 ); ?> </ul> </li> <?php endif; ?>
This will display the 5 most popular posts within the last 30 days and will update this list every 24 hours.
Two further functions that I use elsewhere add live statistics data to a posts content. The code can also be added to your functions.php
file…
function ga_visits_shortcode( $paras = '', $content = '' ) { extract( shortcode_atts( array( 'days' => '' ), $paras ) ); if ( $days == '') { $days = 30; } $start_date = date( 'Y-m-d', ( time() - ( 60 * 60 * 24 * $days ) ) ); $end_date = date( 'Y-m-d' ); $login = new GADWidgetData( get_option( 'gad_auth_token' ), get_option('gad_account_id') ); $ga = new GALib( 'oauth', NULL, $login -> oauth_token, $login -> oauth_secret, $login -> account_id ); $visit_data = $ga -> total_visits_for_date_period( $start_date, $end_date ); return number_format( $visit_data[ 'value' ] ); } add_shortcode( 'ga_visits', 'ga_visits_shortcode' ); function ga_pageviews_shortcode( $paras = '', $content = '' ) { extract( shortcode_atts( array( 'days' =>'' ), $paras ) ); if ( $days == '') { $days = 30; } $start_date = date( 'Y-m-d', ( time() - ( 60 * 60 * 24 * $days ) ) ); $end_date = date( 'Y-m-d' ); $login = new GADWidgetData( get_option( 'gad_auth_token' ), get_option( 'gad_account_id' ) ); $ga = new GALib( 'oauth', NULL, $login -> oauth_token, $login -> oauth_secret, $login -> account_id ); $visit_data = $ga -> total_pageviews_for_date_period( $start_date, $end_date ); return number_format( $visit_data[ 'value' ] ); } add_shortcode( 'ga_pageviews', 'ga_pageviews_shortcode' );
Now, all you have to do is call either shortcodes – [ga_visits]
or [ga_pageviews]
to output the number of visitors of pageviews that your site has had. Useful for promoting your site. There is one parameter, days
, which allows you specify the time range this is for. If you don’t specify this parameter then 30 is assumed.
Leave a Reply to Chais MeyerCancel reply