Class: Mixpanel::Tracker
Overview
Use Mixpanel::Tracker to track events and profile updates in your application. To track an event, call
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
Mixpanel::Tracker.track(a_distinct_id, an_event_name, {properties})
To send people updates, call
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
tracker.people.set(a_distinct_id, {properties})
To send groups updates, call
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
tracker.groups.set(group_key, group_id, {properties})
You can find your project token in the settings dialog for your project, inside of the Mixpanel web application.
Mixpanel::Tracker is a subclass of Mixpanel::Events, and exposes an instance of Mixpanel::People as Tracker#people and an instance of Mixpanel::Groups as Tracker#groups
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
An instance of Mixpanel::Groups.
-
#people ⇒ Object
readonly
An instance of Mixpanel::People.
Instance Method Summary collapse
-
#alias(alias_id, real_id, events_endpoint = nil) ⇒ Object
Creates a distinct_id alias.
-
#generate_tracking_url(distinct_id, event, properties = {}, endpoint = nil) ⇒ Object
A call to #generate_tracking_url will return a formatted url for pixel based tracking.
-
#import(api_key, distinct_id, event, properties = {}, ip = nil) ⇒ Object
A call to #import is to import an event occurred in the past.
-
#initialize(token, error_handler = nil, &block) ⇒ Tracker
constructor
Takes your Mixpanel project token, as a string.
-
#track(distinct_id, event, properties = {}, ip = nil) ⇒ Object
A call to #track is a report that an event has occurred.
Constructor Details
#initialize(token, error_handler = nil, &block) ⇒ Tracker
Takes your Mixpanel project token, as a string.
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
By default, the tracker will send an message to Mixpanel synchronously with each call, using an instance of Mixpanel::Consumer.
You can also provide a block to the constructor to specify particular consumer behaviors (for example, if you wanted to write your messages to a queue instead of sending them directly to Mixpanel)
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN) do |type, |
@kestrel.set(MY_MIXPANEL_QUEUE, [type,].to_json)
end
If a block is provided, it is passed a type (one of :event or :profile_update) and a string message. This same format is accepted by Mixpanel::Consumer#send! and Mixpanel::BufferedConsumer#send!
55 56 57 58 59 60 |
# File 'lib/mixpanel-ruby/tracker.rb', line 55 def initialize(token, error_handler=nil, &block) super(token, error_handler, &block) @token = token @people = People.new(token, error_handler, &block) @groups = Groups.new(token, error_handler, &block) end |
Instance Attribute Details
#groups ⇒ Object (readonly)
An instance of Mixpanel::Groups. Use this to send groups updates
34 35 36 |
# File 'lib/mixpanel-ruby/tracker.rb', line 34 def groups @groups end |
#people ⇒ Object (readonly)
An instance of Mixpanel::People. Use this to send profile updates
31 32 33 |
# File 'lib/mixpanel-ruby/tracker.rb', line 31 def people @people end |
Instance Method Details
#alias(alias_id, real_id, events_endpoint = nil) ⇒ Object
Creates a distinct_id alias. Events and updates with an alias will be considered by mixpanel to have the same source, and refer to the same profile.
Multiple aliases can map to the same real_id, once a real_id is used to track events or send updates, it should never be used as an alias itself.
Alias requests are always sent synchronously, directly to the Mixpanel service, regardless of how the tracker is configured.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mixpanel-ruby/tracker.rb', line 123 def alias(alias_id, real_id, events_endpoint=nil) consumer = Mixpanel::Consumer.new(events_endpoint) data = { 'event' => '$create_alias', 'properties' => { 'distinct_id' => real_id, 'alias' => alias_id, 'token' => @token, } } = {'data' => data} ret = true begin consumer.send!(:event, .to_json) rescue MixpanelError => e @error_handler.handle(e) ret = false end ret end |
#generate_tracking_url(distinct_id, event, properties = {}, endpoint = nil) ⇒ Object
A call to #generate_tracking_url will return a formatted url for pixel based tracking. #generate_tracking_url takes 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. For more information, please see: mixpanel.com/docs/api-documentation/pixel-based-event-tracking
tracker = Mixpanel::Tracker.new(YOUR_MIXPANEL_TOKEN)
# generate pixel tracking url in order to track that user
# "12345"'s credit card was declined
url = tracker.generate_tracking_url("12345", "Credit Card Declined", {
'time' => 1310111365
})
url == 'https://api.mixpanel.com/track/?data=[BASE_64_JSON_EVENT]&ip=1&img=1'
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/mixpanel-ruby/tracker.rb', line 164 def generate_tracking_url(distinct_id, event, properties={}, endpoint=nil) properties = { 'distinct_id' => distinct_id, 'token' => @token, 'time' => Time.now.to_f, 'mp_lib' => 'ruby', '$lib_version' => Mixpanel::VERSION, }.merge(properties) raw_data = { 'event' => event, 'properties' => properties, } endpoint = endpoint || 'https://api.mixpanel.com/track/' data = Base64.urlsafe_encode64(raw_data.to_json) "#{endpoint}?data=#{data}&ip=1&img=1" end |
#import(api_key, distinct_id, event, properties = {}, ip = nil) ⇒ Object
A call to #import is to import an event occurred in the past. #import takes 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(YOUR_MIXPANEL_TOKEN)
# Import event that user "12345"'s credit card was declined
tracker.import("API_KEY", "12345", "Credit Card Declined", {
'time' => 1310111365
})
# 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',
'time' => 1310111365
})
107 108 109 110 111 |
# File 'lib/mixpanel-ruby/tracker.rb', line 107 def import(api_key, distinct_id, event, properties={}, ip=nil) # This is here strictly to allow rdoc to include the relevant # documentation super end |
#track(distinct_id, event, properties = {}, ip = nil) ⇒ Object
A call to #track is a report that an event has occurred. #track takes 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(YOUR_MIXPANEL_TOKEN)
# 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'
})
80 81 82 83 84 |
# File 'lib/mixpanel-ruby/tracker.rb', line 80 def track(distinct_id, event, properties={}, ip=nil) # This is here strictly to allow rdoc to include the relevant # documentation super end |