class Rack::Utm
Rack
Middleware for extracting information from the request params and cookies. It populates +env+, # +env+ and +env if it detects a request came from an affiliated link
Constants
- COOKIE_CAMPAIGN
- COOKIE_CONTENT
- COOKIE_FROM
- COOKIE_LP
- COOKIE_MEDIUM
- COOKIE_SOURCE
- COOKIE_TERM
- COOKIE_TIME
Public Class Methods
new(app, opts = {})
click to toggle source
# File lib/rack-utm.rb, line 19 def initialize(app, opts = {}) @app = app @key_param = "utm_source" @cookie_ttl = opts[:ttl] || 60*60*24*30 # 30 days @cookie_domain = opts[:domain] || nil @allow_overwrite = opts[:overwrite].nil? ? true : opts[:overwrite] end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack-utm.rb, line 27 def call(env) req = Rack::Request.new(env) params_tag = req.params[@key_param] cookie_tag = req.cookies[COOKIE_SOURCE] if cookie_tag source, medium, term, content, campaign, from, time, lp = cookie_info(req) end if params_tag && params_tag != cookie_tag if source if @allow_overwrite source, medium, term, content, campaign, from, time, lp = params_info(req) end else source, medium, term, content, campaign, from, time, lp = params_info(req) end end if source env["utm.source"] = source env['utm.medium'] = medium env['utm.term'] = term env['utm.content'] = content env['utm.campaign'] = campaign env['utm.from'] = from env['utm.time'] = time env['utm.lp'] = lp end status, headers, body = @app.call(env) if source != cookie_tag bake_cookies(headers, source, medium, term, content, campaign, from, time, lp) end [status, headers, body] end
params_info(req)
click to toggle source
# File lib/rack-utm.rb, line 72 def params_info(req) [ req.params["utm_source"], req.params["utm_medium"], req.params["utm_term"], req.params["utm_content"], req.params["utm_campaign"], req.env["HTTP_REFERER"], Time.now.to_i, req.path ] end
utm_info(req)
click to toggle source
# File lib/rack-utm.rb, line 68 def utm_info(req) params_info(req) || cookie_info(req) end