Class: Mixpal::Tracker
- Inherits:
-
Object
- Object
- Mixpal::Tracker
- Defined in:
- lib/mixpal/tracker.rb
Constant Summary collapse
- STORAGE_KEY =
'mixpal'
Instance Attribute Summary collapse
-
#alias_user ⇒ Object
readonly
Returns the value of attribute alias_user.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#user_updates ⇒ Object
readonly
Returns the value of attribute user_updates.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Tracker
constructor
A new instance of Tracker.
- #register_user(properties) ⇒ Object
- #render ⇒ Object
- #restore!(session) ⇒ Object
- #store!(session) ⇒ Object
- #track(name, properties = {}) ⇒ Object
- #update_user(properties) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Tracker
Returns a new instance of Tracker.
7 8 9 10 11 12 |
# File 'lib/mixpal/tracker.rb', line 7 def initialize(args = {}) @events = [] @user_updates = [] @identity = args[:identity] end |
Instance Attribute Details
#alias_user ⇒ Object (readonly)
Returns the value of attribute alias_user.
3 4 5 |
# File 'lib/mixpal/tracker.rb', line 3 def alias_user @alias_user end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
3 4 5 |
# File 'lib/mixpal/tracker.rb', line 3 def events @events end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
3 4 5 |
# File 'lib/mixpal/tracker.rb', line 3 def identity @identity end |
#user_updates ⇒ Object (readonly)
Returns the value of attribute user_updates.
3 4 5 |
# File 'lib/mixpal/tracker.rb', line 3 def user_updates @user_updates end |
Instance Method Details
#register_user(properties) ⇒ Object
14 15 16 17 |
# File 'lib/mixpal/tracker.rb', line 14 def register_user(properties) @alias_user = true update_user(properties) end |
#render ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mixpal/tracker.rb', line 27 def render ''.tap do |html| html << '<script type="text/javascript">' html << "mixpanel.alias(\"#{identity}\");" if alias_user html << "mixpanel.identify(\"#{identity}\");" if identity html << events.map(&:render).join('') html << user_updates.map(&:render).join('') html << '</script>' end.html_safe end |
#restore!(session) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mixpal/tracker.rb', line 42 def restore!(session) data = session[STORAGE_KEY] || {} @alias_user = data['alias_user'] @identity ||= data['identity'] if data['events'] @events = data['events'].map { |e| Mixpal::Event.from_store(e) } end if data['user_updates'] @user_updates = data['user_updates'] .map { |u| Mixpal::User.from_store(u) } end session.delete(STORAGE_KEY) end |
#store!(session) ⇒ Object
38 39 40 |
# File 'lib/mixpal/tracker.rb', line 38 def store!(session) session[STORAGE_KEY] = to_store end |
#track(name, properties = {}) ⇒ Object
23 24 25 |
# File 'lib/mixpal/tracker.rb', line 23 def track(name, properties = {}) events << Mixpal::Event.new(name, properties) end |
#update_user(properties) ⇒ Object
19 20 21 |
# File 'lib/mixpal/tracker.rb', line 19 def update_user(properties) user_updates << Mixpal::User.new(properties) end |