require "open-uri" require 'base64' require 'json' require 'thread' require 'mixpanel/tracker/middleware' module Mixpanel class Tracker def initialize(token, env, async = false) @token = token @env = env @async = async clear_queue end def append_event(event, properties = {}) append_api('track', event, properties) end def append_api(type, *args) queue << [type, args.map {|arg| arg.to_json}] end def track_event(event, properties = {}) options = { :token => @token, :time => Time.now.utc.to_i, :ip => ip } options.merge!(properties) params = build_event(event, options) parse_response request(params) end def ip @env.has_key?("REMOTE_ADDR") ? @env["REMOTE_ADDR"] : "" end def queue @env["mixpanel_events"] end def clear_queue @env["mixpanel_events"] = [] end class < e Tracker.dispose_worker(w) end else open(url).read end end def build_event(event, properties) {:event => event, :properties => properties} end end end