Created
April 5, 2013 10:19
-
-
Save Dan-Q/5318224 to your computer and use it in GitHub Desktop.
Cookieless tracking using HTTP 301s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# | |
# Author: Dan Q (dan@scatmania.org) | |
# More information: http://www.scatmania.org/?p=4600 | |
# | |
# This program is free software. It comes without any warranty, to | |
# the extent permitted by applicable law. You can redistribute it | |
# and/or modify it under the terms of the Do What The Fuck You Want | |
# To Public License, Version 2, as published by Sam Hocevar. See | |
# http://sam.zoy.org/wtfpl/COPYING for more details. | |
# | |
require 'rubygems' | |
require 'sinatra' # gem install sinatra | |
require 'sinatra/reloader' if development? # gem install sinatra-reloader | |
LIFESPAN = 365 * 86400 # sustain "cookie" for 365 days | |
get '/' do | |
redirect '/page/Home', 301 | |
end | |
get '/page/:page_name' do | |
page_name = params[:page_name].gsub(/[^a-z0-9 ]/i,'') | |
result = <<-END_OF_HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>C301 Cookieless Tracking Example</title> | |
</head> | |
<body> | |
<h1>#{page_name} Page</h1> | |
<p>This is the #{page_name} page.</p> | |
<p>Go to: <a href="/page/Home">Home Page</a> | <a href="/page/Second">Second Page</a> | <a href="/page/Third">Third Page</a></p> | |
<p><strong>How does this work?</strong> <a href="http://scatmania.org/?p=4600">This blog post explains everything</a>.</p> | |
<div id="session_info"><span style="color: red;">This sample requires Javascript (but does not require cookies!).</span></div> | |
<script language="Javascript" src="/c301.js"></script> | |
</body> | |
</html> | |
END_OF_HTML | |
result.strip | |
end | |
get '/c301.js' do | |
# generate a unique ID for this visitor and redirect them | |
unique_id = "#{Time::now.to_i}#{rand(999999999)}" | |
headers 'Expires' => (Time::now + LIFESPAN).httpdate | |
redirect "/c301-#{unique_id}.js", 301 | |
end | |
get '/c301-:unique_id.js' do | |
# track the user by their unique ID | |
headers 'Content-Type' => 'application/javascript', 'Expires' => (Time::now + LIFESPAN).httpdate | |
first_visit = Time::at(params[:unique_id][0...10].to_i) | |
result = <<-END_OF_JS | |
document.getElementById('session_info').innerHTML = '<p>Your unique ID is #{params[:unique_id]}.<br />Your first visit was #{first_visit}.</p><p>Now close your web browser, re-open it, and return to this page - or just click the links to browse to other pages. Even though no cookie has been set, you\\'ll see the same unique ID and first visit datetime. Try in a different browser or clear your cache to see a different unique ID.'; | |
END_OF_JS | |
result.strip | |
end | |
get '/c301.rb' do | |
# allow downloading of this source file | |
headers 'Content-Type' => 'application/x-ruby-script' | |
File::read(__FILE__) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment