Created
February 6, 2014 00:48
-
-
Save brentfisher/8836436 to your computer and use it in GitHub Desktop.
Simple Pixel Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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