Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rack middleware to append mixpanel scripts
- Loading branch information
Showing
6 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
require 'mixpanel/mixpanel' | ||
require 'mixpanel/middleware' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'rack' | ||
|
||
class Middleware | ||
def initialize(app, mixpanel_token) | ||
@app = app | ||
@token = mixpanel_token | ||
end | ||
|
||
def call(env) | ||
status, headers, response = @app.call(env) | ||
|
||
if is_html?(headers) | ||
response = build_response(response) | ||
headers = update_content_length(response, headers) | ||
end | ||
|
||
[status, headers, response] | ||
end | ||
|
||
def each(&block) | ||
@response.each(&block) | ||
end | ||
|
||
private | ||
|
||
def build_response(response) | ||
body = "" | ||
|
||
response.each do |part| | ||
part.gsub!("</head>", "#{include_mixpanel_scripts}</head>") | ||
body << part | ||
end | ||
end | ||
|
||
def update_content_length(response, headers) | ||
headers.merge("Content-Length" => response.join("").length.to_s) | ||
end | ||
|
||
def is_html?(headers) | ||
headers["Content-Type"].include?("text/html") if headers.has_key?("Content-Type") | ||
end | ||
|
||
def include_mixpanel_scripts | ||
<<-EOT | ||
<script type='text/javascript'> | ||
var mp_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://'); | ||
document.write(unescape('%3Cscript src="' + mp_protocol + 'api.mixpanel.com/site_media/js/api/mixpanel.js" type="text/javascript"%3E%3C/script%3E')); | ||
</script> | ||
<script type='text/javascript'> | ||
try { | ||
var mpmetrics = new MixpanelLib('#{@token}'); | ||
} catch(err) { | ||
null_fn = function () {}; | ||
var mpmetrics = { | ||
track: null_fn, track_funnel: null_fn, register: null_fn, register_once: null_fn, register_funnel: null_fn | ||
}; | ||
} | ||
</script> | ||
EOT | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'spec_helper' | ||
|
||
describe Middleware do | ||
include Rack::Test::Methods | ||
|
||
describe "Dummy apps, no text/html" do | ||
before do | ||
setup_rack_application(DummyApp) | ||
get "/" | ||
end | ||
|
||
it "should pass through if the document is not text/html content type" do | ||
last_response.body.should == DummyApp.new.body | ||
end | ||
end | ||
|
||
describe "Appending mixpanel scripts" do | ||
before do | ||
setup_rack_application(HtmlApp) | ||
get "/" | ||
end | ||
|
||
it "should append mixpanel scripts to head element" do | ||
Nokogiri::HTML(last_response.body).search('head script').should_not be_empty | ||
end | ||
|
||
it "should use the specified token instantiating mixpanel lib" do | ||
last_response.should =~ /new MixpanelLib\('#{MIX_PANEL_TOKEN}'\)/ | ||
end | ||
|
||
it "should define Content-Length if not exist" do | ||
last_response.headers.has_key?("Content-Length").should == true | ||
end | ||
|
||
it "should update Content-Length in headers" do | ||
last_response.headers["Content-Length"].should_not == HtmlApp.new.body.length | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
def setup_rack_application(application) | ||
stub!(:app).and_return(Middleware.new(application.new, MIX_PANEL_TOKEN)) | ||
end | ||
|
||
class HtmlApp | ||
def body | ||
<<-EOT | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
EOT | ||
end | ||
|
||
def call(env) | ||
["200", {"Content-Type" => "text/html"}, [body]] | ||
end | ||
end | ||
|
||
class DummyApp | ||
def body | ||
"" | ||
end | ||
|
||
def call(env) | ||
["200", {}, [body]] | ||
end | ||
end |