Plugin: Stats Helper Functions And Widgets

What it does? It helps you retrieve data from wordpress.com stats and put it on your blog.

Who’s using it?

Download:

http://wordpress.org/extend/plugins/wordpresscom-stats-helper/

If you’re using Jetpack, please have a look at this workaround/hack until a new version of the plugin is available.

So far it provides helper functions and widgets to display:

  • Most Visited Posts – lists the most visited posts along with a link to the post and the views count
    • (New, Recommended) Function usage – put it into your theme template:
      if (function_exists('wpcomstats_most_visited')) {
      wpcomstats_most_visited($options);
      }

      Where:
      $options – An array which can contain the following key – value pairs:

      • posts, before, after, show_count, days – Same as before, see bellow
      • show – What type of items to display; one of ‘posts’, ‘pages’ or ‘both’
    • (Old) Function usage – put it into your theme template:
      if (function_exists('wpcomstats_most_visited_posts')) {
      wpcomstats_most_visited_posts($posts, $before,
      $after, $show_count, $days);
      }

      Where:
      $posts – Number of posts to be fetched
      $before, $after – HTML/text to put before and after each post
      $show_count – Set to false if you don’t want to display post views count
      $days – Number of days for which the stats to be calculated. Valid values: 0 (all), 1 (today), 7 (this week), 30 (this month – I know, I’m lazy but gotta KISS)

    • Widget – just drag and drop it on a sidebar.
      You can configure it’s title, the number of posts that you want to list, if to show views count for each post, for what time frame to calculate stats and if you want to restrict the lists to posts or pages only.
  • Blog Visits – shows a counter of blog visits since stats where enabled
    • Function – put in theme template:
      if (function_exists('wpcomstats_visits')) {
      wpcomstats_visits($before, $after, $post_id, $days);
      }

      Where:
      $before, $after – HTML/text to put before and after views count
      $post_id – Post/page id for which the stats must be calculated (use null for all blog stats)
      $days – Number of days for which the stats to be calculated. Valid values: 0 (all), 1 (today), 7 (this week), 30 (this month)

    • Widget – drag&drop to sidebar.
      Title is configurable, so is the timeframe for which the stats is to be calculated. The widget automagically detects single post/page contexts and shows stats for that post/page only.

Note:

  • The plugin assumes you have installed the wordpress.com stats plugin and configured it properly. If not, it will display some error messages. Pretty self descriptive.
  • The plugin caches the data from wordpress.com stats so it won’t put too much load on their system. This means visits to posts will probably not be updated in real time :)

More to come… soon! Like it, hate it?

Changes:

0.5

  • Added possibility to filter most visited items by type (post/page)
  • Added a new function for most visited items, with more flexible arguments passing
  • Safer fetching from stats.wordpress.com (try… catch)

0.4

  • Added possibility to restrict the timeframe for stats
  • Added possibility to get views count for a specific post/page (by id)

0.3:

  • Added a function and a widget for blog visits.

0.2:

  • Implemented a custom, option-based, cache for stats fetch data (on Andy‘s suggestion)
  • Capped the maximum post count to 25 (you won’t be using this to spam, right?)
  • Added a configurable option for views count display

