Class: Ahoy::Tracker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tracker

Returns a new instance of Tracker.



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

def initialize(options = {})
  @store = Ahoy::Store.new(options.merge(ahoy: self))
  @controller = options[:controller]
  @request = options[:request] || @controller.try(:request)
  @options = options
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



3
4
5
# File 'lib/ahoy/tracker.rb', line 3

def controller
  @controller
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/ahoy/tracker.rb', line 3

def request
  @request
end

Instance Method Details

#authenticate(user) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/ahoy/tracker.rb', line 43

def authenticate(user)
  unless exclude?
    @store.authenticate(user)
  end
  true
rescue => e
  report_exception(e)
end

#new_visit?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/ahoy/tracker.rb', line 64

def new_visit?
  !existing_visit_id
end


68
69
70
# File 'lib/ahoy/tracker.rb', line 68

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


72
73
74
75
76
# File 'lib/ahoy/tracker.rb', line 72

def set_visitor_cookie
  if !existing_visitor_id
    set_cookie("ahoy_visitor", visitor_id, Ahoy.visitor_duration)
  end
end

#track(name, properties = {}, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ahoy/tracker.rb', line 12

def track(name, properties = {}, options = {})
  unless exclude?
    options = options.dup

    options[:time] = trusted_time(options[:time])
    options[:id] = ensure_uuid(options[:id] || generate_id)

    @store.track_event(name, properties, options)
  end
  true
rescue => e
  report_exception(e)
end

#track_visit(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ahoy/tracker.rb', line 26

def track_visit(options = {})
  unless exclude?
    if options[:defer]
      set_cookie("ahoy_track", true)
    else
      options = options.dup

      options[:started_at] ||= Time.zone.now

      @store.track_visit(options)
    end
  end
  true
rescue => e
  report_exception(e)
end

#userObject



78
79
80
# File 'lib/ahoy/tracker.rb', line 78

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

#visitObject



52
53
54
# File 'lib/ahoy/tracker.rb', line 52

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

#visit_idObject



56
57
58
# File 'lib/ahoy/tracker.rb', line 56

def visit_id
  @visit_id ||= ensure_uuid(existing_visit_id || visit_token)
end

#visit_propertiesObject

TODO better name



83
84
85
# File 'lib/ahoy/tracker.rb', line 83

def visit_properties
  @visit_properties ||= Ahoy::VisitProperties.new(request, @options.slice(:api))
end

#visit_tokenObject

for ActiveRecordTokenStore only - do not use



88
89
90
# File 'lib/ahoy/tracker.rb', line 88

def visit_token
  @visit_token ||= existing_visit_id || (@options[:api] && request.params["visit_token"]) || generate_id
end

#visitor_idObject



60
61
62
# File 'lib/ahoy/tracker.rb', line 60

def visitor_id
  @visitor_id ||= ensure_uuid(existing_visitor_id || visitor_token)
end

#visitor_tokenObject

for ActiveRecordTokenStore only - do not use



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

def visitor_token
  @visitor_token ||= existing_visitor_id || (@options[:api] && request.params["visitor_token"]) || generate_id
end