Module: MixpanelRubyBatch::People

Defined in:
lib/mixpanel-ruby-batch/people.rb

Constant Summary collapse

VALID_OPERATIONS =
[
  "$set", "$set_once", "$add", "$append", "$union", "$unset", "$delete"
]
ADDITIVE_OPERATIONS =
[
  "$set", "$set_once", "$increment", "$append", "$union", "$track_charge"
]

Instance Method Summary collapse

Instance Method Details

#batch(profile_updates, ip = nil, optional_params = {}) ⇒ Object

Send a generic batch update to Mixpanel people analytics. The profile updates should be passed as an array of Hash objects. Each has should have a single string key that is the distinct id on which the perform the updates. The value should be a Hash with valid operation names (e.g. “$set”, “$unset”) as keys and the appropriate data for each operation as values. For details about the operations and their expected data, see the documentation at # mixpanel.com/help/reference/http

tracker = Mixpanel::Tracker.new

tracker.people.batch([
  {
    "12345" => {
      "$set" => {
         "$firstname" => "David"
      },
      "$unset" => ["Levels Completed"]
    }
  },
  {
    "67890" => {
      "$set" => {
        "$firstname" => "Mick"
      },
      "$unset" => ["Levels Completed"]
    }
  }
])


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mixpanel-ruby-batch/people.rb', line 44

def batch(profile_updates, ip=nil, optional_params={})
  messages = []
  profile_updates.each do |profile_update|
    profile_update.each_pair do |distinct_id, updates|
      updates.select! { |key, value| VALID_OPERATIONS.include?(key) }
      updates.each_pair do |operation, data|
        data = fix_property_dates(data) if ADDITIVE_OPERATIONS.include?(operation)

        message = {
          "$distinct_id" => distinct_id,
          operation => data
        }.merge(optional_params)

        message["$ip"] = ip if ip

        messages << message
      end
    end
  end

  messages.each_slice(50) { |slice| batch_update(slice) }
end