class Mixpanel

Constants

URL

Public Class Methods

new(key) click to toggle source

Require token at instantiation

@param [string] project token

# File lib/mixpanel-client.rb, line 13
def initialize(key)
  @key = key
end

Public Instance Methods

conn() click to toggle source

Create Faraday connection

# 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={}) click to toggle source

Assemble data for request

@param [string] tracking event @param [hash] additional data points

# 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={}) click to toggle source

Get data and make HTTP request

@param [string] tracking event @param [hash] additional data points

# 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