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

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Tiny Arcade revision 6 kit assembly and decal application

Jersey JAX-RS filters and interceptors execution order for a simple GET request

How to stop macOS adding shadows to window screenshots

How to run Atari Lynx games on the SNES Classic Mini

Maven dependency scopes with relation to WAR file packaging and the WEB-INF/lib directory

Hacking the Sonoff B1 WiFi LED bulb to run custom firmware

What does an idle WebLogic server run on the hour to cause a CPU spike

How to open the Sonoff B1 wifi LED bulb to access its internal circuitry

Australian release SNES mini classic unboxing and a quick play through

Troubleshooting high CPU usage for JVM threads

Recent Galleries

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity