Skip to content

Commit

Permalink
track with js when response is text/javascript or text/html
Browse files Browse the repository at this point in the history
  • Loading branch information
zevarito committed Jul 14, 2010
1 parent 48885e4 commit 1251f6b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
32 changes: 22 additions & 10 deletions lib/mixpanel/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ def call(env)

def build_response!
@response.each do |part|
if is_html?
part.gsub!("</head>", "#{render_mixpanel_scripts}</head>") if !is_ajax?
if is_regular_request? && is_html_response?
part.gsub!("</head>", "#{render_mixpanel_scripts}</head>")
part.gsub!("</head>", "#{render_event_tracking_scripts}</head>")
elsif is_ajax_request? && is_html_response?
part.gsub!(part, render_event_tracking_scripts + part)
elsif is_ajax_request? && is_javascript_response?
part.gsub!(part, render_event_tracking_scripts(false) + part)
end
end
end
Expand All @@ -32,14 +36,22 @@ def update_content_length!
@headers.merge!("Content-Length" => @response.join("").length.to_s)
end

def is_ajax?
def is_regular_request?
!is_ajax_request?
end

def is_ajax_request?
@env.has_key?("HTTP_X_REQUESTED_WITH") && @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
end

def is_html?
def is_html_response?
@headers["Content-Type"].include?("text/html") if @headers.has_key?("Content-Type")
end

def is_javascript_response?
@headers["Content-Type"].include?("text/javascript") if @headers.has_key?("Content-Type")
end

def render_mixpanel_scripts
<<-EOT
<script type='text/javascript'>
Expand All @@ -64,13 +76,13 @@ def queue
@env['mixpanel_events']
end

def render_event_tracking_scripts
def render_event_tracking_scripts(include_script_tag=true)
return "" if queue.empty?

<<-EOT
<script type='text/javascript'>
#{queue.map {|event| %(mpmetrics.track("#{event[:event]}", #{event[:properties].to_json});) }.join("\n")}
</script>
EOT
output = queue.map {|event| %(mpmetrics.track("#{event[:event]}", #{event[:properties].to_json});) }.join("\n")

output = "try {#{output}} catch(err) {}"

include_script_tag ? "<script type='text/javascript'>#{output}</script>" : output
end
end
24 changes: 23 additions & 1 deletion spec/mixpanel/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

describe "With ajax requests and text/html response" do
before do
setup_rack_application(DummyApp, :body => html_document, :headers => {"Content-Type" => "text/html"})
setup_rack_application(DummyApp, :body => "<p>response</p>", :headers => {"Content-Type" => "text/html"})

get "/", {}, {"mixpanel_events" => @mixpanel.queue, "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
end
Expand All @@ -79,18 +79,40 @@

it "should be tracking the correct events inside a script tag" do
script = Nokogiri::HTML(last_response.body).search('script')
script.inner_html.should =~ /try\s?\{(.*)\}\s?catch/m
script.inner_html.should =~ /mpmetrics\.track\("Visit",\s?\{"article":1\}\)/
script.inner_html.should =~ /mpmetrics\.track\("Sign in",\s?\{\}\)/
end
end

describe "With ajax requests and text/javascript response" do
before do
setup_rack_application(DummyApp, :body => "alert('response')", :headers => {"Content-Type" => "text/javascript"})
get "/", {}, {"mixpanel_events" => @mixpanel.queue, "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"}
end

it "should not render a script tag" do
Nokogiri::HTML(last_response.body).search('script').size.should == 0
end

it "should be tracking the correct events inside a try/catch" do
script = last_response.body.match(/try\s?\{(.*)\}\s?catch/m)[1]
script.should =~ /mpmetrics\.track\("Visit",\s?\{"article":1\}\)/
script.should =~ /mpmetrics\.track\("Sign in",\s?\{\}\)/
end
end

describe "With regular requests" do
before do
setup_rack_application(DummyApp, :body => html_document, :headers => {"Content-Type" => "text/html"})

get "/", {}, {"mixpanel_events" => @mixpanel.queue}
end

it "should render 3 script tags" do
Nokogiri::HTML(last_response.body).search('script').size.should == 3
end

it "should be tracking the correct events" do
last_response.body.should =~ /mpmetrics\.track\("Visit",\s?\{"article":1\}\)/
last_response.body.should =~ /mpmetrics\.track\("Sign in",\s?\{\}\)/
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'ruby-debug'
require File.join(File.dirname(__FILE__), "../lib", "mixpanel")
require 'rack/test'
require 'fakeweb'
Expand Down

0 comments on commit 1251f6b

Please sign in to comment.