Simple Tracking Pixel

Posted by Miguel Lopes on Fri, Dec 18, 2015
In Scripts,

Recently I felt the need to use an old trick. I needed to know when an email has been seen without expressly asking the recipient to do so. Yesterday before sending an email I was considering possible outcomes and decided to resort to what is now called a tracking pixel. A tracking pixel is a technique that consists in creating a link that is inserted in a email as an image, the nice part is that we record the event so we know when it happened. For this trick I wanted to use as low resources as possible and make it reusable. For that I added a location to nginx configuration making it log all access to a separate file and returning an empty image in the process.

location /pixel.gif {
access_log /var/log/www/trackingpixel/acessos.log track;
empty_gif;
}

With that I already have the basic functionality that allows me to create arbitrary links with a identifiable argument and a different access log. The second argument in the access_log refers to the log format, I wanted to reduce the number of data stored per record in the log as there was irrelevant information there. So I created a log format in nginx as refered in the previous configuration.

log_format track '$remote_addr [$time_iso8601] [$http_user_agent] $args';

The links will be differentiated from the arguments of the URI, there are other ways, like using a subdomain, or the path which will look more legit but for the purposes of this demo we will use the arguments. Now with this I lack only an interface for easy access. For this I created a small script in PHP that reads the log file and presents the data in a table. Now we have a system that allows us in some cases (not all email clients will open the remote images by default) to know when the email was seen and the user-agent of the e-mail client or web browser.

Now for this to work I only need to attach a remote image to the email like

<img src="http://somedomain.com/pixel.gif?3b23c" alt="A single pixel" />

This project is available on GitHub.



comments powered by Disqus