lib/landable/traffic/tracker.rb in landable-1.13.1 vs lib/landable/traffic/tracker.rb in landable-1.13.2

- old
+ new

@@ -3,57 +3,57 @@ module Landable module Traffic class Tracker 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], + '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 + ATTRIBUTION_KEYS = TRACKING_PARAMS.except('click_id').keys TRACKING_PARAMS_TRANSFORM = { - "ad_type" => { 'pe' => 'product_extensions', + 'ad_type' => { 'pe' => 'product_extensions', 'pla' => 'product_listing' }, - "bid_match_type" => { 'bb' => 'bidded broad', + 'bid_match_type' => { 'bb' => 'bidded broad', 'bc' => 'bidded content', 'be' => 'bidded exact', 'bp' => 'bidded phrase' }, - "device_type" => { 'c' => 'computer', + 'device_type' => { 'c' => 'computer', 'm' => 'mobile', 't' => 'tablet' }, - "match_type" => { 'b' => 'broad', + 'match_type' => { 'b' => 'broad', 'c' => 'content', 'e' => 'exact', 'p' => 'phrase', 'std' => 'standard', 'adv' => 'advanced', 'cnt' => 'content' }, - "network" => { 'g' => 'google_search', + 'network' => { 'g' => 'google_search', 's' => 'search_partner', - 'd' => 'display_network' }, + '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/ @@ -73,37 +73,36 @@ delegate :headers, :path, :query_parameters, :referer, :remote_ip, to: :request class << self 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 and not controller.request.format.html? + 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 end def initialize(controller) # Allow subclasses to super from initialize - raise NotImplementedError, "You must subclass Tracker" if self.class == Tracker + fail NotImplementedError, 'You must subclass Tracker' if self.class == Tracker @controller = controller @start_time = Time.now end def track - raise NotImplementedError, "You must subclass Tracker" if self.class == Tracker + fail NotImplementedError, 'You must subclass Tracker' if self.class == Tracker end def visitor_id @visitor_id = visitor.id if visitor_changed? @visitor_id end def create_event(type, meta = {}) return unless @visit_id - Event.create(visit_id: @visit_id, event_type: type, meta: meta) end def visit_referer_domain visit.referer.try(:uri).try(:host) @@ -116,27 +115,29 @@ def visit_referer_url visit.referer.try(:url) end def landing_path - @visit_id and PageView.where(visit_id: @visit_id).order(:page_view_id).first.try(:path) + @visit_id && PageView.where(visit_id: @visit_id).order(:page_view_id).first.try(:path) end + # TODO: Is this used in multiple applications outside Landable. If not, + # then lets get rid of this method. + # rubocop:disable Style/AccessorMethodName def get_user_agent user_agent end - protected + protected + def cookies request.cookie_jar end def cookie validate_cookie - @cookie_id ||= Cookie.create.id - set_cookie end def validate_cookie return unless @cookie_id @@ -153,13 +154,12 @@ def cookie_defaults { domain: :all, secure: false, httponly: true } end def do_not_track - return unless headers["DNT"] - - headers["DNT"] == "1" + return unless headers['DNT'] + headers['DNT'] == '1' end def user_agent @user_agent ||= UserAgent[request_user_agent] end @@ -170,14 +170,18 @@ params = Rack::Utils.parse_query referer_uri.query attribution = Attribution.lookup params.slice(*ATTRIBUTION_KEYS) query = params.except(*ATTRIBUTION_KEYS) - @referer = Referer.where(domain_id: Domain[referer_uri.host], - path_id: Path[referer_uri_path], - query_string_id: QueryString[query.to_query], - attribution_id: attribution.id).first_or_create + begin + @referer = Referer.where(domain_id: Domain[referer_uri.host], + path_id: Path[referer_uri_path], + query_string_id: QueryString[query.to_query], + attribution_id: attribution.id).first_or_create + rescue ActiveRecord::RecordNotUnique + retry + end end def ip_address @ip_address ||= IpAddress[remote_ip] end @@ -252,25 +256,24 @@ attribution? && attribution_hash != @attribution_hash end def visit_stale? return false unless @last_visit_time - Time.current - @last_visit_time > 30.minutes end def extract_tracking(params) hash = {} TRACKING_PARAMS.each do |key, names| - next unless param = names.find { |name| params.key?(name) } + param = names.find { |name| params.key?(name) } + next unless param hash[key] = params[param] end TRACKING_PARAMS_TRANSFORM.each do |key, transform| next unless hash.key? key - hash[key] = transform[hash[key]] if transform.key? hash[key] end hash end @@ -295,9 +298,11 @@ @attribution ||= Attribution.lookup attribution_parameters end def visitor @visitor ||= Visitor.with_ip_address(ip_address).with_user_agent(user_agent).first_or_create + rescue ActiveRecord::RecordNotUnique + retry end def visit @visit ||= @visit_id && Visit.find(@visit_id) end