Class: Landable::Traffic::UserTracker

Inherits:
Tracker
  • Object
show all
Defined in:
lib/landable/traffic/user_tracker.rb

Constant Summary

Constants inherited from Tracker

Tracker::ATTRIBUTION_KEYS, Tracker::KEYS, Tracker::TRACKING_KEYS, Tracker::TRACKING_PARAMS, Tracker::TRACKING_PARAMS_TRANSFORM, Tracker::UUID_REGEX, Tracker::UUID_REGEX_V4

Instance Attribute Summary

Attributes inherited from Tracker

#controller

Instance Method Summary collapse

Methods inherited from Tracker

#create_event, for, #get_user_agent, #initialize, #landing_path, #visit_referer_domain, #visit_referer_path, #visit_referer_url, #visitor_id

Constructor Details

This class inherits a constructor from Landable::Traffic::Tracker

Instance Method Details

#identify(identifier) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/landable/traffic/user_tracker.rb', line 56

def identify(identifier)
  visit = Visit.find(@visit_id)
  begin
    owner = Owner.where(owner: identifier).first_or_create
  rescue ActiveRecord::RecordNotUnique
    retry
  end

  visit.owner = owner
  visit.save!

  begin
    Ownership.where(cookie_id: @cookie_id, owner: owner).first_or_create
  rescue ActiveRecord::RecordNotUnique
    retry
  end
end

#loadObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/landable/traffic/user_tracker.rb', line 10

def load
  hash = session[:landable] || {}

  @cookie_id        = cookies[:landable]

  @visit_id         = hash[KEYS[:visit_id]]
  @last_visit_time  = hash[KEYS[:visit_time]].try :to_time
  @visitor_id       = hash[KEYS[:visitor_id]]
  @visitor_hash     = hash[KEYS[:visitor_hash]]
  @attribution_hash = hash[KEYS[:attribution_hash]]
  @referer_hash     = hash[KEYS[:referer_hash]]
end

#record_page_viewObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/landable/traffic/user_tracker.rb', line 23

def record_page_view
  PageView.create do |p|
    p.http_method  = request.method
    p.mime_type    = request.format.to_s
    p.path         = request.path
    p.query_string = untracked_parameters.to_query
    p.request_id   = request.uuid

    p.click_id     = tracking_parameters['click_id']

    p.http_status  = response.status

    p.visit_id     = @visit_id
    current_time = Time.now
    p.created_at   = current_time
    p.response_time = (current_time - @start_time) * 1000
  end
end

#saveObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/landable/traffic/user_tracker.rb', line 42

def save
  p = record_page_view
  EventPublisher.publish(p) if Landable.configuration.amqp_service_enabled

  session[:landable] = {
    KEYS[:visit_id]         => @visit_id,
    KEYS[:visit_time]       => Time.current,
    KEYS[:visitor_id]       => @visitor_id,
    KEYS[:visitor_hash]     => visitor_hash,
    KEYS[:attribution_hash] => attribution? ? attribution_hash : @attribution_hash,
    KEYS[:referer_hash]     => referer_changed? ? referer_hash : @referer_hash
  }
end

#trackObject



4
5
6
7
8
# File 'lib/landable/traffic/user_tracker.rb', line 4

def track
  load
  cookie
  record_visit
end