Class: Land::Tracker

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

Constant Summary collapse

TRACKING_PARAMS =
{
  'ad_group'       => %w[ad_group adgroup adset_name ovadgrpid ysmadgrpid],
  'ad_type'        => %w[ad_type adtype],
  'affiliate'      => %w[affiliate aff affid],
  'app'            => %w[aid],
  'bid_match_type' => %w[bidmatchtype bid_match_type bmt],
  'brand'          => %w[brand brand_name],
  'campaign'       => %w[campaign campaign_name utm_campaign ovcampgid ysmcampgid cn],
  'click_id'       => %w[click_id clickid dclid fbclid gclid gclsrc msclkid zanpid],
  'content'        => %w[content ad_name utm_content cc],
  'creative'       => %w[creative adid ovadid],
  'device_type'    => %w[device_type devicetype device],
  '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 cm],
  'network'        => %w[network anid],
  'placement'      => %w[placement],
  'position'       => %w[position adposition ad_position],
  'search_term'    => %w[search_term searchterm q querystring ovraw ysmraw],
  'source'         => %w[source utm_source cs],
  'subsource'      => %w[subsource subid sid effi_id customid afftrack pubref argsite fobs epi ws u1],
  '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' },

  'source'         => { 'fb'  => 'facebook',
                        'ig'  => 'instagram',
                        'msg' => 'messenger',
                        'an'  => 'audience network' }
}.freeze
TRACKED_PARAMS =
TRACKING_PARAMS.values.flatten.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 =

Compact the session by shortening names

{
  visit_id:         'vid',
  visit_time:       'vt',
  user_agent_hash:  'uh',
  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.



114
115
116
117
118
119
120
121
# File 'lib/land/tracker.rb', line 114

def initialize(controller)
  # Allow subclasses to super from initialize
  fail NotImplementedError, "You must subclass Land::Tracker" if self.class == Tracker
  @controller = controller
  @events     = []
  @start_time = Time.now
  @status     = nil
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



96
97
98
# File 'lib/land/tracker.rb', line 96

def controller
  @controller
end

#eventsObject (readonly)

Returns the value of attribute events.



96
97
98
# File 'lib/land/tracker.rb', line 96

def events
  @events
end

#statusObject

Returns the value of attribute status.



94
95
96
# File 'lib/land/tracker.rb', line 94

def status
  @status
end

#visitObject (readonly)

Returns the value of attribute visit.



96
97
98
# File 'lib/land/tracker.rb', line 96

def visit
  @visit
end

Class Method Details

.for(controller) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/land/tracker.rb', line 102

def for(controller)
  type = Land::UserAgent[controller.request.user_agent].try(:user_agent_type)
  type = 'noop' unless Land.config.enabled
  type = 'user' if type.nil?

  # override
  type = 'user' if controller.request.query_parameters.with_indifferent_access.slice(*TRACKING_KEYS).any?

  "Land::Trackers::#{type.classify}Tracker".constantize.new(controller)
end

Instance Method Details

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



123
124
125
126
127
128
129
# File 'lib/land/tracker.rb', line 123

def create_event(type, meta = {})
  return unless tracking?

  Event.new(visit_id: @visit_id, event_type: type, meta: meta).tap do |event|
    @events << event
  end
end

#trackObject



131
132
133
# File 'lib/land/tracker.rb', line 131

def track
  fail NotImplementedError, "You must subclass Land::Tracker" if self.class == Tracker
end