Class: Mixpanel::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/mixpanel-ruby/events.rb

Overview

Handles formatting Mixpanel event tracking messages and sending them to the consumer. Mixpanel::Tracker is a subclass of this class, and the best way to track events is to instantiate a Mixpanel::Tracker

tracker = Mixpanel::Tracker.new # Has all of the methods of Mixpanel::Event
tracker.track(...)

Direct Known Subclasses

Tracker

Instance Method Summary collapse

Constructor Details

#initialize(token, &block) ⇒ Events

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

# tracker has all of the methods of Mixpanel::Events
tracker = Mixpanel::Tracker.new(...)


23
24
25
26
27
28
29
30
31
# File 'lib/mixpanel-ruby/events.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

#import(api_key, distinct_id, event, properties = {}, ip = nil) ⇒ Object

Imports an event that has occurred in the past, along with a distinct_id representing the source of that event (for example, a user id), an event name describing the event and a set of properties describing that event. Properties are provided as a Hash with string keys and strings, numbers or booleans as values.

tracker = Mixpanel::Tracker.new

# Track that user "12345"'s credit card was declined
tracker.import("API_KEY", "12345", "Credit Card Declined")

# Properties describe the circumstances of the event,
# or aspects of the source or user associated with the event
tracker.import("API_KEY", "12345", "Welcome Email Sent", {
    'Email Template' => 'Pretty Pink Welcome',
    'User Sign-up Cohort' => 'July 2013'
})


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mixpanel-ruby/events.rb', line 91

def import(api_key, distinct_id, event, properties={}, ip=nil)
  properties = {
    'distinct_id' => distinct_id,
    'token' => @token,
    'time' => Time.now.to_i,
    'mp_lib' => 'ruby',
    '$lib_version' => Mixpanel::VERSION
  }.merge(properties)
  if ip
    properties['ip'] = ip
  end

  data = {
    'event' => event,
    'properties' => properties
  }

  message = {
    'data' => data,
    'api_key' => api_key
  }

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

#track(distinct_id, event, properties = {}, ip = nil, as_pixel = nil) ⇒ Object

Notes that an event has occurred, along with a distinct_id representing the source of that event (for example, a user id), an event name describing the event and a set of properties describing that event. Properties are provided as a Hash with string keys and strings, numbers or booleans as values.

tracker = Mixpanel::Tracker.new

# Track that user "12345"'s credit card was declined
tracker.track("12345", "Credit Card Declined")

# Properties describe the circumstances of the event,
# or aspects of the source or user associated with the event
tracker.track("12345", "Welcome Email Sent", {
    'Email Template' => 'Pretty Pink Welcome',
    'User Sign-up Cohort' => 'July 2013'
})


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mixpanel-ruby/events.rb', line 50

def track(distinct_id, event, properties={}, ip=nil, as_pixel=nil)
  properties = {
      'distinct_id' => distinct_id,
      'token' => @token,
      'time' => Time.now.to_i,
      'mp_lib' => 'ruby',
      '$lib_version' => Mixpanel::VERSION
  }.merge(properties)
  if ip
    properties['ip'] = ip
  end

  data = {
    'event' => event,
    'properties' => properties
  }

  message = {
    'data' => data
  }

  @sink.call(:event, message.to_json, as_pixel)
end