Class: Landable::Traffic::Tracker

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

Constant Summary collapse

TRACKING_PARAMS =
{
  'ad_type'        => %w(ad_type adtype),
  'ad_group'       => %w(ad_group adgroup ovadgrpid ysmadgrpid),
  'bid_match_type' => %w(bidmatchtype bid_match_type bmt),
  'campaign'       => %w(campaign utm_campaign ovcampgid ysmcampgid),
  'content'        => %w(content utm_content),
  'creative'       => %w(creative adid ovadid),
  'device_type'    => %w(device_type devicetype device),
  'click_id'       => %w(gclid click_id clickid),
  'experiment'     => %w(experiment aceid),
  'keyword'        => %w(keyword kw utm_term ovkey ysmkey),
  'match_type'     => %w(match_type matchtype match ovmtc ysmmtc),
  'medium'         => %w(medium utm_medium),
  'network'        => %w(network),
  'placement'      => %w(placement),
  'position'       => %w(position adposition ad_position),
  'search_term'    => %w(search_term searchterm q querystring ovraw ysmraw),
  'source'         => %w(source utm_source),
  'target'         => %w(target)
}.freeze
TRACKING_KEYS =
TRACKING_PARAMS.values.flatten.freeze
ATTRIBUTION_KEYS =
TRACKING_PARAMS.except('click_id').keys
TRACKING_PARAMS_TRANSFORM =
{
  'ad_type' => { 'pe'  => 'product_extensions',
                 'pla' => 'product_listing' },

  'bid_match_type' => { 'bb'  => 'bidded broad',
                        'bc'  => 'bidded content',
                        'be'  => 'bidded exact',
                        'bp'  => 'bidded phrase' },

  'device_type' => { 'c'   => 'computer',
                     'm'   => 'mobile',
                     't'   => 'tablet' },

  'match_type' => { 'b'   => 'broad',
                    'c'   => 'content',
                    'e'   => 'exact',
                    'p'   => 'phrase',
                    'std' => 'standard',
                    'adv' => 'advanced',
                    'cnt' => 'content' },

  'network' => { 'g'   => 'google_search',
                 's'   => 'search_partner',
                 'd'   => 'display_network' }
}.freeze
UUID_REGEX =
/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\Z/
UUID_REGEX_V4 =
/\A\h{8}-\h{4}-4\h{3}-[89aAbB]\h{3}-\h{12}\Z/
KEYS =

Save space in the session by shortening names

{
  visit_id:         'vid',
  visitor_id:       'vsid',
  visit_time:       'vt',
  visitor_hash:     'vh',
  attribution_hash: 'ah',
  referer_hash:     'rh'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Tracker

Returns a new instance of Tracker.



86
87
88
89
90
91
# File 'lib/landable/traffic/tracker.rb', line 86

def initialize(controller)
  # Allow subclasses to super from initialize
  fail NotImplementedError, 'You must subclass Tracker' if self.class == Tracker
  @controller = controller
  @start_time = Time.now
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
end

Class Method Details

.for(controller) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/landable/traffic/tracker.rb', line 76

def for(controller)
  type = controller.request.user_agent.presence && Landable::Traffic::UserAgent[controller.request.user_agent].user_agent_type
  type = 'noop' if Landable.configuration.traffic_enabled == :html && !controller.request.format.html?
  type = 'user' if type.nil?
  type = 'user' if controller.request.query_parameters.with_indifferent_access.slice(*TRACKING_KEYS).any?

  "Landable::Traffic::#{type.classify}Tracker".constantize.new(controller)
end

Instance Method Details

#create_event(type, meta = {}) ⇒ Object



102
103
104
105
# File 'lib/landable/traffic/tracker.rb', line 102

def create_event(type, meta = {})
  return unless @visit_id
  Event.create(visit_id: @visit_id, event_type: type, meta: meta)
end

#get_user_agentObject

TODO: Is this used in multiple applications outside Landable. If not, then lets get rid of this method. rubocop:disable Style/AccessorMethodName



126
127
128
# File 'lib/landable/traffic/tracker.rb', line 126

def get_user_agent
  user_agent
end

#landing_pathObject



119
120
121
# File 'lib/landable/traffic/tracker.rb', line 119

def landing_path
  @visit_id && PageView.where(visit_id: @visit_id).order(:page_view_id).first.try(:path)
end

#trackObject



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

def track
  fail NotImplementedError, 'You must subclass Tracker' if self.class == Tracker
end

#visit_referer_domainObject



107
108
109
# File 'lib/landable/traffic/tracker.rb', line 107

def visit_referer_domain
  visit.referer.try(:uri).try(:host)
end

#visit_referer_pathObject



111
112
113
# File 'lib/landable/traffic/tracker.rb', line 111

def visit_referer_path
  visit.referer.try(:uri).try(:path)
end

#visit_referer_urlObject



115
116
117
# File 'lib/landable/traffic/tracker.rb', line 115

def visit_referer_url
  visit.referer.try(:url)
end

#visitor_idObject



97
98
99
100
# File 'lib/landable/traffic/tracker.rb', line 97

def visitor_id
  @visitor_id = visitor.id if visitor_changed?
  @visitor_id
end