class Mengpaneel::Strategy::ClientSide

Constants

SETUP_CODE

Public Instance Methods

run() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 14
def run
  return false unless controller
  return false unless client_side?

  response_body = response.body

  head_end = response_body.index("</head")
  return false unless head_end

  if source = source_for_head
    response_body.insert(head_end, source)
  end

  body_end = response_body.index("</body")
  return false unless body_end

  if source = source_for_body
    response_body.insert(body_end, source)
  end

  response.body = response_body

  true
end

Private Instance Methods

attachment?() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 82
def attachment?
  response.headers["Content-Disposition"].to_s.include?("attachment")
end
client_side?() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 74
def client_side?
  html? && !request.xhr? && !attachment? && !streaming?
end
html?() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 78
def html?
  response.content_type && response.content_type.include?("text/html")
end
javascript_calls(mode) click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 63
def javascript_calls(mode)
  calls = all_calls[mode] || []

  calls.map do |method_names, args|
    method_name = method_names.join(".")
    arguments   = args.map(&:to_json).join(", ")

    %{mixpanel.#{method_name}(#{arguments});}
  end
end
source_for_body() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 53
def source_for_body
  return if all_calls[:tracking].blank?

  [
    %{<script type="text/javascript">},
      *javascript_calls(:tracking),
    %{</script>}
  ].join("\n")
end
source_for_head() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 40
def source_for_head
  [
    %{<script type="text/javascript">},
      SETUP_CODE,

      %{mixpanel.init(#{Mengpaneel.token.to_json});},
      
      *javascript_calls(:before_setup),
      *javascript_calls(:setup),
    %{</script>}
  ].join("\n")
end
streaming?() click to toggle source
# File lib/mengpaneel/strategy/client_side.rb, line 86
def streaming?
  return false unless defined?(ActionController::Live)

  env["action_controller.instance"].class.included_modules.include?(ActionController::Live)
end