114 thoughts on “Plugin: Stats Helper Functions And Widgets

  1. I was suggesting to check what stats.wordpress.com is reporting directly (using the given link). Then we know if it’s a problem with the plugin or the stats service.

  2. OK.

    I will try to do that.

    Stats is workign as I can see numbers on the dashboard. The only place i cannnot see them is the plugin.

  3. Hi Vlad
    Thanks for the plugin – question: Is it possiblt to edit ‘blog visits’ field so that only blog visits after aug 31 2008 are counted?
    thanks

  4. Hi Vlad, your plug in was just what I was looking for. But I can’t get it to only show posts. If I set the widget that way, nothing is shown except the widget title. I’ve tried to change around some code as you’ve written earlier in this thread, but no success. Any ideas?

  5. If you’ll forgive my presumption to modify your code, I made some improvements after realising your plugin wasn’t working on my WP 2.6.2 site when selecting to show posts only. The plugin was just displaying no results at all, because it was using an empty string for the ‘include’ part of the post query, instead of ‘any’, which it should’ve been.

    I also made some improvements to the interface, adding defaults, replacing a drop-down used for a boolean value with a checkbox, and adding the option to truncate titles or not, and the option to display or not display the link to the plugin.

    I’ve temporarily uploaded a zip file containing the new version as well as a patch showing the changes to http://michael.tyson.id.au/wpcomstats-helper-0.5.5.zip . Tell me when you’ve downloaded it and I shall remove it.

    Let me know what you think.

  6. Thanks Michael!

    I downloaded the file and I’ll have a look at the code as soon as possible.

    Any contributions to the plugin are always welcome!

  7. Any word on a fix for WordPress version 2.6.2? Ever since the upgrade my most viewed posts have stopped reporting – it just shows nothing.

    I’ve upgraded my code to the newest recommended array format but no change.

  8. You are using the latest version of the plugin, right? Please also try a disable/enable cycle for the plugin (clears the cache)!

  9. same here.
    Ever since i updated the plugin i have not got it to work.
    Disable/Enable were done but no help.
    The code to call the script was changed to the array one.
    No list at the moment.

  10. Yuval, your problem might be solved if you make sure your $options['show'] has one of the ‘post’, ‘page’ or ‘both’ values set. There’s a missing default there that’s been causing people a headache. Let me know how this works out!

  11. I switched from wordpress.com to hosting through bluehost. I downloaded your plugin but it only gives me stats after I switched to the new hosting service. Is there a way to combine both stats? I used the same API code but the WP.com stats table gives me a choice to select between these two sites but not one that combines all stats.
    Thanks.

  12. Stats are recorded separately for different URLs. Since I supposed you’ve changed the URL while moving from wp.com to your own domain and hosting, there’s nothing you can do about it (unless you can convince the wp.com stats team to move your old stats to your new URL).

  13. Vlad, I’m following your installation, but I can’t seem to get blog visits working… most visited works just fine.
    I’ve installed the wordpress stats. Any ideas? Thanks

  14. It’s not working as in: it doesn’t show anything or it shows wrong data/error? Can you send me a link to your site so I can check out the behaviour?

  15. Hey there, I’ve the same problem like IndoS. It shows: “There’s no data yet.”

    The wordpres.com/stats plugin is up and running for over 2 months now and there are some visits.

    At the moment I’ve got 2 blogs running on 1 API. Mayby that is the problem??

  16. I’ve installed the plugin and added the “most visited post” widget.

    on my page you can see “Top Post”, but no post or links are showing up.

    Do I need to add this code and if so, where exactly does this code need to go?

    if (function_exists(‘wpcomstats_most_visited’)) {
    wpcomstats_most_visited($options);
    }

  17. I have installed wordpress stats plugin and wordpress stats helper plugin. Both of them are installed properly and I can see stats my dashboard. Now I want display most visited post in tab . So I am using this following code
    wpcomstats_most_visited($options = array (
    ‘posts’ => 10,
    ‘before’ => ”,
    ‘after’ => ”,
    ‘show_count’ => false,
    ‘days’ => 30,
    ‘show’ => posts
    ));
    but nothing is displayed. Can you please help me this

  18. I’ve had the Stats Helper plugin installed for a few months at the bottom of my blog to display a counter of visits/hits. It was working fine until recently when an error appeared…

    Warning: Invalid argument supplied for foreach() in /wp-content/plugins/wpcomstats-helper.php on line 107

    However it still displays the hits/view count after the error. I’ve tried to tinker a few things but nothing seemed to help. Any ideas?

  19. Hi Vlad,
    Thanks for developing this wonderful plugin.
    I wanted to use this plugin to display most viewed post on my website http://www.asianweek.com
    I have installed wordpress stats plugin and wordpress stats helper plugin.I can see the stats appearing on my dashboard but however i cannot see the stats on my Most viewed section. here my code. Most Viewed

    Please let me know where i am going wrong
    Thank you in advance

  20. Hey there, just installed your plugin and it’s awesome. Short, sweet & simple.

    I’m trying to modify it to display top categories (i.e. count the total views for each category) but I’m struggling. Any tips or suggestions on where/how to start?

    Cheers

  21. Hi,

    Sometimes instead of getting the data that I was supposed to get, it displays the last posts.

    Any ideas why that happens? For me it seams to be completely random.

  22. My problem may not be solvable.

    I upgraded and accidently created a second site with stats. Both have the same name. How can I fix this so that my stats will not only work but reflect my entire history?

  23. Richard, what do you mean by “accidently created a second site with stats”? A new API key or a new blog id?

    For all things related to API keys, blog IDs and stats merging I think you should talk to the wp.com support.

  24. Tried to update to 0.5.5.3, no luck once again: the plug-in continues to not to show the “most read posts”. Disappointing…

    Reverting back to 0.5.4 for the third time….

  25. Love the plugin and it was exactly what I wanted.

    Would it be possible to filter my categories and have multiple instances in the sidebar?

  26. Cristi: to display all visits to your blog you can use:


    if (function_exists('wpcomstats_visits')) {
    wpcomstats_visits('', '', null, 0);
    }

    in your template.

  27. This plugin uses data from the WordPress.com stats plugin. If you installed that recently, it’s normal to get “No data yet!”.

  28. I am trying to tweak your code, changing the results maximum from 25, to 50. But even when changed, it still only returns 50. Is this a setting locked from wordpress.com? Any ideas?

  29. Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

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>