Class: Rack::Mixpanel::Tracker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, ip, distinct_id) ⇒ Tracker

Returns a new instance of Tracker.



28
29
30
31
32
# File 'lib/rack/mixpanel.rb', line 28

def initialize(token, ip, distinct_id)
  @token = token
  @ip = ip
  @distinct_id = distinct_id
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



27
28
29
# File 'lib/rack/mixpanel.rb', line 27

def thread
  @thread
end

Instance Method Details

#track(event, properties) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rack/mixpanel.rb', line 34

def track(event, properties)
  base_props = {
    'token' => @token,
    'ip' => @ip,
    'distinct_id' => @distinct_id
  }
  enc_props = Base64.encode64 JSON.generate({'event' => event, 'properties' => base_props.merge(properties)})
  uri = URI('https://api.mixpanel.com/track')
  uri.query = URI.encode_www_form(:data => enc_props)
  @thread = Thread.new { uri.read }
  nil
end