Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

FlatPress 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)...
 plugin.postviews.php
add_action('entry_block', 'plugin_postviews_do');
add_filter('the_content', 'plugin_postviews_track', -1);
function plugin_postviews_calc($id, $calc) {
$dir = entry_dir($id);
if (!$dir) return;
$f = $dir . '/view_counter' .EXT;
$v = io_load_file($f);
if ($v===false){
$v = 0;
}
return $v;
}
function plugin_postviews_do($id) {
global $fpdb, $smarty;
$q = $fpdb->getQuery();
$calc = $q->single;
$v = plugin_postviews_calc($id, $calc);
$smarty->assign('views', $v);
}
function plugin_postviews_track($content) {
global $smarty;
$id = $smarty->get_template_vars('id');
return $content .
'<img src="/'.PLUGINS_DIR.'postviews/v.php?v='.$id.'" width="1" height="1"/>';
}




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...
 v.php
<?php
chdir(__DIR__ . '/../..'); // simulate working from blog root
require_once('defaults.php');
require_once(INCLUDES_DIR.'includes.php');
if (function_exists('system_init')) {
system_init();
}
else {
plugin_loadall();
}
if (isset($_GET['v'])) {
$id = $_GET['v'];
$calc = true;
// make sure we have a valid ID
if (strlen($id) == 18) {
$dir = entry_dir($id);
if (!$dir) return;
$f = $dir . '/view_counter' .EXT;
$v = io_load_file($f);
if ($v===false){
$v = 0;
}
elseif ($v < 0) {
$v = 0;
$calc = false;
}
if ($calc && !user_loggedin()) {
$v++;
io_write_file($f, $v);
}
// output 1x1 transparent tracking pixel
header("Content-type: image/gif");
header("Content-Length: 42");
header("Cache-Control: private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
header("Expires: Wed, 11 Jan 2000 12:59:00 GMT");
header("Pragma: no-cache");
echo base64_decode('R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEA');
}
}
?>


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.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.