Skip to content

Instantly share code, notes, and snippets.

@brentfisher
Created February 6, 2014 00:48
Show Gist options
  • Save brentfisher/8836436 to your computer and use it in GitHub Desktop.
Save brentfisher/8836436 to your computer and use it in GitHub Desktop.
Simple Pixel Middleware
module CBVisitorTracker
module MiddleWare
class PixelWriter
def initialize(appl)
@app = appl
end
def call(env)
response= @app.call(env)
status,headers,body= response
headers['Content-Length']= body.length.to_s
return [status,headers,body] if headers["Content-Type"] !~ /text\/html|application\/xhtml\+xml/
imagetag = %Q!<img src='http://localhost:8081/track/?event=wat' />!
resp = Rack::Response.new
index = body.body.first.rindex("</body>")
body.body.first.insert index, imagetag
resp.body = body.body
resp.status = status
resp.finish
# This works defaulty below
# [status,headers,body]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment