Sha256: 7b1925743c54976955caedf871ece1ec886a08ddefe47dce0625bc6b8d0887b3

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

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
          <!-- Analytical Init: Console -->
          <script type="text/javascript">
            if(typeof(console) !== 'undefined' && console != null) {
              console.log('Analytical Init: Console');
            }
          </script>
          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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
analytical-1.5.0 lib/analytical/console.rb