Sha256: 1a0dfa1f840580be1ca68bf28e70382a8b52f5443af1bf482babd665687e67ed

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 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 "console.log(\"Analytical Track: \"+\"#{escape args.first}\");"
      end

      def identify(id, *args)
        data = args.first || {}
        check_for_console "console.log(\"Analytical Identify: \"+\"#{id}\"+\" \"+$H(#{data.to_json}).toJSON());"
      end

      def event(name, *args)
        data = args.first || {}
        check_for_console "console.log(\"Analytical Event: \"+\"#{name}\"+\" \"+$H(#{data.to_json}).toJSON());"
      end

      def set(data)
        check_for_console "console.log(\"Analytical Set: \"+$H(#{data.to_json}).toJSON());"
      end

      private

      CONSOLE_JS_ESCAPE_MAP = {
        '\\' => '\\\\',
        '</' => '<\/',
        "\r\n" => '\n',
        "\n" => '\n',
        "\r" => '\n',
        '"' => '\\"',
        "'" => "\\'"
      }

      def escape(js)
        js.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.4.0 lib/analytical/console.rb
analytical-1.3.0 lib/analytical/console.rb