lib/staccato/rack.rb in staccato-rack-0.3.1 vs lib/staccato/rack.rb in staccato-rack-0.3.2

- old
+ new

@@ -3,10 +3,11 @@ require 'rack/request' require 'ostruct' module Staccato module Rack + # Proxy Class to do page views class PageView < OpenStruct def initialize super @custom_metrics = [] @custom_dimensions = [] @@ -31,17 +32,16 @@ end private def track_hit(tracker, page_view_params, request) - hit = Staccato::Pageview.new(tracker, page_view_params.merge(path: request.fullpath, - hostname: request.host, - user_agent: request.env['HTTP_USER_AGENT'], - user_ip: request.ip)) + hit = Staccato::Pageview.new(tracker, { path: request.fullpath, + user_agent: request.env['HTTP_USER_AGENT'], + user_ip: request.ip }.merge(page_view_params)) add_custom_to_hit(hit) r = hit.track! - logger.info "GA Tracking: #{hit.params.inspect} => #{r.response.code if r}" + log_response(r, hit) hit end def add_custom_to_hit(hit) @custom_metrics.each do |p, v| @@ -49,12 +49,17 @@ end @custom_dimensions.each do |p, v| hit.add_custom_dimension(p, v) end end + + def log_response(r, hit) + logger.info "GA Tracking: #{hit.params.inspect} => #{r.response.code if r}" + end end + # Null Logger clas class NullLogger def info(*) end end @@ -70,10 +75,10 @@ @default_tracker = Staccato.tracker(tracking_id) @logger = options[:logger] || NullLogger.new end def call(env) - env['staccato.pageview'] = PageView.new.tap{|p| p.logger = @logger } + env['staccato.pageview'] = PageView.new.tap { |p| p.logger = @logger } @last_hit = nil status, headers, body = @app.call(env) if (200..299).include?(status.to_i)