I am really fascinated by the idea of abusing ETag (caching mechanism built into HTTP protocol) for tracking users. It is not that I want to do in a real production application; I just appreciate how simple and clever the “trick” is. Also, not so many people seem to be aware of it.
I do not think that I can do better at explaining how ETag tracking works in general than “Cookieless cookies” article. Please take a look at it first and then (hopefully) come back here.
Instead of just admiring how clever the method is in theory, I wanted to play a little bit with the idea and create a small application to reproduce it. I decided to combine it with my desire to write some Elixir code. Hence, in this blog post I will show how to implement ETag tracking in a Phoenix application.
mix phoenix.new we can jump straight to the code. Our model is very simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
We have to store a token associated with every visitor. And of course we can store some additional information, in this case the number of previous visits.
The template that we want to render as the main (and only) page is:
1 2 3
And the interesting code is in the controller:
1 2 3 4 5 6 7 8 9 10 11 12
put_resp_header to set
ETag header to the value that we saved in the database.
1 2 3 4 5 6 7 8 9 10
If the request has
If-None-Match header set we try to find a matching record in our database. If the header is not set we treat the request as coming from a new visitor and initialize a new record.
generate_token simply returns a long, random string:
1 2 3