Class: Bacchanalytics::AdwordsConversion

Inherits:
Object
  • Object
show all
Includes:
AdwordsConversionTrackingCode, GoogleAnalytics::Base
Defined in:
lib/bacchanalytics/adwords_conversion.rb

Instance Method Summary collapse

Methods included from AdwordsConversionTrackingCode

#adwords_tracking_code

Methods included from GoogleAnalytics::Base

#load_ga_src, #response_source, #should_instrument?

Constructor Details

#initialize(app, conversions = []) ⇒ AdwordsConversion

Returns a new instance of AdwordsConversion.



72
73
74
75
# File 'lib/bacchanalytics/adwords_conversion.rb', line 72

def initialize(app, conversions = [])
  @app = app
  @conversions = conversions
end

Instance Method Details

#call(env) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bacchanalytics/adwords_conversion.rb', line 77

def call(env)
  status, headers, response = @app.call(env)

  if should_instrument?(headers) && (source = response_source(response))
    page = env['REQUEST_URI']
    page.gsub!(/\?.*/, '') if page #remove url parameters

    tracking_code = adwords_tracking_code(page, @conversions)
    return [status, headers, response] if tracking_code.to_s == ""

    new_body = source.sub /<\/[bB][oO][dY][yY]\s*>/, "#{tracking_code}\n</body>"
    headers["Content-Length"] = new_body.length.to_s
    Rack::Response.new(new_body, status, headers).finish
  else
    [status, headers, response]
  end
end