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.
-
#js_snippets ⇒ Object
readonly
Returns the value of attribute js_snippets.
-
#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
- #add_js_snippet(snippet) ⇒ Object
-
#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 16 |
# File 'lib/mixpal/tracker.rb', line 7 def initialize(args = {}) extend Mixpal.configuration.helper_module @events = [] @user_updates = [] @revenue_updates = [] @js_snippets = [] @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 |
#js_snippets ⇒ Object (readonly)
Returns the value of attribute js_snippets.
3 4 5 |
# File 'lib/mixpal/tracker.rb', line 3 def js_snippets @js_snippets 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
#add_js_snippet(snippet) ⇒ Object
35 36 37 |
# File 'lib/mixpal/tracker.rb', line 35 def add_js_snippet(snippet) js_snippets << Mixpal::Snippet.new(snippet) end |
#register_user(properties) ⇒ Object
18 19 20 21 |
# File 'lib/mixpal/tracker.rb', line 18 def register_user(properties) @alias_user = true update_user(properties) end |
#render ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mixpal/tracker.rb', line 39 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 << js_snippets.map(&:render).join('') html << '</script>' end.html_safe end |
#restore!(session) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mixpal/tracker.rb', line 56 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 if data['js_snippets'] @js_snippets = data['js_snippets'].map { |u| Mixpal::Snippet.from_store(u) } end session.delete(STORAGE_KEY) end |
#store!(session) ⇒ Object
52 53 54 |
# File 'lib/mixpal/tracker.rb', line 52 def store!(session) session[STORAGE_KEY] = to_store end |
#track(name, properties = {}) ⇒ Object
27 28 29 |
# File 'lib/mixpal/tracker.rb', line 27 def track(name, properties = {}) events << Mixpal::Event.new(name, properties) end |
#track_charge(amount, properties = {}) ⇒ Object
31 32 33 |
# File 'lib/mixpal/tracker.rb', line 31 def track_charge(amount, properties = {}) revenue_updates << Mixpal::Revenue.new(amount, properties) end |
#update_user(properties) ⇒ Object
23 24 25 |
# File 'lib/mixpal/tracker.rb', line 23 def update_user(properties) user_updates << Mixpal::User.new(properties) end |