Module: IntercomRails::ScriptTagHelper

Defined in:
lib/intercom-rails/script_tag_helper.rb

Overview

Helper methods for generating Intercom javascript script tags.

Instance Method Summary (collapse)

Instance Method Details

- (String) intercom_script_tag(user_details, widget_options = {})

Intercom script tag

Examples:

basic example

<%= intercom_script_tag({ :app_id => "your-app-id",
                          :user_id => current_user.id,
                          :email => current_user.email,
                          :custom_data => { :plan => current_user.plan.name },
                          :name => current_user.name }
                        ) %>

with widget activator for launching then widget when an element matching the css selector '#Intercom' is clicked.

<%= intercom_script_tag({ :app_id => "your-app-id",
                          :user_id => current_user.id,
                          :email => current_user.email,
                          :custom_data => { :plan => current_user.plan.name },
                          :name => current_user.name }
                          {:activator => "#Intercom"}
                       ) %>

Parameters:

  • user_details (Hash)

    a customizable hash of user details

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

    an optional hash for widget customisation

Options Hash (user_details):

  • :app_id (String)

    Your application id (get it here)

  • :user_id (String)

    unique id of this user within your application

  • :email (String)

    email address for this user

  • :name (String)

    the users name, optional but useful for identify people in the Intercom App.

  • :custom_data (Hash)

    custom attributes you'd like saved for this user on Intercom. See

Options Hash (widget_options):

  • :activator (String)

    a css selector for an element which when clicked should show the Intercom widget

Returns:

  • (String)

    Intercom script tag



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/intercom-rails/script_tag_helper.rb', line 30

def intercom_script_tag(user_details, widget_options={})
  intercom_settings = user_details.merge({:widget => widget_options})
  intercom_settings_with_dates_as_timestamps = convert_dates_to_unix_timestamps(intercom_settings)
  intercom_script = <<-INTERCOM_SCRIPT
<script id="IntercomSettingsScriptTag">
var intercomSettings = #{ActiveSupport::JSON.encode(intercom_settings_with_dates_as_timestamps)};
</script>
<script>
(function() {
  function async_load() {
var s = document.createElement('script');
s.type = 'text/javascript'; s.async = true;
s.src = 'https://api.intercom.io/api/js/library.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
  }
  if (window.attachEvent) {
window.attachEvent('onload', async_load);
  } else {
window.addEventListener('load', async_load, false);
  }
})();
</script>
  INTERCOM_SCRIPT
  intercom_script.respond_to?(:html_safe) ? intercom_script.html_safe : intercom_script
end