module Analytical module Console class Api include Analytical::Base::Api def initialize(parent, options={}) super @tracking_command_location = :body_append end def init_javascript(location) init_location(location) do js = <<-HTML HTML js end end def track(*args) check_for_console <<-HERE console.log("Analytical Track: "+"#{escape args.first}"); HERE end def identify(id, *args) data = args.first || {} check_for_console <<-HERE console.log("Analytical Identify: "+"#{escape id}"); console.log(#{data.to_json}); HERE end def event(name, *args) data = args.first || {} check_for_console <<-HERE console.log("Analytical Event: "+"#{escape name}"); console.log(#{data.to_json}); HERE end def set(data) check_for_console <<-HERE console.log("Analytical Set: "); console.log(#{data.to_json}); HERE end private CONSOLE_JS_ESCAPE_MAP = { '\\' => '\\\\', ' '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" } def escape(js) js.to_s.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { CONSOLE_JS_ESCAPE_MAP[$1] } end def check_for_console(data) "if(typeof(console) !== 'undefined' && console != null) { #{data} }" end end end end