Created
August 8, 2012 22:50
-
-
Save titanous/3299536 to your computer and use it in GitHub Desktop.
Mixpanel Rack Middleware
This file contains hidden or 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
require 'base64' | |
require 'json' | |
require 'uri' | |
module Mixpanel | |
class Tracker | |
attr_reader :distinct_id, :ip, :token | |
def initialize(token, distinct_id, ip) | |
@token = token | |
@distinct_id = distinct_id | |
@ip = ip | |
end | |
def track(event, properties = {}) | |
return unless distinct_id | |
params = { event: event, properties: { token: token, distinct_id: distinct_id, ip: ip }.merge(properties) } | |
data = Base64.strict_encode64(JSON.generate(params)) | |
request = "http://api.mixpanel.com/track/?data=#{data}" | |
`curl -s '#{request}' &` | |
end | |
end | |
class Middleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
load_mixpanel(env) | |
@app.call(env) | |
end | |
private | |
def load_mixpanel(env) | |
request = Rack::Request.new(env) | |
cookie = request.cookies.find { |k,v| k =~ /^mp_.+|.+_mixpanel$/ } | |
id = JSON.parse(URI.decode(cookie[1]))['distinct_id'] if cookie | |
ensure | |
env['mixpanel'] = Tracker.new(ENV['MIXPANEL_TOKEN'], id, request.ip) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment