Class: Ahoy::Tracker
- Inherits:
-
Object
- Object
- Ahoy::Tracker
- Defined in:
- lib/ahoy/tracker.rb
Constant Summary collapse
- UUID_NAMESPACE =
"a82ae811-5011-45ab-a728-569df7499c5f"
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #authenticate(user) ⇒ Object
- #exclude? ⇒ Boolean
- #geocode(data) ⇒ Object
-
#initialize(**options) ⇒ Tracker
constructor
A new instance of Tracker.
- #new_visit? ⇒ Boolean
- #new_visitor? ⇒ Boolean
- #reset ⇒ Object
- #reset_visit ⇒ Object
- #set_visit_cookie ⇒ Object
- #set_visitor_cookie ⇒ Object
-
#track(name, properties = {}, options = {}) ⇒ Object
can’t use keyword arguments here.
- #track_visit(defer: false, started_at: nil) ⇒ Object
- #user ⇒ Object
- #visit ⇒ Object
- #visit_or_create ⇒ Object
- #visit_properties ⇒ Object
- #visit_token ⇒ Object (also: #visit_id)
- #visitor_token ⇒ Object (also: #visitor_id)
Constructor Details
#initialize(**options) ⇒ Tracker
Returns a new instance of Tracker.
9 10 11 12 13 14 15 16 |
# File 'lib/ahoy/tracker.rb', line 9 def initialize(**) @store = Ahoy::Store.new(.merge(ahoy: self)) @controller = [:controller] @request = [:request] || @controller.try(:request) @visit_token = [:visit_token] @user = [:user] @options = end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
7 8 9 |
# File 'lib/ahoy/tracker.rb', line 7 def controller @controller end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/ahoy/tracker.rb', line 7 def request @request end |
Instance Method Details
#authenticate(user) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ahoy/tracker.rb', line 76 def authenticate(user) if exclude? debug "Authentication excluded" else @store.user = user data = { visit_token: visit_token, user_id: user.try(:id) } @store.authenticate(data) end true rescue => e report_exception(e) end |
#exclude? ⇒ Boolean
148 149 150 151 152 153 |
# File 'lib/ahoy/tracker.rb', line 148 def exclude? unless defined?(@exclude) @exclude = @store.exclude? end @exclude end |
#geocode(data) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ahoy/tracker.rb', line 65 def geocode(data) data = { visit_token: visit_token }.merge(data).select { |_, v| v } @store.geocode(data) true rescue => e report_exception(e) end |
#new_visit? ⇒ Boolean
101 102 103 |
# File 'lib/ahoy/tracker.rb', line 101 def new_visit? Ahoy. ? !existing_visit_token : visit.nil? end |
#new_visitor? ⇒ Boolean
105 106 107 |
# File 'lib/ahoy/tracker.rb', line 105 def new_visitor? !existing_visitor_token end |
#reset ⇒ Object
137 138 139 140 |
# File 'lib/ahoy/tracker.rb', line 137 def reset reset_visit ("ahoy_visitor") end |
#reset_visit ⇒ Object
142 143 144 145 146 |
# File 'lib/ahoy/tracker.rb', line 142 def reset_visit ("ahoy_visit") ("ahoy_events") ("ahoy_track") end |
#set_visit_cookie ⇒ Object
109 110 111 |
# File 'lib/ahoy/tracker.rb', line 109 def ("ahoy_visit", visit_token, Ahoy.visit_duration) end |
#set_visitor_cookie ⇒ Object
113 114 115 116 117 |
# File 'lib/ahoy/tracker.rb', line 113 def if new_visitor? ("ahoy_visitor", visitor_token, Ahoy.visitor_duration) end end |
#track(name, properties = {}, options = {}) ⇒ Object
can’t use keyword arguments here
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ahoy/tracker.rb', line 19 def track(name, properties = {}, = {}) if exclude? debug "Event excluded" else data = { visit_token: visit_token, user_id: user.try(:id), name: name.to_s, properties: properties, time: trusted_time([:time]), event_id: [:id] || generate_id }.select { |_, v| v } @store.track_event(data) end true rescue => e report_exception(e) end |
#track_visit(defer: false, started_at: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ahoy/tracker.rb', line 39 def track_visit(defer: false, started_at: nil) if exclude? debug "Visit excluded" else if defer ("ahoy_track", true, nil, false) else ("ahoy_track") data = { visit_token: visit_token, visitor_token: visitor_token, user_id: user.try(:id), started_at: trusted_time(started_at) }.merge(visit_properties).select { |_, v| v } @store.track_visit(data) Ahoy::GeocodeV2Job.perform_later(visit_token, data[:ip]) if Ahoy.geocode && data[:ip] end end true rescue => e report_exception(e) end |
#user ⇒ Object
119 120 121 |
# File 'lib/ahoy/tracker.rb', line 119 def user @user ||= @store.user end |
#visit ⇒ Object
93 94 95 |
# File 'lib/ahoy/tracker.rb', line 93 def visit @visit ||= @store.visit end |
#visit_or_create ⇒ Object
97 98 99 |
# File 'lib/ahoy/tracker.rb', line 97 def visit_or_create @visit ||= @store.visit_or_create end |
#visit_properties ⇒ Object
123 124 125 |
# File 'lib/ahoy/tracker.rb', line 123 def visit_properties @visit_properties ||= request ? Ahoy::VisitProperties.new(request, api: api?).generate : {} end |
#visit_token ⇒ Object Also known as: visit_id
127 128 129 |
# File 'lib/ahoy/tracker.rb', line 127 def visit_token @visit_token ||= ensure_token(visit_token_helper) end |
#visitor_token ⇒ Object Also known as: visitor_id
132 133 134 |
# File 'lib/ahoy/tracker.rb', line 132 def visitor_token @visitor_token ||= ensure_token(visitor_token_helper) end |