Module: UniversalTrackManagerConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#visit_evictedObject

Returns the value of attribute visit_evicted.



3
4
5
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 3

def visit_evicted
  @visit_evicted
end

Instance Method Details

#any_utm_params?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 72

def any_utm_params?
  return false if ! UniversalTrackManager.track_utms?
  UniversalTrackManager.campaign_column_symbols.any? do |key|
    params[key].present?
  end
end

#evict_visit!(old_visit) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 98

def evict_visit!(old_visit)
  @visit_evicted = true
  params = {
    first_pageload: now,
    last_pageload: now,
    original_visit_id: old_visit.original_visit_id.nil? ?  old_visit.id : old_visit.original_visit_id,
    count: old_visit.original_visit_id.nil? ?  old_visit.count + 1 : 1,
    ip_v4_address: ip_address,
    campaign: find_or_create_campaign_by_current
  }

  # fail silently if there is no user agent
  if request.user_agent
    params[:browser] =  find_or_create_browser_by_current
  end

  visit = UniversalTrackManager::Visit.create!(params)

  session[:visit_id] = visit.id
end

#find_or_create_browser_by_currentObject



79
80
81
82
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 79

def find_or_create_browser_by_current
  return nil if ! UniversalTrackManager.track_user_agent?
  browser = UniversalTrackManager::Browser.find_or_create_by(name: user_agent)
end

#find_or_create_campaign_by_currentObject



84
85
86
87
88
89
90
91
92
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 84

def find_or_create_campaign_by_current
  return nil if ! UniversalTrackManager.track_utms?
  gen_sha1 = gen_campaign_key(permitted_utm_params)

  # find_or_create_by finding only by sha1 would be nice here, but how to do so with a dynamic set of columns?
  # we've got a small chance of dups here due to the non-atomic find/create and sha1, but that's ok for this application.
  c = UniversalTrackManager::Campaign.find_by(sha1: gen_sha1)
  c ||= UniversalTrackManager::Campaign.create(*(permitted_utm_params.merge({"sha1": gen_sha1})))
end

#gen_campaign_key(params) ⇒ Object



94
95
96
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 94

def gen_campaign_key(params)
  Digest::SHA1.hexdigest(params.keys.map{|k| k.downcase()}.sort.map{|k| {"#{k}":  params[k]}}.to_s)
end

#ip_addressObject



23
24
25
26
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 23

def ip_address
  return nil if ! UniversalTrackManager.track_ips?
  request.ip
end

#new_visitorObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 37

def new_visitor
  params = {
    first_pageload: now,
    last_pageload: now,
    ip_v4_address: ip_address,
    campaign: find_or_create_campaign_by_current
  }
  params[:browser] =  find_or_create_browser_by_current if request.user_agent
  visit = UniversalTrackManager::Visit.create!(params)
  session[:visit_id] = visit.id
end

#nowObject



33
34
35
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 33

def now
  @now ||= Time.now
end

#permitted_utm_paramsObject



18
19
20
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 18

def permitted_utm_params
  params.permit(*(UniversalTrackManager.campaign_column_symbols))
end

#track_visitorObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 49

def track_visitor
  if !session['visit_id']
    new_visitor
  else
    # existing visit
    begin
      existing_visit = UniversalTrackManager::Visit.find(session['visit_id'])

      evict_visit!(existing_visit) if any_utm_params? && !existing_visit.matches_all_utms?(permitted_utm_params)

      evict_visit!(existing_visit) if existing_visit.ip_v4_address != ip_address
      evict_visit!(existing_visit) if existing_visit.browser && existing_visit.browser.name != user_agent

      existing_visit.update_columns(:last_pageload => Time.now) if !@visit_evicted
    rescue ActiveRecord::RecordNotFound
      # this happens if the session table is cleared or if the record in the session
      # table points to a visit that has been cleared
      new_visitor
    end
  end
end

#user_agentObject



28
29
30
31
# File 'lib/universal_track_manager/controllers/concerns/universal_track_manager_concern.rb', line 28

def user_agent
  return nil if ! UniversalTrackManager.track_user_agent?
  request.user_agent && request.user_agent[0..254]
end