Skip to content

Instantly share code, notes, and snippets.

@t0nylombardi
Created April 19, 2017 13:08
Show Gist options
  • Save t0nylombardi/5f5a5c0a7c05b6b9367c52f54625f833 to your computer and use it in GitHub Desktop.
Save t0nylombardi/5f5a5c0a7c05b6b9367c52f54625f833 to your computer and use it in GitHub Desktop.
pixel_fragment.rb
# app/lib/tracker/rack.rb
module Tracker
class Rack
def initialize(app)
@app = app
end
def call(env)
@req = ::Rack::Request.new(env)
if @req.path_info =~ /tracker.gif/
result = Services::Params.deploy @req.query_string
location = Services::Locations.lookup(@req.ip)
ip_address = location["ip"] || @req.ip
params = {
ip_address: ip_address,
campaign: result[:campaign],
banner_size: result[:banner_size],
content_type: result[:content_type],
city: location["city"],
state: location["region_name"],
user_agent: @req.user_agent,
referral: @req.referer
}
if @pixels = Pixel.create!(params)
[
200, { 'Content-Type' => 'image/gif' },
[File.read(File.join(File.dirname(__FILE__), 'tracker.gif'))]
]
else
Rails.logger.warn "\n\n Failed to create record on:#{Date.today}"
end
else
@app.call(env)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment