Module: HeapAPI::Helpers

Included in:
Client
Defined in:
lib/heap-helpers/helpers.rb

Overview

Helper tags.

Constant Summary collapse

JS_SNIPPET =

The fragments that make up the Heap JS snippet.

File.read(File.join(File.dirname(__FILE__), 'snippet.js')).
split('$$args')
JS_OPTIONS =

Maps Ruby-esque snake_case heap.js options to JS-esque CamelCase options.

{
  :force_ssl => 'forceSSL',
  :secure_cookie => 'secureCookie',
  :disable_text_capture => 'disableTextCapture',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#js_optionsHash<Symbol, Object>

Returns default heap.js advanced options.

Returns:

  • (Hash<Symbol, Object>)

    default heap.js advanced options

See Also:

  • Client#javascript_tag


41
42
43
# File 'lib/heap-helpers/helpers.rb', line 41

def js_options
  @js_options
end

Instance Method Details

#javascript_tag(options = {}) ⇒ ActiveSupport::SafeBuffer

Generates a <script> tag that includes and configures heap.js.

The tag should be inserted before the closing </head> tag on each page of the site.

Parameters:

  • options (Hash) (defaults to: {})

    advanced configuration for heap.js

Options Hash (options):

  • :force_ssl (Boolean)

    when set to true, heap.js will only use SSL to send data to our collection endpoints; defaults to false

  • :secure_cookie (Boolean)

    when set to true, user cookies in heap.js will only be transmitted via SSL; defaults to false

  • :disable_text_capture (Boolean)

    when set to true, heap.js will not capture the text content of elements; defaults to false

Returns:

  • (ActiveSupport::SafeBuffer)

    <script> tag that includes and configures heap.js

See Also:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/heap-helpers/helpers.rb', line 18

def javascript_tag(options = {})
  ensure_valid_app_id!
  app_string = app_id.inspect

  option_strings = []
  options.merge(js_options).each do |key, value|
    unless js_key = JS_OPTIONS[key]
      raise ArgumentError, "Invalid heap.js advanced option #{key.inspect}"
    end
    option_strings << "#{js_key}:#{!!value}"
  end

  if option_strings.empty?
    js_args = app_string
  else
    js_args = "#{app_string},{#{option_strings.join(',')}}"
  end

  JS_SNIPPET.join(js_args).html_safe
end