Sha256: 3b7702e02a6048539eb24d7539a7767ef8f775f93ce378db4c6653633d912f9d

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

module Analytical
  module Console
    class Api
      include Analytical::Base::Api

      def initialize(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

      def alias_identity(old_identity,new_identity)
        check_for_console <<-HERE
        console.log("Analytical Alias: #{old_identity} => #{new_identity}");
        HERE
      end

      private

      CONSOLE_JS_ESCAPE_MAP = {
        '\\' => '\\\\',
        '</' => '<\/',
        "\r\n" => '\n',
        "\n" => '\n',
        "\r" => '\n',
        '"' => '\\"',
        "'" => "\\'"
      } unless defined?(CONSOLE_JS_ESCAPE_MAP)

      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

2 entries across 2 versions & 1 rubygems

Version Path
analytical-1.8.0 lib/analytical/console.rb
analytical-1.7.0 lib/analytical/console.rb