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.
-
#revenue_updates ⇒ Object
readonly
Returns the value of attribute revenue_updates.
-
#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
- #track_charge(amount, properties = {}) ⇒ Object
- #update_user(properties) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Tracker
Returns a new instance of Tracker.
7 8 9 10 11 12 13 14 15 |
# File 'lib/mixpal/tracker.rb', line 7 def initialize(args = {}) extend Mixpal.configuration.helper_module @events = [] @user_updates = [] @revenue_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 |
#revenue_updates ⇒ Object (readonly)
Returns the value of attribute revenue_updates.
3 4 5 |
# File 'lib/mixpal/tracker.rb', line 3 def revenue_updates @revenue_updates 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
17 18 19 20 |
# File 'lib/mixpal/tracker.rb', line 17 def register_user(properties) @alias_user = true update_user(properties) end |
#render ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mixpal/tracker.rb', line 34 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 << revenue_updates.map(&:render).join('') html << '</script>' end.html_safe end |
#restore!(session) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mixpal/tracker.rb', line 50 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 if data['revenue_updates'] @revenue_updates = data['revenue_updates'].map { |u| Mixpal::Revenue.from_store(u) } end session.delete(STORAGE_KEY) end |
#store!(session) ⇒ Object
46 47 48 |
# File 'lib/mixpal/tracker.rb', line 46 def store!(session) session[STORAGE_KEY] = to_store end |
#track(name, properties = {}) ⇒ Object
26 27 28 |
# File 'lib/mixpal/tracker.rb', line 26 def track(name, properties = {}) events << Mixpal::Event.new(name, properties) end |