require "open-uri" require 'base64' require 'json' require 'thread' require 'mixpanel/tracker/middleware' module Mixpanel class Tracker def initialize(token, env, async = false, url = 'http://api.mixpanel.com/track/?data=') @token = token @env = env @async = async @url = url 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 = { :time => Time.now.utc.to_i, :ip => ip } options.merge!( :token => @token ) if @token options.merge!(properties) params = build_event(event, options) parse_response request(params) end def ip if @env.has_key?("HTTP_X_FORWARDED_FOR") @env["HTTP_X_FORWARDED_FOR"].split(",").last elsif @env.has_key?("REMOTE_ADDR") @env["REMOTE_ADDR"] else "" end 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