module Mixpanel::Person

Constants

PERSON_PROPERTIES

from mixpanel.com/docs/people-analytics/special-properties

PERSON_REQUEST_PROPERTIES

from mixpanel.com/docs/people-analytics/people-http-specification-insert-data

PERSON_URL

Public Instance Methods

append_identify(distinct_id) click to toggle source
# File lib/mixpanel/person.rb, line 66
def append_identify(distinct_id)
  append 'identify', distinct_id
end
append_increment(property, increment=1) click to toggle source
# File lib/mixpanel/person.rb, line 50
def append_increment(property, increment=1)
  append 'people.increment', property, increment
end
append_register(properties={}) click to toggle source
# File lib/mixpanel/person.rb, line 58
def append_register(properties={})
  append 'register', properties_hash(properties, PERSON_PROPERTIES)
end
append_register_once(properties={}) click to toggle source
# File lib/mixpanel/person.rb, line 62
def append_register_once(properties={})
  append 'register_once', properties_hash(properties, PERSON_PROPERTIES)
end
append_set(properties={}) click to toggle source
# File lib/mixpanel/person.rb, line 42
def append_set(properties={})
  append 'people.set', properties_hash(properties, PERSON_PROPERTIES)
end
append_set_once(properties = {}) click to toggle source
# File lib/mixpanel/person.rb, line 46
def append_set_once(properties = {})
  append 'people.set_once', properties_hash(properties, PERSON_PROPERTIES)
end
append_track_charge(amount) click to toggle source
# File lib/mixpanel/person.rb, line 54
def append_track_charge(amount)
  append 'people.track_charge', amount
end
delete(distinct_id) click to toggle source
# File lib/mixpanel/person.rb, line 34
def delete(distinct_id)
  engage 'delete', distinct_id, {}, {}
end
increment(distinct_id, properties={}, options={}) click to toggle source
# File lib/mixpanel/person.rb, line 20
def increment(distinct_id, properties={}, options={})
  engage :add, distinct_id, properties, options
end
reset_charges(distinct_id, options={}) click to toggle source
# File lib/mixpanel/person.rb, line 38
def reset_charges(distinct_id, options={})
  engage :set, distinct_id, { '$transactions' => [] }, options
end
set(distinct_id, properties={}, options={}) click to toggle source
# File lib/mixpanel/person.rb, line 8
def set(distinct_id, properties={}, options={})
  engage :set, distinct_id, properties, options
end
set_once(distinct_id, properties={}, options={}) click to toggle source
# File lib/mixpanel/person.rb, line 16
def set_once(distinct_id, properties={}, options={})
  engage :set_once, distinct_id, properties, options
end
track_charge(distinct_id, amount, time=Time.now, options={}) click to toggle source
# File lib/mixpanel/person.rb, line 24
def track_charge(distinct_id, amount, time=Time.now, options={})
  charge_properties = {
    '$transactions' => {
      '$amount' => amount,
      '$time' => time,
      }
    }
  engage :append, distinct_id, charge_properties, options
end
unset(distinct_id, property, options={}) click to toggle source
# File lib/mixpanel/person.rb, line 12
def unset(distinct_id, property, options={})
  engage :unset, distinct_id, property, options
end

Protected Instance Methods

build_person(action, request_properties, person_properties) click to toggle source
# File lib/mixpanel/person.rb, line 96
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
build_person_unset(request_properties, property) click to toggle source
# File lib/mixpanel/person.rb, line 100
def build_person_unset(request_properties, property)
  properties_hash(request_properties, PERSON_REQUEST_PROPERTIES).merge({ "$unset".to_sym => [property] })
end
engage(action, request_properties_or_distinct_id, properties, options) click to toggle source
# File lib/mixpanel/person.rb, line 72
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)

  if action == :unset
    data = build_person_unset request_properties, properties
  else
    data = build_person action, request_properties, properties
  end

  parse_response post_request(options[:url], { :data => encoded_data(data) }, options[:async])
end
person_request_properties(request_properties_or_distinct_id) click to toggle source
# File lib/mixpanel/person.rb, line 87
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