Show Your Google Analytics Top Content in WordPress

Published March 25, 2010 by Rob Glazebrook.

Google Analytics Top Content on WordPress

If you’re a typical blog owner, one of your biggest goals is to keep visitors browsing your site. One of the best methods for doing that is to provide links to additional content on your site that your visitors might like. I’ve been doing this for a while with the “Related Articles” section at the bottom of every article (check it out if you haven’t noticed it before).

But many top-tier blogs also showcase their overall most popular posts. These are the articles that often bring in a lion’s share of the site’s traffic, so they must have something going for them, right?

There are a lot of plugins out there that help bloggers rate and publicize the popular content on their blogs. Alex King’s Popularity Contest is a perennial favorite, but it has a lot of overhead, and it often breaks when a new version of WordPress comes out. For that reason, I’ve stopped using it on my site. At one point I’d even written a simple WordPress plugin for my own use that tallied up the number of comments, trackbacks and pingbacks each post had and used that to determine “popularity.”

But let’s say you have a WordPress site and are tracking your stats with Google Analytics. You and I have that in common. If that’s the case, then you already know which pages on your site are the most popular. Google Analytics’ Top Content section makes it a breeze to see what pages on your site get the most traffic (Content -> Top Content, in case you haven’t found it before).

What isn’t as easy is to take that information and display it on your site. But earlier this week I figured out a pretty simple way by piggybacking on an existing popular Google Analytics WordPress Plugin.

a list of my google analytics top content

On the Shoulders

For this hack, we’re going to piggyback on the Google Analytics Dashboard plugin for WordPress. This plugin is pretty cool: once you install it and provide it with your credentials, you can see your Google Analytics stats right on your WordPress dashboard. Neat, huh?

But what the plugin lacks is any way to share this wealth of information with your readers. The plugin does come with a widget, but the widget only currently lets you display two bits of information: your pageviews over the last month, and a graph showing your pageviews over the last month. However, the plugin contains plenty of functions we can use to get at the information we need.

What You’ll Need

To get this to work, you’re going to need some things first:

  • WordPress installed (obviously)
  • Google Analytics installed (yep, that too)
  • The Google Analytics Dashboard plugin installed and operational
  • A willingness to drop a PHP script into your WordPress theme

The PHP Function

Here’s the code that makes it all happen:

<?php
$start = date('Y-m-d', (time() - (60 * 60 * 24 * 30)));
$end = date('Y-m-d');
$showpages = 5;
$thispage = 1;

$login = new GADWidgetData();
$ga = new GALib($login->auth_token, $login->account_id, 60);
$pages = $ga->pages_for_date_period($start, $end);

echo "<ul>";
foreach($pages as $page) {
	$url = $page['value'];
	$title = $page['children']['value'];
	echo '<li><a href="' . $url . '">' . $title . '</a></li>';
	$thispage++;
	if($thispage > $showpages) break;
}
echo "</ul>";
?>

Now I’ll walk you through (in general terms) what we’re doing here.

The first four lines are variables: the start date from when we want to start pulling data (I’m using 30 days before now), the end date (now), the number of posts to display, and a counter we’ll increment later.

The next three lines are hooks into classes provided by the Google Analytics Dashboard plugin. First we’re calling GADWidgetData, because it gives us easy access to the auth_token and account_id stuff we need to log in to Google. Then we use that data to get access to the GALib class, which contains all the functions the dashboard display uses. Then we use those functions to grab a list of the top pages for the duration specified.

The final lines are just outputting that data. In this example I’m putting mine in an unordered list with a link to each article and the article’s title. Once we’ve hit the number of articles we wanted to show, we stop. You could edit this section fairly easily to change the output (for example, using an ordered list might make more sense…)

And that’s it! You now have an easy and reliable way to display your most popular posts (as determined by page views) on your site. You can see it working in my sidebar right now.

67 Responses

  1. plasterers in st albans (reply)

    I feel this is among the such a lot vital information for me.

    And i am happy reading your article. But should statement on some common issues, The web site taste is
    perfect, the articles is in reality great : D. Excellent job, cheers

  2. Leonardo (reply)

    I think this is among the most vital information for me.

    And i am glad reading your article. But should remark on some general things, The web site style
    is great, the articles is really great : D.
    Good job, cheers

  3. Eve (reply)

    Its not my first time to go to see this site, i am visiting this site dailly and obtain pleasant data from here all the
    time.

  4. how to restore faded bumpers (reply)

    Hi I am so thrilled I found your webpage, I really found you by
    mistake, while I was looking on Google for something
    else, Anyways I am here now and would just like to say thank you for a remarkable post
    and a all round exciting blog (I also love the theme/design),
    I don’t have time to read it all at the minute
    but I have bookmarked it and also included your RSS feeds, so
    when I have time I will be back to read a great deal more, Please do keep up the fantastic b.

  5. www.nomsdrs.com (reply)

    I believe that is one of the such a lot important
    information for me. And i’m satisfied studying your article.
    However should commentary on few normal things, The web site style is wonderful,
    the articles is in point of fact nice : D. Excellent process, cheers

  6. Andra (reply)

    This is really interesting, You are a very skilled blogger.

    I have joined your rss feed and look forward to seeking more of your magnificent post.

    Also, I’ve shared your web site in my social networks!

  7. Cards (reply)

    Hey there this is kind of of off topic but I was
    wanting to know if blogs use WYSIWYG editors or if you have to manually
    code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get advice from someone with experience.

    Any help would be enormously appreciated!

  8. minu.me (reply)

    Hello! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  9. torrent download (reply)

    I don’t know whether it’s just me or if everyone else encountering problems with your blog.
    It looks like some of the written text on your
    posts are running off the screen. Can someone else please
    provide feedback and let me know if this is happening to them too?
    This could be a issue with my browser because I’ve had this happen before.
    Kudos

  10. how to advertise your website (reply)

    Hey I am so glad I found your site, I really found
    you by error, while I was browsing on Aol for something else, Anyways I am here now
    and would just like to say kudos for a fantastic post and a
    all round exciting blog (I also love the theme/design), I don’t have time to browse it all at the moment but I
    have book-marked it and also added in your RSS feeds, so when I
    have time I will be back to read more, Please do keep up the awesome
    work.

  11. www.im-web.me (reply)

    Nice weblog here! Additionally your web site lots up
    very fast! What web host are you the usage of? Can I am getting your
    associate hyperlink to your host? I desire my website loaded up as
    quickly as yours lol

  12. aabadetailing.com.au (reply)

    Hello! I know this is kinda off topic however I’d figured
    I’d ask. Would you be interested in exchanging links or maybe guest writing a blog
    post or vice-versa? My site goes over a lot of the same topics
    as yours and I think we could greatly benefit from each other.
    If you are interested feel free to send me an e-mail.
    I look forward to hearing from you! Superb blog by the way!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>