Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Our site is quite high traffic and we use both nginx and w3 total cache to handle the load. We've previously been using wp-postviews to count the page views, but it seems to be locking the postmeta table now, and often doesnt count views at all. It's unreliable at best.

Can anyone suggest a way for us to count page views and put them into the DB, or any specific workable solutions?

My initial thoughts are to have the view count done via javascript to update a separate database, then a cron job at the end of each day to merge the results, but I'm not sure where to start.

Thanks in advance

share|improve this question
1  
Am I missing something stupid here, Google Analytics will track page views. While it wont be in your database you will have complete access. –  Cristian Jul 8 '11 at 8:54
    
I want it in our local database so I can start manipulating the data - a widget with the days top posts for instance. I'd also like to avoid masses of API programming ideally. –  James Bruce Jul 8 '11 at 16:53
    
Ok, for now until I've implement a quick count php script that enters data onto it's own separate server and hence avoid locking the postmeta table. The count is called from a bit of jquery ajax, and the postviews table is going to be merged each day when traffic is lowest. I'm pretty sure this will reduce queries by a factor of thousands, and yet still give us daily accurate stats that wont be affected by caching. I need to clean it up a bit, but will post something here when finished so others can use it / abuse me for bad programming ;) Thanks to everyone who contributed so far. –  James Bruce Jul 8 '11 at 17:03
    
James, could you please post more information on your solution? –  Sergey Sedykh Apr 9 '12 at 12:02
    
Actually, we had to disable my solution because it was killing the server! So, sorry, no solution. We've resorted to not using pageviews for calculated metrics, and now using social share counts instead. –  James Bruce Apr 9 '12 at 15:28

4 Answers 4

up vote 1 down vote accepted

It really depends what you need to view counts for - if it's just for seeing traffic stats, then use Google Analytics or any number of javascript tracker based analytics tools.

If you need integration of page view counts and the ability to do things like order post by views, then you can either

  • spend some time optimising your database - some options and things to consider
    • more memory for MySQL
    • change the postmeta table to be InnoDB
    • get a separate database server
    • make sure you've tuned your MySQL settings (use mysqltuner as a starting point)

OR

  • Use something like Piwik and spend time integrating it (it has a decent API) with WordPress.
share|improve this answer
    
Yes, as you say we'd like to use it in various ways with popular post widgets etc. We also already run on a separate dedicated DB server, but we avoid innoDB as I read about lots of incompatibility issues with Wordpress. I'm not a DB expert, obviously. Will take a look at Piwik though, thanks! –  James Bruce Jul 8 '11 at 16:54

Here's how you do it, assuming you have a memory caching extension active:

(I'm going to use APC as an example here because it will be bundled with PHP 6)

// set this to the current post id
$post_id = get_the_ID();

// this will attempt to get the post view count from cache (the memory)
$page_views = apc_fetch("post_{$post_id}_views");

// if it exists, increase the counter (again, only in memory)
if($page_views !== false){
  $page_views = apc_inc("post_{$post_id}_views");

  // write the data to the database every ...let's say 100 views
  if(($page_views % 10) == 100)
    update_your_database_here();
}

// doesn't exist; most likely this is the first view, so create the cache entry
else{
  apc_store("post_{$post_id}_views", 1);
  $page_views = 1;
}

// show it
printf('This post has %d views', $page_views);

You can choose to use WP's functions - wp_cache_incr() or wp_cache_decr() + W3TC with APC selected. But personally I would avoid using any cache plugins and create my own object cache handler which I can drop in the wp-content directory.

You can also use xcache for this too, it doesn't matter - The idea is that you need to store the count in memory up to a certain point, to avoid any disk writes. Since your site has high-traffic I assume you own a dedicated server, on which you can install your own PHP extensions etc. Shared hosters won't allow you to cache data in memory for obvious reasons...

share|improve this answer

Just use Google Analytics or wp.com stats.

It's not worth wasting time on something that's already solved.

share|improve this answer
1  
But what if i want to order posts by views ? –  Bainternet Jul 8 '11 at 10:09
    
Both services allow you to do that, especially wp.com stats. –  scribu Jul 8 '11 at 15:40
    
I wasn't aware wordpress.com would work with caching systems, but great idea, I'll certainly test that out! I'm not sure how I would use Google analytics to sort posts though - I guess you mean using the API and writing something custom? –  James Bruce Jul 8 '11 at 16:49
    
Actually, I'm not sure how wordpress.stats would allow us to perform wp-queries sorted by post views, and I really want to avoid additional plugin overhead for features we dont need - pretty graphs etc. –  James Bruce Jul 8 '11 at 17:00
    
You could do the query just like the plugin itself does it (Top Posts & Pages section). As for code bloat, you're free to take apart the plugin and leave only what you need. –  scribu Jul 8 '11 at 20:12

thank you for your answer onetrickpony. If I may ask, could you please expand on this answer to make it work through a JavaScript request? Because I use reverse proxy which doesn't process the PHP code on every page view. However, a Javascript call would.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.