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



51
52
53
54
55
56
57
58
59
60
# File 'lib/ahoy/tracker.rb', line 51

def authenticate(user)
  if exclude?
    debug "Authentication excluded"
  else
    @store.authenticate(user)
  end
  true
rescue => e
  report_exception(e)
end

#new_visit?Boolean

Returns:

  • (Boolean)


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

def new_visit?
  !existing_visit_token
end

#new_visitor?Boolean

Returns:

  • (Boolean)


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

def new_visitor?
  !existing_visitor_token
end


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

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


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

def set_visitor_cookie
  if new_visitor?
    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
25
26
27
28
# File 'lib/ahoy/tracker.rb', line 12

def track(name, properties = {}, options = {})
  if exclude?
    debug "Event excluded"
  elsif missing_params?
    debug "Missing required parameters"
  else
    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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ahoy/tracker.rb', line 30

def track_visit(options = {})
  if exclude?
    debug "Visit excluded"
  elsif missing_params?
    debug "Missing required parameters"
  else
    if options[:defer]
      set_cookie("ahoy_track", true, nil, false)
    else
      options = options.dup

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

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

#userObject



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

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

#visitObject



62
63
64
# File 'lib/ahoy/tracker.rb', line 62

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

#visit_idObject



66
67
68
# File 'lib/ahoy/tracker.rb', line 66

def visit_id
  @visit_id ||= ensure_uuid(visit_token_helper)
end

#visit_propertiesObject

TODO better name



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

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

#visit_tokenObject



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

def visit_token
  @visit_token ||= ensure_token(visit_token_helper)
end

#visitor_idObject



70
71
72
# File 'lib/ahoy/tracker.rb', line 70

def visitor_id
  @visitor_id ||= ensure_uuid(visitor_token_helper)
end

#visitor_tokenObject



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

def visitor_token
  @visitor_token ||= ensure_token(visitor_token_helper)
end