Show Your Google Analytics Top Content in WordPress

Published March 25, 2010 by CSS Newbies.

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:

$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>';
	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.

101 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

  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. (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. (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.

  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

  11. (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. (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!

  13. Monaco business pages (reply)

    Here are some simple ways to make the most of your Facebook page, but, of course, all these
    strategies have time to implement and grow in popularity.
    These could include monitoring payments received, tracking
    affiliate performance, doing product inventory,
    creating sales reports and basically, making
    sure that the entire business is running smoothly.
    It was not at all uncommon for a person live out their entire lives in the same town they were born in, just like their ancestors did, because travel was costly, difficult and sometimes

  14. parasite cleanse (reply)

    Do you have a spam issue on this site; I also am a blogger, and I was
    curious about your situation; we have created some nice procedures
    and we are looking to trade solutions with other folks,
    please shoot me an e-mail if interested.

  15. Terrell (reply)

    I’m truly enjoying the design and layout of your
    website. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme?
    Outstanding work!

  16. uk removals (reply)

    Pretty nice post. I simply stumbled upon your weblog and wished to
    mention that I have truly enjoyed browsing your weblog posts.
    After all I will be subscribing in your rss feed and I hope you
    write again soon!

  17. (reply)

    Very great post. I simply stumbled upon your weblog and wanted to mention that I’ve really enjoyed
    browsing your blog posts. In any case I will be subscribing for your
    rss feed and I am hoping you write again very soon!

  18. get paid to answer questions online (reply)

    When you find an income opportunity review website that you
    feel that you can trust, that is half the battle.

    It really doesn’t cost you to get started, unless you need a little help.
    You can purchase them in large quantities from various flea
    markets, thrift stores and garage sales.

  19. landlord insurance quote (reply)

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your
    point. You definitely know what youre talking about, why waste your intelligence
    on just posting videos to your weblog when you could be giving us something enlightening to read?

  20. hypnotize (reply)

    Thank you, I have recently been searching for information approximately this subject for a while and yours is the best I have came upon so far.

    But, what about the conclusion? Are you positive
    about the source?

  21. Comment Pirater un compte Gmail (reply)

    Hi there to all, because I am truly keen of
    reading this blog’s post to be updated regularly.
    It contains nice material.

  22. Cómo ganar dinero (reply)

    Hey There. I discovered your blog the use of msn. This
    is a really neatly written article. I’ll be sure to bookmark
    it and come back to learn extra of your useful
    info. Thank you for the post. I’ll certainly comeback.

  23. letsplay (reply)

    Hey excellent website! Does running a blog similar to this require a massive amount work?
    I’ve virtually no knowledge of coding but I was hoping to start my own blog in the near future.
    Anyhow, if you have any ideas or tips for new blog
    owners please share. I understand this is off topic nevertheless I just had to ask.

  24. aksesoris samsung galaxy tab 3 7.0 (reply)

    I’m not sure why but this web site is loading extremely slow for me.
    Is anyone else having this problem or is it
    a issue on my end? I’ll check back later and see if the problem still

  25. ​​SubwaySurferPc.Com (reply)

    I’m truly enjoying the design and layout of your
    blog. It’s a very easy on the eyes which makes it much more enjoyable for me
    to come here and visit more often. Did you hire out a designer to create your theme?
    Outstanding work!

  26. SubwaySurferPc (reply)

    Hi mates, its impressive piece of writing concerning cultureand completely explained, keep it up
    all the time.

  27. Job search japan (reply)

    It’ll be worthwhile for the prospect to find the accreditation. Prima di valutare le
    immagini valutate laffinit che avete con il fotografo.
    Author JK Rowling has teamed up with Sony to launch a direct outlet, called Potter – More.

  28. Jerrold (reply)

    Just desire to say your article is as amazing.
    The clarity on your post is simply spectacular and that i can think you’re a professional in this
    subject. Well together with your permission let me to clutch your feed to keep
    up to date with imminent post. Thank you a million and please carry on the enjoyable work.

  29. swiftcover (reply)

    Usually I do not learn post on blogs, however I wish to say that this write-up
    very forced me to try and do so! Your writing style has
    been surprised me. Thank you, very nice article.

  30. (reply)

    Thanks , I have recently been searching for info about
    this subject for a while and yours is the best I’ve discovered till now.

    However, what about the bottom line? Are you
    positive in regards to the supply?

  31. B2B email lists (reply)

    Thaat is very attention-grabbing, You’re a very professional blogger.
    I’ve joined your rss feed and look ahead to in the hunt for more of your excellent post.
    Additionally, I’ve shared your website in my social networks

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>