Class: Sinatra::MixpanelObject

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

Constant Summary collapse

@@version =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MixpanelObject

Returns a new instance of MixpanelObject.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sinmetrics/mixpanel.rb', line 8

def initialize app
  if app.respond_to?(:options)
    @app = app
    [:api_key, :secret, :token].each do |var|
      instance_variable_set("@#{var}", app.options.send("mixpanel_#{var}"))
    end
  else
    @request = Proc.new { |url| Net::HTTP.get( URI.parse(url) ) }
    [:api_key, :secret, :token, :request ].each do |var|
      instance_variable_set("@#{var}", app[var]) if app.has_key?(var)
    end
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



23
24
25
# File 'lib/sinmetrics/mixpanel.rb', line 23

def api_key
  @api_key
end

#appObject (readonly)

Returns the value of attribute app.



22
23
24
# File 'lib/sinmetrics/mixpanel.rb', line 22

def app
  @app
end

#secretObject

Returns the value of attribute secret.



23
24
25
# File 'lib/sinmetrics/mixpanel.rb', line 23

def secret
  @secret
end

#tokenObject

Returns the value of attribute token.



23
24
25
# File 'lib/sinmetrics/mixpanel.rb', line 23

def token
  @token
end

Instance Method Details

#log_event(event, user_id, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sinmetrics/mixpanel.rb', line 33

def log_event(event, user_id, opts = {})      
  options = {}
  options['ip'] = @app.request.ip if @app
  options['time'] = Time.now.to_i
  options['token'] = token
  options['distinct_id'] = user_id if user_id
  opts.each do |key, value|
    if [:step].include? key
      options[key.to_s] = value.to_i
    else
      options[key.to_s] = value.to_s
    end
  end
  
  data = ::Base64.encode64( { 'event' => event, 'properties' => options }.to_json ).gsub(/\n/, '')
  data = "#{data}&ip=1" if options.has_key? 'ip'
  request "http://api.mixpanel.com/track/?data=#{data}"
end

#log_funnel(funnel_name, step_number, step_name, user_id, opts = {}) ⇒ Object



52
53
54
55
# File 'lib/sinmetrics/mixpanel.rb', line 52

def log_funnel(funnel_name, step_number, step_name, user_id, opts = {})
  funnel_opts = opts.merge({:funnel => funnel_name, :step => step_number, :goal => step_name})
  log_event("mp_funnel", user_id, funnel_opts)
end

#request(*args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/sinmetrics/mixpanel.rb', line 25

def request *args
  if @app
    @app.options.mixpanel_request *args
  else
    @request.call *args
  end
end