Class: EM::Mixpanel

Inherits:
Object
  • Object
show all
Defined in:
lib/em/mixpanel.rb,
lib/em/mixpanel/version.rb

Constant Summary collapse

TRACK_URI =
'http://api.mixpanel.com/track'
IMPORT_URI =
'http://api.mixpanel.com/import'
VERSION =
'0.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, default_properties = {}) ⇒ Mixpanel

Returns a new instance of Mixpanel.



13
14
15
# File 'lib/em/mixpanel.rb', line 13

def initialize(token, default_properties={})
  @token, @default_properties = token, default_properties
end

Instance Attribute Details

#default_propertiesObject (readonly)

Returns the value of attribute default_properties.



11
12
13
# File 'lib/em/mixpanel.rb', line 11

def default_properties
  @default_properties
end

#tokenObject (readonly)

Returns the value of attribute token.



11
12
13
# File 'lib/em/mixpanel.rb', line 11

def token
  @token
end

Instance Method Details

#import(event, properties = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/em/mixpanel.rb', line 29

def import(event, properties={})
  data = self.class.encode_data(event, {
    token: token
  }.merge(default_properties).merge(properties))
  
  EM::HttpRequest.new(IMPORT_URI.to_s).get(
    query: {
      data: data,
      api_key: token
    }
  )
end

#track(event, properties = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/em/mixpanel.rb', line 17

def track(event, properties={})
  data = self.class.encode_data(event, {
    token: token,
    time: Time.now.to_i
  }.merge(default_properties).merge(properties))
  
  EM::HttpRequest.new(TRACK_URI.to_s).post(
    body: {data: data},
    query: {ip: 0}
  )
end