Created
June 30, 2010 06:19
-
-
Save amerine/458309 to your computer and use it in GitHub Desktop.
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 Rack | |
| class Clicky | |
| TRACKING_CODE = <<-EOTC | |
| <script src="http://static.getclicky.com/js" type="text/javascript"></script> | |
| <script type="text/javascript">clicky.init({{CODE}});</script> | |
| <noscript><p><img alt="Clicky" width="1" height="1" src="http://in.getclicky.com/{{CODE}}ns.gif" /></p></noscript> | |
| EOTC | |
| def initialize(app, site_id, options = {}) | |
| @app = app | |
| @site_id = site_id | |
| end | |
| def call(env) | |
| dup._call(env) | |
| end | |
| def _call(env) | |
| @status, @headers, @response = @app.call(env) | |
| return [@status, @headers, @response] unless @headers['Content-Type'] =~ /html/ | |
| @headers.delete('Content-Length') | |
| response = Rack::Response.new([], @status, @headers) | |
| @response.each do |fragment| | |
| response.write(inject_tracking(fragment)) | |
| end | |
| response.finish | |
| end | |
| def inject_tracking(response) | |
| response.sub!(/<\/body>/, "#{track_code}\n</body>") | |
| end | |
| private | |
| def track_code | |
| TRACKING_CODE.sub(/\{\{CODE\}\}/, @site_id) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment