Class: Mixpanel

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

Defined Under Namespace

Classes: Subprocess

Constant Summary collapse

WORKER_MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, env, async = false) ⇒ Mixpanel

Returns a new instance of Mixpanel.



8
9
10
11
12
13
# File 'lib/mixpanel/mixpanel.rb', line 8

def initialize(token, env, async = false)
  @token = token
  @env = env
  @async = async
  clear_queue
end

Class Method Details

.cmdObject



58
59
60
61
62
63
64
65
66
# File 'lib/mixpanel/mixpanel.rb', line 58

def cmd
  @cmd || begin
    require 'escape'
    require 'rbconfig'
    interpreter = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['RUBY_SO_NAME'])
    subprocess  = File.join(File.dirname(__FILE__), 'mixpanel_subprocess.rb')
    @cmd = Escape.shell_command([interpreter, subprocess])
  end
end

.dispose_worker(w) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/mixpanel/mixpanel.rb', line 49

def dispose_worker(w)
  WORKER_MUTEX.synchronize do
    if(@worker == w)
      @worker = nil
      w.close
    end
  end
end

.workerObject



43
44
45
46
47
# File 'lib/mixpanel/mixpanel.rb', line 43

def worker
  WORKER_MUTEX.synchronize do
    @worker || (@worker = IO.popen(self.cmd, 'w'))
  end
end

Instance Method Details

#append_api(type, *args) ⇒ Object



19
20
21
# File 'lib/mixpanel/mixpanel.rb', line 19

def append_api(type, *args)
  queue << [type, args.map {|arg| arg.to_json}]
end

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



15
16
17
# File 'lib/mixpanel/mixpanel.rb', line 15

def append_event(event, properties = {})
  append_api('track', event, properties)
end

#clear_queueObject



36
37
38
# File 'lib/mixpanel/mixpanel.rb', line 36

def clear_queue
  @env["mixpanel_events"] = []
end

#ipObject



28
29
30
# File 'lib/mixpanel/mixpanel.rb', line 28

def ip
  @env.has_key?("REMOTE_ADDR") ? @env["REMOTE_ADDR"] : ""
end

#queueObject



32
33
34
# File 'lib/mixpanel/mixpanel.rb', line 32

def queue
  @env["mixpanel_events"]
end

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



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

def track_event(event, properties = {})
  params = build_event(event, properties.merge(:token => @token, :time => Time.now.utc.to_i, :ip => ip))
  parse_response request(params)
end