Class: Mixpanel::People

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(token, &block) ⇒ People

You likely won’t need to instantiate instances of Mixpanel::People directly. The best way to get an instance of Mixpanel::People is

tracker = Mixpanel::Tracker.new(...)
tracker.people # An instance of Mixpanel::People


23
24
25
26
27
28
29
30
31
32
# File 'lib/mixpanel-ruby/people.rb', line 23

def initialize(token, &block)
  @token = token

  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

Appends a values to the end of list-valued properties. If the given properties don’t exist, a new list-valued property will be created.

tracker = Mixpanel::Tracker.new
tracker.people.append("12345", {
    'Login Dates' => DateTime.now,
    'Alter Ego Names' => 'Ziggy Stardust'
});


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mixpanel-ruby/people.rb', line 133

def append(distinct_id, properties, ip=nil, optional_params={})
  properties = fix_property_dates(properties)
  message = {
      '$distinct_id' => distinct_id,
      '$append' => properties,
  }.merge(optional_params)

  if ip
    message['$ip'] = ip
  end

  update(message)
end

#clear_charges(distinct_id, ip = nil, optional_params = {}) ⇒ Object

Clear all charges from a Mixpanel people profile



216
217
218
# File 'lib/mixpanel-ruby/people.rb', line 216

def clear_charges(distinct_id, ip=nil, optional_params={})
  unset(distinct_id, '$transactions', ip, optional_params)
end

#delete_user(distinct_id) ⇒ Object

Permanently delete a profile from Mixpanel people analytics



221
222
223
224
225
226
# File 'lib/mixpanel-ruby/people.rb', line 221

def delete_user(distinct_id)
  update({
      '$distinct_id' => distinct_id,
      '$delete' => ''
  })
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
tracker.people.increment("12345", {
    'Coins Spent' => 7,
    'Coins Earned' => -7, # Use a negative number to subtract
});


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mixpanel-ruby/people.rb', line 98

def increment(distinct_id, properties, ip=nil, optional_params={})
  properties = fix_property_dates(properties)
  message = {
      '$distinct_id' => distinct_id,
      '$add' => properties,
  }.merge(optional_params)

  if ip
    message['$ip'] = ip
  end

  update(message)
end

#plus_one(distinct_id, property_name, ip = nil, optional_params = {}) ⇒ Object

Convenience method- increases the value of a numeric property by one. Calling #plus_one(distinct_id, property_name) is the same as calling #increment(distinct_id, => 1)

tracker = Mixpanel::Tracker.new
tracker.people.plus_one("12345", "Albums Released")


119
120
121
# File 'lib/mixpanel-ruby/people.rb', line 119

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
# 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)



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mixpanel-ruby/people.rb', line 48

def set(distinct_id, properties, ip=nil, optional_params={})
  properties = fix_property_dates(properties)
  message = {
      '$distinct_id' => distinct_id,
      '$set' => properties,
  }.merge(optional_params)

  if ip
    message['$ip'] = ip
  end

  update(message)
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
tracker.people.set_once("12345", {
    'First Login Date': DateTime.now
});


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mixpanel-ruby/people.rb', line 72

def set_once(distinct_id, properties, ip=nil, optional_params={})
  properties = fix_property_dates(properties)
  message = {
      '$distinct_id' => distinct_id,
      '$set_once' => properties,
  }.merge(optional_params)

  if ip
    message['$ip'] = ip
  end

  update(message)
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

# 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")
})


209
210
211
212
213
# File 'lib/mixpanel-ruby/people.rb', line 209

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
tracker.people.union("12345", {
    'Levels Completed' => ['Suffragette City']
});


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mixpanel-ruby/people.rb', line 158

def union(distinct_id, properties, ip=nil, optional_params={})
  properties = fix_property_dates(properties)
  message = {
      '$distinct_id' => distinct_id,
      '$union' => properties,
  }.merge(optional_params)

  if ip
    message['$ip'] = ip
  end

  update(message)
end

#unset(distinct_id, properties, ip = nil, optional_params = {}) ⇒ Object

Removes properties and their values from a profile.

tracker = Mixpanel::Tracker.new

# 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"])


182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/mixpanel-ruby/people.rb', line 182

def unset(distinct_id, properties, ip=nil, optional_params={})
  properties = [properties] unless properties.is_a?(Array)
  message = {
      '$distinct_id' => distinct_id,
      '$unset' => properties
  }.merge(optional_params)

  if ip
    message['$ip'] = ip
  end

  update(message)
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



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/mixpanel-ruby/people.rb', line 236

def update(message)
  data = {
    '$token' => @token,
    '$time' =>  ((Time.now.to_f) * 1000.0).to_i
  }.merge(message)

  message = {
    'data' => data
  }

  @sink.call(:profile_update, message.to_json)
end