lib/mixpanel/person.rb in mixpanel-3.1.0 vs lib/mixpanel/person.rb in mixpanel-3.4.0

- old
+ new

@@ -1,45 +1,59 @@ module Mixpanel::Person PERSON_PROPERTIES = %w{email created first_name last_name name last_login username country_code} + PERSON_REQUEST_PROPERTIES = %w{token distinct_id ip ignore_time} PERSON_URL = 'http://api.mixpanel.com/engage/' - + def set(distinct_id, properties={}, options={}) engage :set, distinct_id, properties, options end - + def increment(distinct_id, properties={}, options={}) engage :add, distinct_id, properties, options end - + def append_set(properties={}) append 'people.set', properties_hash(properties, PERSON_PROPERTIES) end - + def append_increment(property, increment=1) append 'people.increment', property, increment end - + def append_register(properties={}) append 'register', properties_hash(properties, PERSON_PROPERTIES) end - + def append_identify(distinct_id) append 'identify', distinct_id end def append_people_identify(distinct_id) append 'people.identify', distinct_id end - + protected - - def engage(action, distinct_id, properties, options) - options.reverse_merge! :async => @async, :url => PERSON_URL - data = build_person action, distinct_id, properties + + def engage(action, request_properties_or_distinct_id, properties, options) + default = {:async => @async, :url => PERSON_URL} + options = default.merge(options) + + request_properties = person_request_properties(request_properties_or_distinct_id) + + data = build_person action, request_properties, properties url = "#{options[:url]}?data=#{encoded_data(data)}" parse_response request(url, options[:async]) end - - def build_person(action, distinct_id, properties) - { "$#{action}".to_sym => properties_hash(properties, PERSON_PROPERTIES), :$token => @token, :$distinct_id => distinct_id } + + def person_request_properties(request_properties_or_distinct_id) + default = {:token => @token, :ip => ip} + if request_properties_or_distinct_id.respond_to? :to_hash + default.merge(request_properties_or_distinct_id) + else + default.merge({ :distinct_id => request_properties_or_distinct_id }) + end end -end \ No newline at end of file + + def build_person(action, request_properties, person_properties) + properties_hash(request_properties, PERSON_REQUEST_PROPERTIES).merge({ "$#{action}".to_sym => properties_hash(person_properties, PERSON_PROPERTIES) }) + end +end