Class: Ahoy::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy/tracker.rb

Constant Summary collapse

UUID_NAMESPACE =
"a82ae811-5011-45ab-a728-569df7499c5f"

Instance Attribute Summary collapse

Instance Method Summary collapse

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(**options)
  @store = Ahoy::Store.new(options.merge(ahoy: self))
  @controller = options[:controller]
  @request = options[:request] || @controller.try(:request)
  @visit_token = options[:visit_token]
  @user = options[:user]
  @options = options
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



7
8
9
# File 'lib/ahoy/tracker.rb', line 7

def controller
  @controller
end

#requestObject (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

Returns:

  • (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

Returns:

  • (Boolean)


101
102
103
# File 'lib/ahoy/tracker.rb', line 101

def new_visit?
  Ahoy.cookies? ? !existing_visit_token : visit.nil?
end

#new_visitor?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ahoy/tracker.rb', line 105

def new_visitor?
  !existing_visitor_token
end

#resetObject



137
138
139
140
# File 'lib/ahoy/tracker.rb', line 137

def reset
  reset_visit
  delete_cookie("ahoy_visitor")
end

#reset_visitObject



142
143
144
145
146
# File 'lib/ahoy/tracker.rb', line 142

def reset_visit
  delete_cookie("ahoy_visit")
  delete_cookie("ahoy_events")
  delete_cookie("ahoy_track")
end


109
110
111
# File 'lib/ahoy/tracker.rb', line 109

def set_visit_cookie
  set_cookie("ahoy_visit", visit_token, Ahoy.visit_duration)
end


113
114
115
116
117
# File 'lib/ahoy/tracker.rb', line 113

def set_visitor_cookie
  if new_visitor?
    set_cookie("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 = {}, options = {})
  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(options[:time]),
      event_id: options[: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
      set_cookie("ahoy_track", true, nil, false)
    else
      delete_cookie("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

#userObject



119
120
121
# File 'lib/ahoy/tracker.rb', line 119

def user
  @user ||= @store.user
end

#visitObject



93
94
95
# File 'lib/ahoy/tracker.rb', line 93

def visit
  @visit ||= @store.visit
end

#visit_or_createObject



97
98
99
# File 'lib/ahoy/tracker.rb', line 97

def visit_or_create
  @visit ||= @store.visit_or_create
end

#visit_propertiesObject



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_tokenObject 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_tokenObject 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