Profile pic of Tommy KuTommy Ku's Blog

Reading and thinking

Be lazy and still respect DNT and GPC

Posted on  by Tommy Ku

This is a (rather late) follow-up to my previous posts to self-dogfooding the use tracking pixel to track the number of page views to my site while being as lazy as I can.

My method is known to bypass (my own) adblocker and does a good job double-counting all repeated page visits from everyone, meaning I really isn’t tracking anyone except for the page view. Even Nginx logs out more information than I do.

Log from Nginx
Hmm…legit traffic?

Oh yeah that…I should subscribe to some more IP blocklists.

AbuseIPDB
Well, not legit traffic

But I am lazy and poor so I didn’t want to setup a Nginx reverse proxy somewhere on the cloud just to get some logs. And if you don’t want to be tracked I don’t want to track you. In fact, I simply don’t want to track myself because it pollutes my statistics.

Being lazy, the simplest way is to add my own website to my adblocker’s custom list. That’ll cover the desktops that I use. However Google is evil enough to disallow Chrome extension on Chrome for Android. Blocking traffic to the tracking pixel endpoint using adblocker isn’t enough.

DNT stands for “Do Not Track”, a long neglected wanna-be standard that failed to become mainstream like other things that came before because websites decided that they don’t care.

GPC stands for “Global Privacy Control” that seems to be backed by a legal standpoint but I bet still nobody cares enough about (except), especially the small web.

Since I am getting bored about this already, here is the code. The only interesting part is the if conditions about DNT and GPC headers.

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
  const page = decodeURIComponent(new URL(request.url).search.replace(/^\?/, ''));
  // Try to respect DNT and GPC---
  const dnt = request.headers.get('dnt');
  const secGpc = request.headers.get('Sec-GPC');
  if (dnt === '1' || secGpc === '1') {
    return Response.redirect('https://blog.tommyku.com/assets/images/pixel.png?ignored', 301);
  }
  // ---Try to respect DNT and GPC
  if (page && page.substr(0, 4) === 'http') {
    let views = parseInt(await BLOG_VIEW.get(page));
    if (Number.isNaN(views)) {
        views = 0;
    }
    views++;
    await BLOG_VIEW.put(page, views);
  }
  return Response.redirect('https://blog.tommyku.com/assets/images/pixel.png', 301);
}

You could also look at...

This post first appeared on . Permalink:

About the author

Profile pic of Tommy Ku

Tommy Ku, a Hong Kong-based Software Engineer experienced developing PHP and Java-based web solutions and passionate in Web technology.

Also a hobbyist digital and film photographer.