Class: Mixpanel::People
- Inherits:
-
Object
- Object
- Mixpanel::People
- Defined in:
- lib/mixpanel-ruby/people.rb
Overview
Handles formatting Mixpanel profile updates and sending them to the consumer. You will rarely need to instantiate this class directly- to send profile updates, use Mixpanel::Tracker#people
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
tracker.people.set(...) # Or .append(..), or track_charge(...) etc.
Instance Method Summary collapse
-
#append(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Appends a values to the end of list-valued properties.
-
#clear_charges(distinct_id, ip = nil, optional_params = {}) ⇒ Object
Clear all charges from a Mixpanel people profile.
-
#delete_user(distinct_id, optional_params = {}) ⇒ Object
Permanently delete a profile from Mixpanel people analytics To delete a user and ignore alias pass into optional params “$ignore_alias”=>true.
-
#increment(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Changes the value of properties by a numeric amount.
-
#initialize(token, error_handler = nil, &block) ⇒ People
constructor
You likely won’t need to instantiate instances of Mixpanel::People directly.
-
#plus_one(distinct_id, property_name, ip = nil, optional_params = {}) ⇒ Object
Convenience method- increases the value of a numeric property by one.
-
#set(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Sets properties on a user record.
-
#set_once(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
set_once works just like #set, but will only change the value of properties if they are not already present in the profile.
-
#track_charge(distinct_id, amount, properties = {}, ip = nil, optional_params = {}) ⇒ Object
Records a payment to you to a profile.
-
#union(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Set union on list valued properties.
-
#unset(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Removes properties and their values from a profile.
-
#update(message) ⇒ Object
Send a generic update to Mixpanel people analytics.
Constructor Details
#initialize(token, error_handler = nil, &block) ⇒ People
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mixpanel-ruby/people.rb', line 25 def initialize(token, error_handler=nil, &block) @token = token @error_handler = error_handler || ErrorHandler.new if block @sink = block else consumer = Consumer.new @sink = consumer.method(:send!) end end |
Instance Method Details
#append(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mixpanel-ruby/people.rb', line 127 def append(distinct_id, properties, ip=nil, optional_params={}) properties = fix_property_dates(properties) = { '$distinct_id' => distinct_id, '$append' => properties, }.merge(optional_params) ['$ip'] = ip if ip update() end |
#clear_charges(distinct_id, ip = nil, optional_params = {}) ⇒ Object
Clear all charges from a Mixpanel people profile
201 202 203 |
# File 'lib/mixpanel-ruby/people.rb', line 201 def clear_charges(distinct_id, ip=nil, optional_params={}) unset(distinct_id, '$transactions', ip, optional_params) end |
#delete_user(distinct_id, optional_params = {}) ⇒ Object
Permanently delete a profile from Mixpanel people analytics To delete a user and ignore alias pass into optional params
{"$ignore_alias"=>true}
208 209 210 211 212 213 |
# File 'lib/mixpanel-ruby/people.rb', line 208 def delete_user(distinct_id, optional_params={}) update({ '$distinct_id' => distinct_id, '$delete' => '', }.merge(optional_params)) end |
#increment(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Changes the value of properties by a numeric amount. Takes a hash with string keys and numeric properties. Mixpanel will add the given amount to whatever value is currently assigned to the property. If no property exists with a given name, the value will be added to zero.
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
tracker.people.increment("12345", {
'Coins Spent' => 7,
'Coins Earned' => -7, # Use a negative number to subtract
});
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mixpanel-ruby/people.rb', line 95 def increment(distinct_id, properties, ip=nil, optional_params={}) properties = fix_property_dates(properties) = { '$distinct_id' => distinct_id, '$add' => properties, }.merge(optional_params) ['$ip'] = ip if ip update() end |
#plus_one(distinct_id, property_name, ip = nil, optional_params = {}) ⇒ Object
113 114 115 |
# File 'lib/mixpanel-ruby/people.rb', line 113 def plus_one(distinct_id, property_name, ip=nil, optional_params={}) increment(distinct_id, {property_name => 1}, ip, optional_params) end |
#set(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Sets properties on a user record. Takes a Hash with string keys, and values that are strings, numbers, booleans, or DateTimes
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
# Sets properties on profile with id "1234"
tracker.people.set("1234", {
'company' => 'Acme',
'plan' => 'Premium',
'Sign-Up Date' => DateTime.now
});
If you provide an ip argument, Mixpanel will use that ip address for geolocation (rather than the ip of your server)
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mixpanel-ruby/people.rb', line 51 def set(distinct_id, properties, ip=nil, optional_params={}) properties = fix_property_dates(properties) = { '$distinct_id' => distinct_id, '$set' => properties, }.merge(optional_params) ['$ip'] = ip if ip update() end |
#set_once(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
set_once works just like #set, but will only change the value of properties if they are not already present in the profile. That means you can call set_once many times without changing an original value.
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
tracker.people.set_once("12345", {
'First Login Date': DateTime.now
});
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mixpanel-ruby/people.rb', line 72 def set_once(distinct_id, properties, ip=nil, optional_params={}) properties = fix_property_dates(properties) = { '$distinct_id' => distinct_id, '$set_once' => properties, }.merge(optional_params) ['$ip'] = ip if ip update() end |
#track_charge(distinct_id, amount, properties = {}, ip = nil, optional_params = {}) ⇒ Object
Records a payment to you to a profile. Charges recorded with #track_charge will appear in the Mixpanel revenue report.
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
# records a charge of $25.32 from user 12345
tracker.people.track_charge("12345", 25.32)
# records a charge of $30.50 on the 2nd of January,
mixpanel.people.track_charge("12345", 30.50, {
'$time' => DateTime.parse("Jan 2 2013")
})
194 195 196 197 198 |
# File 'lib/mixpanel-ruby/people.rb', line 194 def track_charge(distinct_id, amount, properties={}, ip=nil, optional_params={}) properties = fix_property_dates(properties) charge_properties = properties.merge({'$amount' => amount}) append(distinct_id, {'$transactions' => charge_properties}, ip, optional_params) end |
#union(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Set union on list valued properties. Associates a list containing all elements of a given list, and all elements currently in a list associated with the given property. After a union, every element in the list associated with a property will be unique.
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
tracker.people.union("12345", {
'Levels Completed' => ['Suffragette City']
});
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mixpanel-ruby/people.rb', line 149 def union(distinct_id, properties, ip=nil, optional_params={}) properties = fix_property_dates(properties) = { '$distinct_id' => distinct_id, '$union' => properties, }.merge(optional_params) ['$ip'] = ip if ip update() end |
#unset(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object
Removes properties and their values from a profile.
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
# removes a single property and its value from a profile
tracker.people.unset("12345", "Overdue Since")
# removes multiple properties and their values from a profile
tracker.people.unset("12345", ["Overdue Since", "Paid Date"])
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/mixpanel-ruby/people.rb', line 170 def unset(distinct_id, properties, ip=nil, optional_params={}) properties = [properties] unless properties.is_a?(Array) = { '$distinct_id' => distinct_id, '$unset' => properties, }.merge(optional_params) ['$ip'] = ip if ip update() end |
#update(message) ⇒ Object
Send a generic update to Mixpanel people analytics. Caller is responsible for formatting the update message, as documented in the Mixpanel HTTP specification, and passing the message as a dict to #update. This method might be useful if you want to use very new or experimental features of people analytics from Ruby The Mixpanel HTTP tracking API is documented at mixpanel.com/help/reference/http
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mixpanel-ruby/people.rb', line 223 def update() data = { '$token' => @token, '$time' => Time.now.to_f, }.merge() = {'data' => data} ret = true begin @sink.call(:profile_update, .to_json) rescue MixpanelError => e @error_handler.handle(e) ret = false end ret end |