Class: Mixpanel

Inherits:
Hash
  • Object
show all
Defined in:
lib/mixpanel-client.rb

Constant Summary collapse

URL =
'http://api.mixpanel.com'

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Mixpanel

Require token at instantiation

Parameters:

  • project (string)

    token



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

def initialize(key)
  @key = key
end

Instance Method Details

#connObject

Create Faraday connection



41
42
43
44
45
# File 'lib/mixpanel-client.rb', line 41

def conn 
  Faraday.new(:url => URL) do |c|
    c.adapter Faraday.default_adapter
  end
end

#get_data(event, data = {}) ⇒ Object

Assemble data for request

Parameters:

  • tracking (string)

    event

  • additional (hash)

    data points



21
22
23
24
25
26
27
28
# File 'lib/mixpanel-client.rb', line 21

def get_data(event, data={})
  self['event'] = event
  self['properties'] = data 
  self['properties']['token'] = @key
  self['properties']['time'] = Time.now.to_i
  
  Base64.encode64(JSON.generate(self))
end

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

Get data and make HTTP request

Parameters:

  • tracking (string)

    event

  • additional (hash)

    data points



34
35
36
37
38
# File 'lib/mixpanel-client.rb', line 34

def track(event, data={})
  params = self.get_data(event, data)
  r = self.conn.get '/track', {:data => params}
  r.body.to_i
end