Invisible Tracking Pixel

There already tons and tons of articles on this topic, but I think this knowledge is definitely nifty, so I decided to repeat this information in my blog.

A tracking pixel is a script that is disguised as an image. Each time the image loads, the script does whatever it was coded to do, but at the same time returns an image. This image doesn’t need to be a pixel, but a pixel is the smallest graphical information you can return. The pixel doesn’t need to be transparent either, but I just think it’s annoying to see an out of place pixel sitting on the edge of the page. So for all those who are like me, we like to implement something called a transparent pixel. Without further ado here is the php code for a transparent pixel:

header('Content-Type: image/gif');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " PST");
header("Pragma: no-cache");
$img = imagecreatetruecolor(1,1);
$color = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $color);
return imagegif($img);

The header code is telling the browser that the file is a gif and to not cache it. If you want your tracking pixel to work well, it’s best for you to include it.

For those hardcore nerds out there, you can save the generated gif into a file, check if the file is there, and if it exists, use that existing gif instead of creating a new one. Not only does this method prevent regeneration of the gif, it also allows you to pass file size information through the headers.

For those even more hardcore nerds out there, you can simply print the string that represents a transparent pixel, it’ll save you all that image creation code. Although you’ll sacrifice readability, since only a handful of us can look at a string of bytes and is able to tell exactly its supposed to be.

Leave a Reply