A faster alternative to image beacons with 1x1 gif

Mar 31, 2011 misc-web
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

So it seems like a very common solution for tracking hit rates is the transparent 1x1 pixel gif. Load this and parse your access logs: requests logged.

The problem I have with this is two fold:

a) it sends a 1x1 gif’s worth of data to the server. This should exist - it could be possible for browsers to show a broken image if the gif doesn’t exist

b) it requires a full data connection and transmission to do something so simple.

Instead, let’s use the HTTP Code 204: No Content

HTTP response code 204 stands for “no content”. This means that there will be no additional data processed, sent or displayed after the last blank line in the header of the request.

Let’s put this in practice:

Before:

var track = new Image();
track.src="/my/source/image/1x1.gif";

And the corresponding image is sent.

After:

var track = new Image();
track.src = "/track/this/page.php";

page.php

<?php
die(header("HTTP/1.1 204 No Content"));

(Note, this also could be done probably just with an apache configuration to not even invoke the PHP engine).

Go to All Posts