Module: Workarea::CurrentTracking

Extended by:
ActiveSupport::Concern
Includes:
HttpCaching
Included in:
ApplicationController, CurrentSegments
Defined in:
app/controllers/workarea/current_tracking.rb

Instance Method Summary collapse

Methods included from HttpCaching

#cache_page, #http_caching?

Instance Method Details

#current_metricsObject



18
19
20
# File 'app/controllers/workarea/current_tracking.rb', line 18

def current_metrics
  current_visit.metrics
end

#current_referrerObject



22
23
24
# File 'app/controllers/workarea/current_tracking.rb', line 22

def current_referrer
  current_visit.referrer
end

#current_visitObject



14
15
16
# File 'app/controllers/workarea/current_tracking.rb', line 14

def current_visit
  request.env['workarea.visit']
end

#ensure_current_metricsObject

This method is mostly for tests, but could surface in an implementation. Since Rails doesn’t set and load a session until needed, an initial request won’t have a session ID for ‘current_visit` to check when loading metrics. This ensures there will be one.



41
42
43
44
45
46
47
# File 'app/controllers/workarea/current_tracking.rb', line 41

def ensure_current_metrics
  return if current_metrics_id.present? || http_caching?

  # This forces Rails to initialize the session, which provides an ID for metrics
  session.delete(:foo)
  self.current_metrics_id = session.id.cookie_value
end

#update_tracking!(email: current_user&.email) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/workarea/current_tracking.rb', line 26

def update_tracking!(email: current_user&.email)
  if email.blank?
    cookies.delete(:email)
  elsif email != cookies.signed[:email]
    Metrics::User.find_or_initialize_by(id: email).merge!(current_visit&.metrics)
    cookies.permanent.signed[:email] = email
  end

  request.env['workarea.visit'] = Visit.new(request.env)
end