Class: Sinatra::MixpanelObject
- Inherits:
-
Object
- Object
- Sinatra::MixpanelObject
- Defined in:
- lib/sinmetrics/mixpanel.rb
Constant Summary collapse
- @@version =
1
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(app) ⇒ MixpanelObject
constructor
A new instance of MixpanelObject.
- #log_event(event, user_id, opts = {}) ⇒ Object
- #log_funnel(funnel_name, step_number, step_name, user_id, opts = {}) ⇒ Object
- #request(*args) ⇒ Object
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..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_key ⇒ Object
Returns the value of attribute api_key.
23 24 25 |
# File 'lib/sinmetrics/mixpanel.rb', line 23 def api_key @api_key end |
#app ⇒ Object (readonly)
Returns the value of attribute app.
22 23 24 |
# File 'lib/sinmetrics/mixpanel.rb', line 22 def app @app end |
#secret ⇒ Object
Returns the value of attribute secret.
23 24 25 |
# File 'lib/sinmetrics/mixpanel.rb', line 23 def secret @secret end |
#token ⇒ Object
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 = {}) = {} ['ip'] = @app.request.ip if @app ['time'] = Time.now.to_i ['token'] = token ['distinct_id'] = user_id if user_id opts.each do |key, value| if [:step].include? key [key.to_s] = value.to_i else [key.to_s] = value.to_s end end data = ::Base64.encode64( { 'event' => event, 'properties' => }.to_json ).gsub(/\n/, '') data = "#{data}&ip=1" if .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..mixpanel_request *args else @request.call *args end end |