has a great plugin called PostViews
that counts the number of times a blog post has been viewed. One of the issues I had with this plugin is it stopped tracking the number of views when I turned on HTML
caching at my CloudFlare
front end. Naturally the HTML
was cached for most requests and therefore the plugin was never executed. The solution to this was to use a tracking pixel
and set CloudFlare
to disable caching on the tracking pixel
itself so that I could capture each of the views.
These are my changes to the plugin...
First I've replaced the PHP content of the plugin.postviews.php
file with this (update the comments at the top of the file as you wish)...
The main changes are to add a content filter to the plugin. This filter, implemented in the plugin_postviews_track
method, simply gets the ID
of the current post and returns the content with the tracking pixel code attached.
The actual tracking work happens in the v.php
file. This file should be in the same directory as the plugin.postviews.php
file. It looks like this...
What that does is load/increment the number of views for the blog post identified by its ID
. The output of the script is a 1x1 transparent pixel
, also knows as a tracking pixel
, which is how we are able to use this script as the source to the img
tag in the previous source file.
What you'll find with this change is it tracks the real 'user' views better. Since bots usually don't load things like images the view counter will not include bot requests.
Hope you found this post useful...
...so please read on! I love writing articles that provide beneficial information,
tips and examples to my readers. All information on my blog is provided free of
charge and I encourage you to share it as you wish. There is a small favour I ask in return however -
engage in comments below, provide feedback, and if you see mistakes let me know.
If you want to show additional support and help me pay for web hosting and
domain name registration,
donations, no matter how small, are always welcome!
Use of any information contained in this blog post/article is subject to this disclaimer
Other posts you may like...