lib/mixpanel/middleware.rb in mixpanel-3.4.0 vs lib/mixpanel/middleware.rb in mixpanel-3.5.0

- old
+ new

@@ -36,10 +36,12 @@ insert_at = part.index(@options[:insert_js_last] ? '</body' : '</head') unless insert_at.nil? part.insert(insert_at, render_event_tracking_scripts) unless queue.empty? part.insert(insert_at, render_mixpanel_scripts) #This will insert the mixpanel initialization code before the queue of tracking events. end + elsif is_turbolink_request? && is_html_response? + part.insert(part.index('</body'), render_event_tracking_scripts) unless queue.empty? elsif is_ajax_request? && is_html_response? part.insert(0, render_event_tracking_scripts) unless queue.empty? elsif is_ajax_request? && is_javascript_response? part.insert(0, render_event_tracking_scripts(false)) unless queue.empty? end @@ -51,13 +53,17 @@ @response.each{|part| new_size += part.bytesize} @headers.merge!("Content-Length" => new_size.to_s) end def is_regular_request? - !is_ajax_request? + !is_ajax_request? && !is_turbolink_request? end + def is_turbolink_request? + @env.has_key?("HTTP_X_XHR_REFERER") + end + def is_ajax_request? @env.has_key?("HTTP_X_REQUESTED_WITH") && @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest" end def is_html_response? @@ -131,10 +137,10 @@ def render_event_tracking_scripts(include_script_tag=true) return "" if queue.empty? output = queue.map {|type, arguments| %(mixpanel.#{type}(#{arguments.join(', ')});) }.join("\n") - output = "try {#{output}} catch(err) {}" + output = "try {#{output}} catch(err) {};" include_script_tag ? "<script type='text/javascript'>#{output}</script>" : output end end end