David Artiss

Using Google Analytics data to show popular posts


Due to updates, over time, that have been made to the site and the age of this article, this post may not display correctly. In particular images may be missing or product reviews display incorrectly.

If this is the case and you'd particularly like me to fix it, then please reach out to me on Twitter.

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.

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…

  1. The number of days across which to gather statistics. If you specify 0 then it will be for all time.
  2. The number of posts to display in the list.
  3. The number of hours to cache the results.

A sidebar example, with checks for function availability, would be…

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…

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.


Samsung Galaxy S2 » « Streamline Hosting

14 Comments

  1. This looks awesome but I’m getting an error message trying to make the last two function items (shortcodes) work.
    Here’s the error I’m getting: Parse error: syntax error, unexpected ‘=’, expecting ‘)’

    This error is happening on this line of code within both of the last two functions: extract( shortcode_atts( array( ‘days’ = > ” ), $paras ) )
    I would love some assistance on this if you don’t mind.

    thank you in advance!
    Chais

  2. You got it – that was the issue!
    Thank you so much. We’ll be throwing a donation your way soon, much appreciated!
    Chais

  3. Good afternoon, all done as described, but an error: “Warning: Invalid argument supplied for foreach () in”. How can I fix this? Thank you.

  4. Thank you. This works well! One question. How do I remove the index page from the list of posts? I’ve increased the post display count to 10. The index page appears twice.

    • The way I got around this was to add a line to only extract if the format of the URL was consistent with one of my posts (this also eliminated pages appearing in the results).

      It depends on your use of permalinks, but I added the following IF statement wrapper around the results…

      if ( substr( $url, 0, 3 ) == '/20' ) {

  5. Great code. Thanks. Any chance of posting (or emailing) the modifications you’ve made? I’m not a programmer. I don’t know how to remove the blog and category names from the code so they don’t appear in the post results.
    Thank you!

    • I have given details above on how to do this. The problem is that you have to extract what you need based on your own blog’s configuration – the details I’ve given will work on my site but won’t on many others. Google Analytics returns the URL and, assuming there’s an easy way of distinguishing post URLs from others then it can be done.

Talk to me!

Copyright © 2017 David Artiss

Theme by Anders NorenUp ↑

Scroll Up
%d bloggers like this: