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
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
OR
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)
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…
Just use Google Analytics or wp.com stats.
It’s not worth wasting time on something that’s already solved.