Class: Rack::Mixpanel

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mixpanel.rb

Defined Under Namespace

Classes: Tracker

Instance Method Summary collapse

Constructor Details

#initialize(app, token = nil) ⇒ Mixpanel

Returns a new instance of Mixpanel.



9
10
11
12
# File 'lib/rack/mixpanel.rb', line 9

def initialize(app, token=nil)
  @app = app
  @token = token || ENV['MIXPANEL_API_TOKEN']
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/mixpanel.rb', line 14

def call(env)
  request = Rack::Request.new(env)
  mp_cookie = request.cookies["mp_#{@token}_mixpanel"]
  if mp_cookie
    mp_env = JSON.parse(mp_cookie)
    distinct_id = mp_env['distinct_id']
    env['mixpanel.distinct_id'] = distinct_id
  end
  env['mixpanel.tracker'] = Tracker.new(@token, request.ip, env['mixpanel.distinct_id'])
  @app.call(env)
end