Igor's Blog

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

comments powered by Disqus
Other posts you may like...
Programming, DIY, Games, Hacks, and Tech

Recent Blog Posts

Swapping HDD logic boards between different brand disks - is it possible?

A guide for plotting your hard drive for Burstcoin mining on a Mac using mjminer

Replacing a broken hard drive in a Samsung Story Station

Fix Java applications slow start and net connection times on macOS sierra

Pierre Cardin Leather Computer Bag (PC2278)

How to stop SSH remote host disconnecting your session

Using PayPal PHP SDK on Google App Engine

Use the Touch Bar to lock your Mac with a single button press

Multi module builds with Maven 3.5 and the parent-child pom version management

Google App Engine local dev server DataStore becoming corrupted after a bad GQL statement is run

Recent Galleries

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

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

Blog Activity

Blog Activity
Don't feel obliged, but you could follow me for more great content!
     
Don't show this again