July 29, 2024
Simple Analytics that Respect Privacy: a How-To Guide
A programming post for today, but with a little twist. We’re not making a story planning program or an interactive fiction “game”. In a way, we’re not even making anything concrete – though I will share with you some code samples. Instead, we’ll take a more theoretical look at what I term simple analytics, with respect for privacy in the foreground.
As you perhaps recall, I recently revamped both the main Home for Fiction site and the blog itself. Doing so, one of the things I wanted to do was to completely eradicate all forms of privacy-invasive analytics. So I threw out Google Analytics as well as Jetpack analytics for WordPress.
Let’s take a look why and, mostly, what could one do if they’d like some simple form of stats/info that respects users’ privacy. I’ll also throw in a story about grandpas’ underwear for good measure – trust me, there is a connection hidden in there!
Simple Analytics: Why I Gave Google and Jetpack the Boot
There were many reasons why I removed that crap from my site. First of all, I wanted a leaner, faster, simpler codebase. Moreover, I didn’t care enough about the stats and their impossible complexity.
I mean, if you’ve ever used Google Analytics, you know how impossibly convoluted they are. To me, it’s entirely useless to know whether someone read my review of A Girl in Exile or an analysis on Madame Bovary on Sunday at 13:45:03, from a Samsung mobile phone running Android 9, connected to an internet cafe in Mumbai while wearing a pink t-shirt (joking; I hope). I’m sure these are useful to the armies of marketers and advertisers out there – and here’s a special message from Bill Hicks:
Seriously, there is something profoundly dystopian about this level of privacy invasion. And don’t get me started on cookie banners that have three pages of 20 lists of options each – such things represent the very worst of the internet.
For the majority of average users – that’s me in our context, in the sense I don’t need any of the convoluted, privacy-invasive analytics data – a basic idea of what’s going on is more than enough.
The Grandpa Underwear Paradigm
I did threaten promise you I’d talk about this; it’s relevant, bear with me.
I knew a guy in my “previous life” who would always say things exactly as they popped up in his head. He had no social filter, no time for niceties. I didn’t know him very well – we were acquaintances, not friends – but from interactions he never came across as arrogant, impolite, or anything of the sort. It was refreshing, he was just an overgrown kid who spoke freely.
So one day this guy went into a store to buy underwear. It was a modern store in a shopping mall, so the eager retail worker started showing him all kinds of fancy boxers with pictures of bananas, eggplants, and other innuendos, short briefs with logos of superheroes, you get the idea. Every time, he kept asking for something simpler. The hapless employee tried to show simpler items, going as far (gasp!) as showing a pair of boxers in a “simple” hot pink without any designs.
At that point the guy, exasperated, sighed and said “the grandpa underwear, ma’am! I want the grandpa underwear”.
That’s what I wanted: simple analytics, grandpa version.
Simple Analytics that Respect Privacy: How, with a Bit of PHP
First of all, the only way to have analytics that respect privacy is to not log any data about your users; not even their IP. If you want to know which country your users are from and such, this isn’t for you.
What I wanted – which might or might not coincide with your needs – was simply to know simple things; grandpa things:
- Which of my books people downloaded today?
- Which was the most used app today?
- What do people want to know about contacting Home for Fiction?
You can do such things easily with a simple PHP script, combining it with JavaScript. Here’s an example from the FAQ section:
//You can place something like this in a click event listener in your JavaScript
fetch('log.php', {method: 'POST',headers: {'Content-Type': 'application/x-www-form-urlencoded',},body: 'questionId=' + encodeURIComponent(question.innerHTML),});
//Then, in your log.php, here's how to receive the info and log it:
$questionId = $_POST['questionId'];
$filename = '/path/to/your_file.log';
$lineLog = (date('Y-m-d H:i:s', strtotime('+4 hours')) . ", " . $questionId);
file_put_contents($filename, $lineLog . PHP_EOL, FILE_APPEND);
This kind of super-simple, grandpa-simple analytics logs the time/date (I’ve added strtotime to align it with my time zone) someone clicked a question and what it was. That’s it. No IP, no device used, nothing. It respects the user, and it’s useful enough – “good enough” – for my purposes.
Simple Analytics: Another Example
Here’s an example from Punning Walrus, that lets me know which cartoons are displayed:
//Every time a cartoon is requested, the JavaScript frontend sends these to a PHP script. "Mode" refers to what kind of request is made (e.g. a random or a specific search), and the associated code (e.g. which specific cartoon is fetched).
let theData = {
theType: JSON.stringify([mode, code]),
};
$.ajax({
url: "script.php",
type: 'GET',
data: theData,
dataType: 'json',
success: function (response) {
//rest of the code
})
//And this is relevant portion of script.php
$type = json_decode($_GET['theType'], true);
$filename = '/path/to/your_file.log';
$lineLog = (date('Y-m-d H:i:s', strtotime('+4 hours')) . ", " . $type[0] . " " . $type[1]);
file_put_contents($filename, $lineLog . PHP_EOL, FILE_APPEND);
Here’s what the entries look like:
2024-01-07 18:25:57, recent 186
2024-01-07 18:26:14, random 99
2024-01-07 18:26:16, random 151
Date, time, what kind of cartoon search and which cartoon. Simple, informative, and above all, respecting the privacy of Home for Fiction users.
Simplicity Allows You to Focus on what Matters
“Everything should be made as simple as possible, but no simpler,” said Einstein – actually, he never said that; he did say this“It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience.”, which is in the same direction. Seems Einstein didn’t follow his own example, huh?!
For our purposes, simple analytics is great; unless it’s simpler than you need. If you need complex data, then you need complex data.
This post simply (!) displays how you don’t need Google or Jetpack to simply know which parts of your site are the ones accessed the most, and when. Needless to say, the examples I shared here are only a starting point. Your creativity is the limit.