Code Monkey home page Code Monkey logo

intercom-rails's Introduction

IntercomRails

The easiest way to install Intercom in a rails app.

For interacting with the Intercom REST API, use the intercom gem (https://github.com/intercom/intercom-ruby)

Requires Ruby 2.0 or higher.

Installation

Add this to your Gemfile:

gem "intercom-rails"

Then run:

bundle install

Take note of your app_id from here and generate a config file:

rails generate intercom:config YOUR-APP-ID

To make installing Intercom easy, where possible a <script> tag will be automatically inserted before the closing </body> tag. For most Rails apps, you won't need to do any extra config. Having trouble? Check out troubleshooting below.

Live Chat

With the Intercom Messenger you can chat with users and visitors to your web site. Include the Intercom Messenger on every page by setting:

  config.include_for_logged_out_users = true

Disabling automatic insertion

To disable automatic insertion for a particular controller or action you can:

  skip_after_action :intercom_rails_auto_include

Troubleshooting

If things are not working make sure that:

  • You've generated a config file with your app_id as detailed above.
  • Your user object responds to an id or email method.
  • Your current user is accessible in your controllers as current_user or @user, if not in config/initializers/intercom.rb:
  config.user.current = Proc.new { current_user_object }

If your users can be defined in different ways in your app you can also pass an array as follows:

  config.user.current = [Proc.new { current_user_object }, Proc.new { @user_object }]
  • If you want the Intercom Messenger to be available when there is no current user, set config.include_for_logged_out_users = true in your config.

Feel free to mail us: [email protected], if you're still having trouble and we'll work with you to get you sorted.

Configuration

API Secret

It is possible to enable Identity Verification for the Intercom Messenger and you can find the documentation in how to do it here. We strongly encourage doing this as it makes your installation more secure! If you want to use this feature, ensure you set your Identity Verification Secret as the API secret in config/initializers/intercom.rb:

  config.api_secret = '123456'

Note: This example is just for the sake of simplicity, you should never include this secret in source control. Instead, you should use the Rails secret config feature.

Shutdown

We make use of first-party cookies so that we can identify your users the next time they open your messenger. When people share devices with someone else, they might be able to see the most recently logged in user’s conversation history until the cookie expires. Because of this, it’s very important to properly shutdown Intercom when a user’s session on your app ends (either manually or due to an automated logout).

Using Devise

If you use devise, you can override (if not already done) the session_controller by replacing in your config/routes.rb file:

devise_for :users

with

devise_for :users, controllers: { sessions: "sessions" }

Then you can use the following code to prepare Intercom Shutdown on log out in your app/session_controller.rb

class SessionsController < Devise::SessionsController

  after_action :prepare_intercom_shutdown, only: [:destroy]

  # Your logic here

  protected
  def prepare_intercom_shutdown
    IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
  end
end

Assuming that the destroy method of session_controller redirects to your visitors_controller.rb#index method, edit your visitors_controller as follow :

class VisitorsController < ApplicationController
  after_action :intercom_shutdown, only: [:index]

  def index
    # You logic here
  end

  # You logic here

  protected
  def intercom_shutdown
    IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, request.domain)
  end
end

Using another service

If you use another service than Devise or if you implemented your own authentication service, you can call the following method in a controller to shutdown Intercom on logout.

IntercomRails::ShutdownHelper::intercom_shutdown_helper(cookies, domain)

Be aware that if you call this method before a 'redirect_to' (quite common on logout) it will have no impact as it is impossible to update cookies when you use a redirection. But you can use the same logic as the devise implementation above.

Session Duration

To add a session_duration variable (in ms) to the widget, add the following line to config/initializers/intercom.rb:

config.session_duration = 5 * 60 * 1000

That will force your Intercom session to expire after 5 minutes which is the minimum amount of time authorized.

More information about how session_duration works in intercom documentation

User Custom data attributes

You can associate any attributes, specific to your app, with a user in Intercom. For custom data attributes you want updated on every request set them in config/initializers/intercom.rb, the latest value will be sent to Intercom on each page visit.

Configure what attributes will be sent using either a:

  • Proc which will be passed the current user object
  • Or, a method which will be sent to the current user object

e.g.

  config.user.custom_data = {
    :plan => Proc.new { |user| user.plan.name },
    :is_paid => Proc.new { |user| user.plan.present? },
    :email_verified => :email_verified?
  }
  # or If User::custom_data method returns a hash
  config.user.custom_data = Proc.new { |user| user.custom_data }

In some situations you'll want to set some custom data attribute specific to a request. You can do this using the intercom_custom_data helper available in your controllers:

class AppsController < ActionController::Base
  def activate
    intercom_custom_data.user[:app_activated_at] = Time.now
    ...
  end

  def destroy
    intercom_custom_data.user[:app_deleted_at] = Time.now
    ...
  end
end

Attributes must be accessible in order to sync with intercom. Additionally, attributes ending in "_at" will be parsed as times.

Custom attributes for non-signed up users

In situations where you want to pass in specific request based custom data for non-signed up users or leads, you can do that by setting custom attributes in config/initializers/intercom.rb.

Any of these attributes can be used to pass in custom data.

Example:

in config/initializers/intercom.rb

config.user.lead_attributes = %w(ref_data utm_source)

in app/controllers/posts_controller.rb

class PostsController < ApplicationController

  before_action :set_custom_attributes, only: [:index]

  # Your logic here

  protected
  def set_custom_attributes
    intercom_custom_data.user[:ref_data] = params[:api_ref_data]
    intercom_custom_data.user[:utm_source] = params[:api_utm_source]
  end
end

Companies

By default, Intercom treats all Users as unrelated individuals. If for example you know users are part of a company, you can group them as such.

Read more about it here http://docs.intercom.io/configuring-Intercom/grouping-users-by-company

Basic usage is as follows - in config/initializers/intercom.rb

config.company.current = Proc.new { current_company }

current_company is the method/variable that contains the user's current company in your controllers. If you are using devise you should replace current_company with current_user.company in the above code and every time you see 'current_company' in your configuration file. This will result in injecting the user current company in the widget settings.

and like with Users, you can set custom attribute on companies too:

config.company.custom_data = {
  :number_of_messages => Proc.new { |app| app.messages.count },
  :is_interesting => :is_interesting?
}

In some situations you'll want to set some custom company data attribute specific to a request. You can do this similarly to user data attribute set by using the intercom_custom_data helper available in your controllers:

class AppsController < ActionController::Base
  def activate
    intercom_custom_data.company[:app_activated_at] = Time.now
    ...
  end

  def destroy
    intercom_custom_data.company[:app_deleted_at] = Time.now
    ...
  end
end

Messenger

Intercom includes an in-app messenger which allows a user to read messages and start conversations.

By default Intercom will add a button that opens the messenger to the page. If you want to customize the style of the link that opens the messenger:

  config.inbox.style = :custom

With this option enabled, clicks on any element with an id of Intercom will open the messenger. So the simplest option here would be to add something like the following to your layout:

  <a id="Intercom">Support</a>

You can customize the CSS selector, by setting

  config.inbox.custom_activator = '.intercom-link'

You can hide default launcher button, by setting

  config.hide_default_launcher = true

You can read more about configuring the messenger in your applications settings, within Intercom.

Environments

By default Intercom will be automatically inserted in development and production Rails environments. If you would like to specify the environments in which Intercom should be inserted, you can do so as follows:

  config.enabled_environments = ["production"]

Manually Inserting the Intercom Javascript

Some situations may require manually inserting the Intercom script tag. If you simply wish to place the Intercom javascript in a different place within the page or, on a page without a closing </body> tag:

  <%= intercom_script_tag %>

This will behave exactly the same as the default auto-install. If for whatever reason you can't use auto-install, you can also provide a hash of user data as the first argument:

<% if logged_in? %>
  <%= intercom_script_tag({
    :app_id => 'your-app-id',
    :user_id => current_user.id,
    :email => current_user.email,
    :name => current_user.name,
    :created_at => current_user.created_at,
    :custom_data => {
      'plan' => current_user.plan.name
    }
  }) %>
<% end %>

You can also override IntercomRails::Config options such as your api_secret, or widget configuration with a second hash:

<% if logged_in? %>
  <%= intercom_script_tag({
    :app_id => 'your-app-id',
    :user_id => current_user.id,
    :email => current_user.email,
    :name => current_user.name,
    :created_at => current_user.created_at
  }, {
    :secret => 'your-apps-api-secret',
    :widget => {:activator => '#Intercom'}
  }) %>
<% end %>

Content Security Policy Level 2 (CSP) support

As of version 0.2.30 this gem supports CSP, allowing you to whitelist the include code using both nonces and SHA-256 hashes.

Automatic Insertion

CSP support for automatic insertion exposes two namespaces that can be defined by the user via monkey patching:

  • String CoreExtensions::IntercomRails::AutoInclude.csp_nonce_hook(controller)
  • nil CoreExtensions::IntercomRails::AutoInclude.csp_sha256_hook(controller, SHA-256 whitelist entry)

For instance, a CSP nonce can be inserted using the Twitter Secure Headers gem with the following code:

module CoreExtensions
  module IntercomRails
    module AutoInclude
      def self.csp_nonce_hook(controller)
        SecureHeaders.content_security_policy_script_nonce(controller.request)
      end
    end
  end
end

or, for whitelisting the SHA-256 hash:

module CoreExtensions
  module IntercomRails
    module AutoInclude
      def self.csp_sha256_hook(controller, sha256)
        SecureHeaders.append_content_security_policy_directives(controller.request, {script_src: [sha256]})
      end
    end
  end
end

Manual Insertion

CSP is supported in manual insertion as well, the request nonce can be passed as an option:

<% if logged_in? %>
  <%= intercom_script_tag({
    :app_id => 'your-app-id',
    :user_id => current_user.id,
    :email => current_user.email,
    :name => current_user.name,
    :created_at => current_user.created_at
  }, {
    :secret => 'your-apps-api-secret',
    :widget => {:activator => '#Intercom'},
    :nonce => get_nonce_from_your_csp_framework
  }) %>
<% end %>

The SHA-256 hash is available using csp_sha256 just after generating the tag itself:

<%= intercom_script_tag %>
<% add_entry_to_csp_whitelist(intercom_script_tag.csp_sha256) %>

Deleting your users

If you delete a user from your system, you should also delete them from Intercom lest they still receive messages.

You can do this using the intercom-ruby gem. In the example below we're using an ActiveJob to perform the delete in the background.

class User
  after_destroy { DeleteFromIntercomJob.perform_later(self) }
end

class DeleteFromIntercomJob < ApplicationJob
  def perform(user)
    intercom = Intercom::Client.new
    user = intercom.users.find(id: user.id)
    deleted_user = intercom.users.delete(user)
  end
end

Running tests/specs

specs should run on a clean clone of this repo, using the following commands. (developed against ruby 2.1.2 and 1.9.3)

bundle install
bundle exec rake spec
or
bundle exec rspec spec/

Pull Requests

  • Add tests! Your patch won't be accepted if it doesn't have tests.

  • Document any change in behaviour. Make sure the README and any other relevant documentation are kept up-to-date.

  • Create topic branches. Don't ask us to pull from your master branch.

  • One pull request per feature. If you want to do more than one thing, send multiple pull requests.

  • Send coherent history. Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before sending them to us.

Contributors

  • Dr Nic Williams (@drnic) - provided a rails generator for adding the Intercom javascript tag into your layout.
  • Alexander Chaychuk (@sashich) - fixed bug in user detection when users not persisted (e.g. new session view with devise).

License

intercom-rails is released under the MIT License.

Copyright

Copyright (c) 2011-2020 Intercom, Inc. All rights reserved.

intercom-rails's People

Contributors

ahorek avatar antidis avatar benmcredmond avatar bobjflong avatar choran avatar ciaranlee avatar danielhusar avatar darragh avatar darraghotoole avatar dkremez avatar drnic avatar eoin avatar erskingardner avatar eugeneius avatar irvinebroque avatar jonnyom avatar josler avatar jsierles avatar kmossco avatar lorcan avatar marks avatar mmartinic avatar pizza avatar raphaelcm avatar ruairik avatar scottsouthard avatar sduquej avatar seanhealy33 avatar sid77 avatar stevenharman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

intercom-rails's Issues

Intercom.update

I am aware that there are many ways to interact with the Intercom API with both the javascript client and the intercom ruby gem. Is there a preferred technique for updating a users details with async/ajax events? Preferably I would like to be able to push these events via javascript without having to add an extra remote javascript dependency.

FWIW. It seems like my best path would be to go to the javascript and not require the niceties of the intercom-rails gem. Can you confirm?

Cannot import users with rake task

Am I doing something wrong?

$ RAILS_ENV=staging be rake intercom:import
We couldn't find your user class, please set one in config/initializers/intercom_rails.rb

But when I run the app I can see the data was sent to Intercom so how this is not returning User?

def user_klass
  if IntercomRails.config.user.model.present?
    IntercomRails.config.user.model.call
  else
    User
  end
rescue NameError
  # Rails lazy loads constants, so this is how we check 
  nil
end

And one more thing, this config/initializers/intercom_rails.rb should be changed to config/initializers/intercom.rb, right?

Thanks.

Allow for secure mode

the user_hash attribute should be generated from the user_id attribute (if present) or the email

Handling different User classes.

On a current project we have 3 different User models (2 that are important for Intercom). One for Venues and one for QuizMasters. Both respond to the current_user method so at first glance it seems to work, but I'm wondering if there will be an issue since the user_id will overlap since they are different models and the ids would not be unique between models.

Eg I'm sure there is a 'user' in both models with the id of 42.

What can I do for make intercom aware of this? Or do I need to?

Thanks!

Update README and explain how to run the tests

So, I'm trying to write tests for some modification I made for the gem but I can't get it to run.

As far as I understand the gem use another gem(minitest) to run the tests but I can't get it to work.

Would be nice to update the README and explain how to run all the tests.

Thanks

Does not work with Turbolinks.visit()?

When I use Turbolinks.visist() directly in my Javascript, I see the Intercom code inserted in the HTML after the page change, but the inbox never show up. Any hints?

prevent exceptions in custom_data

Given the 'production only' nature of intercom, it would be a good idea to me to avoid exceptions in custom data. I'm scared to put something in there which my tests will never catch.

user_id should be stringified

When current_user is a Mongoid model the id will serialize into {"$oid":"51bc552ed1c93ef363000001"} rather than "51bc552ed1c93ef363000001" which causes secure mode to never activate even though the secret key is valid.

For the time being I could get around to it by overriding the config.user.custom_data with a lambda but the real solution would be to run .to_s in the appropriate place.

How do I hide the intercom widget?

I want to use the Intercom script to monitor user activity, but I want to hide the Intercom Messenger displayed in the page. Is there a configuration for it in the gem?

Can't skip auto include filter

As mentioned in #5, you should be able to skip the after filter like this:

skip_after_filter IntercomRails::AutoIncludeFilter

This doesn't work. Under Rails 3.2.9 and IntercomRails 0.2.8, the filter is still executed even with that line in ApplicationController. It seems the problem is with the way ActiveSupport handles the callbacks internally. ActiveSupport::Callbacks::Callback#matches? checks for:

@kind == _kind && @filter == _filter

But the value of @filter for the AutoIncludeFilter callback is being set to a value like "_callback_after_3", and not AutoIncludeFilter, so the match fails as it's effectively comparing:

"_callback_after_3" == IntercomRails::AutoIncludeFilter

This call to matches? happens on lib/active_support/callbacks.rb:519.

Git release tag

There is no git release tag for the latest version 0.2.28

Issues running tests

Hi,
I'm trying to run tests for this repo, but I get 16 failures similar to this:

16) Error:
test_delegates_to_script_tag_generate(ScriptTagHelperTest):
ArgumentError: wrong number of arguments (1 for 2)
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/mock.rb:173:in `stub'
    /home/fregini/Work/intercom-rails/test/intercom-rails/script_tag_helper_test.rb:10:in `test_delegates_to_script_tag_generate'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1301:in `run'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:919:in `block in _run_suite'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:912:in `map'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:912:in `_run_suite'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:899:in `block in _run_suites'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:899:in `map'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:899:in `_run_suites'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:867:in `_run_anything'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1060:in `run_tests'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1047:in `block in _run'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1046:in `each'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1046:in `_run'
    /home/fregini/Work/intercom-rails/test/intercom-rails/import_network_test.rb:43:in `_run'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:1035:in `run'
    /home/fregini/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/minitest/unit.rb:789:in `block in autorun'

It happens on master branch and on tag 0.2.22 as well. Any clue on what's causing this?

Explicitly stop JS from inserting itself on all pages

The gem seems to automatically include the Intercom JavaScript, even if it has not been explicitly added to the file. We have one layout which we do not want Intercom on, but it is still inserting the JS.

It appears there is no way to disable this behaviour. In our case, this layout will never be used by users we want to track, and it adds a substantial load time to the page. There needs to be a way to exclude it explicitly from certain layouts.

How to import special fields

Hi,

We are trying to import our current users into intercom, but we're having a bit of an issue with the config file. From looking at the code, we don't quite understand how we can import elements such as last_seen_ip, last_seen_user_agent, updated_at, last_request_at, and number of previous logins (sessions).
Should those be passed as custom_data?

Add an option in the config file to exclude certain types of users

Is it possible to exclude some types of users from being imported to Intercom.io?

The first simple solution I found was to skip the controller filter, but when I ran the import rake task all of my users are imported into Intercom.io.

I think an useful feature would be having an option in the config file to include/exclude certain types of users, with a Proc.

`.intercom-sheet-footer` missing

The end users can see the default circle and speech bubble with a question mark, however, there's no New message button at the bottom.

Am I missing some steps?

There is no tag for version 0.2.24

I am using the intercom-rails gem and went to look at the specific readme and code for the version we are currently using (per our Gemfile.lock) and noticed that there is no tag for 0.2.24.

API included?

Hi guys,

Are the ones described here available to this gem?
http://doc.intercom.io/api/v1/

I know it's possible to curl it but wondering if you guys already have something built-in. If so, can you guys send me a link of docu. If not, any plans to integrate?

Add Companies in Initializer

Hey there,

Gotta start by saying I love Intercom and that it's got an incredible developer onboarding process. Unfortunately, I've hit a snag with getting companies set up.

To start, we use devise so our intercom user config works with the default current_user and required no work. We also have a Company model and User's have one company. Ie. If you were to execute current_user.company in the controller, you would get an instance of of the class Company.

Here's where the trouble arises. I have attempted the two suggested solutions in the Intercom initializer for adding a company to Intercom.

First, I uncommented:

# config/initializers/intercom.rb

config.user.company_association = Proc.new { |user| [user.company] }

I presumed this would execute something like this at some point:

IntercomRails.config.user.company_association.call(current_user)

And it would return current_user's company in an array. When I do this from the console, it does return just that. It does not do this in the app though. I've also attempted to replace the Proc with just returning any instance of Company by doing:

# config/initializers/intercom.rb

config.user.company_association = Proc.new { |user| [Company.first] }

This did not work either, so I moved onto the other option of current_company by uncommenting:

# config/initializers/intercom.rb

config.company.current = Proc.new { current_company }

I also defined the following helper:

# app/helpers/application_helper.rb

module ApplicationHelper
  ...
  def current_company
    if current_user
      current_user.company
    end
  end
  ...
end

This didn't work though so I checked if I can just provide a random Company object and just return something. I then update the initializer to:

# config/initializers/intercom.rb

config.company.current = Proc.new { Company.first }

This did work and I managed to get a company on Intercom.io. From here, I've tried to work together some sort of solution but I have not been able dynamically produce a company for my users. As I mentioned before, I expected the following to be executed somewhere:

IntercomRails.config.user.company_association.call(current_user)

I started to look through the source code here and in https://github.com/intercom/intercom-ruby and the I believe the Proc is called here:

# intercom-rails/lib/intercom-rails/proxy/company.rb

def self.companies_for_user(user)
  return unless config(:user).company_association.present?
  companies = config(:user).company_association.call(user.user)
  return unless companies.kind_of?(Array)

  companies.map { |company| new(company) }.select { |company_proxy| company_proxy.valid? }
end

Though, I think this is only called in the importing script, I don't understand what user.user does. Any help would be appreciated. Please let me know if I can provide any further information.

Cache company name

I'd like to be able to cache the company name of the current user without loading my company model. However I am not sure how to do that since it appears that the company name is determined by running company.name after loading the company from config.company.current = Proc.new { current_user.company }.

I'd rather not cache the entire company model because the cache invalidation could be tricky, but caching the name (especially if it was just for intercom) would be fine. Am I missing something simple?

The Intercom API request failed with the code: 401

Hello,

I'm using intercom-rails (0.2.27), when I try to import using rake test, getting this error below; doubled checked api key it is all correctly set on initializer. Can you re-produce this?

IntercomRails::IntercomAPIError: The Intercom API request failed with the code: 401, after 3 attempts.

Cleanest way of integrating Intercom gem with Rails 4 app

Hi all, this is more of a question rather than an issue. We are working on integrating Intercom in our app to track all our events and page views, and we initially thought of putting them all in a AnalyticsObserver.rb.

However, as of Rails 4, observers are highly discouraged and we're trying to find what's the best way to track users and send events without cluttering our code.

Any ideas / suggestions?

Inbox should be hidden by default

After doing the very basic integration, I can see that's inbox icon is not hidden by default and it's causing an issue as it's not useful when I am on a free plan or want to use Intercom just to observe the users.

Problems with default app ID in development

In my app, I rely on an environment variable in production to set the Intercom app ID. In development, I prefer that the code not load, primarily so I don't introduce development data into the production Intercom account.

However, after upgrading to the latest version of the intercom-rails gem, I now have no choice about whether the tracking code is loaded or not, because the gem defaults to an app ID of `"abcd1234" in development.

Why is this default app ID set?

It also seems to be causing JavaScript errors along the lines of:

XMLHttpRequest cannot load https://api.intercom.io/api/messages/index. Origin http://myapp.dev is not allowed by Access-Control-Allow-Origin.
GET https://api.intercom.io/api/messages/index?[…]&callback=IntercomJSONP1357105264740&_=1357105264740 422 (Unprocessable Entity) library-9652678f34ecafeaf9ec23023c4ac38d.js:2

Custom activator doesn't work with new messaging box update.

Previously, this

= intercom_script_tag nil, widget: { activator: '.intercom' }

used to work, without any changes from intercom website, and overrode any custom settings done to make the class .intercom act as activator and also not show the intercom messaging icon.

With the new messaging App, the icon still shows even after having a custom activator, unless disabling from here http://take.ms/x29NX

Using other method as unique id for users

Hello,

I'd like to use another method than current_user.id to send to intercom.

I have defined a method intercom_id which I would like to be called instead of .id

How can this be done?

This is what I tried

  config.user.current = Proc.new { 
    _intercom_id = current_user.intercom_id
    current_user.define_singleton_method(:id) { _intercom_id }
    current_user
  }

in combination with

config.user.custom_data = {
      :id => Proc.new { |current_user| current_user.intercom_id },
      ...

but so far, no luck. I keep on getting the result of user.id in intercom.

Import rake task doesn't work when secure mode is enabled.

Hello. We had a problem importing users using the rake task. This seems to be a problem when secure mode is on. It worked perfectly before we enabled that on our Intercom account.

When secure mode is enabled on the account the rake task fails with:

IntercomRails::IntercomAPIError: The Intercom API request failed with the code: 500, after 3 attempts.

We had a suspicion that the import task needed the user_hash to be calculated and sent with the request. See this commit on our fork.

This worked, but I didn't do a pull request as I suspect the correct fix is to allow import without the user_hash as the user_hash is for protecting against forgery in the browser. Where as the import script is run on the server.

If you disagree I'm happy to clean up our fork of course. The code to calculate the user_hash shouldn't be duplicated and I'd be happy to clean that up.

Thanks,

Greg

JS Errors

Hi,

I am getting a lot of errors from intercom JS tracked by bugsnag, I don't know if its related to using turbolinks.

Error attached:

TypeError: Uncaught TypeError: Cannot read property '$el' of undefined
https://js.intercomcdn.com/intercom.f74a6095.js:7:3246 n.b.extend.add
add:function(){a("body").append(this.view.render().$el),this.ready.resolve(),this.trigger("add")},rehttps://js.intercomcdn.com/intercom.f74a6095.js:8:22602 HTMLDocument.
https://js.intercomcdn.com/intercom.f74a6095.js:3:5191 HTMLDocument.fb.event.dispatch
https://js.intercomcdn.com/intercom.f74a6095.js:3:1871 HTMLDocument.q.handle
https://sexyonecoimbra.minestore.com.br/admin/produtos:6:9774 HTMLDocument.nrWrapper
https://duajuoiec0cnh.cloudfront.net/assets/admin-70f0f01dcd76c1f713805e57ce030037.js:7:28061 G
https://duajuoiec0cnh.cloudfront.net/assets/admin-70f0f01dcd76c1f713805e57ce030037.js:7:25940 h
https://duajuoiec0cnh.cloudfront.net/assets/admin-70f0f01dcd76c1f713805e57ce030037.js:7:24908 XMLHttpRequest.K.onload
https://sexyonecoimbra.minestore.com.br/admin/produtos:6:9774 XMLHttpRequest.nrWrapper

Need to update docs to explain companies setup

The README doesn't talk at all about setting up companies. This needs to be updated. The stock intercom.rb initializer file does give some basic instructions so the instructions could likely be copied over without much change.

Add user events

I see on the ruby intercom project that it is possible to push an user event (intercom/intercom-ruby#69) wondering if this is possible on the rails gem too? or If thats something we need to add-on ?

thanks

Don't track if session variable set

Hi guys,

Just started using this gem and am wondering what the best approach for this is:

We have an admin feature in our app where you can 'become' a user and see the site through their eyes. When we do this, we set a session variable to show that the current user is actually a superuser.

Ideally, I'd like to keep session value checks out of my model, i.e.

#config/initializers/intercom.io
...
  config.user.exclude_if = proc { |u| u.imposter? }
...
#app/models/user.rb
def imposter?
  session.key?(:masquerade)
end

feels a bit icky.

I'm not sure if checking the session variable directly in the proc is any nicer either.

Is this the only way to do it?

Thanks!
W

skip_after_filter :intercom_rails_auto_include does not work with turbolinks

In our app, we are using turbolinks and intercom-rails. We do not want the Intercom interface to show up for views under LessonsController, so we include the line skip_after_filter :intercom_rails_auto_include in that controller.

When we visit a page in LessonsController directly, the Intercom include is not served to the client (as expected).

However, if the client starts in a view under DashboardController (which does include Intercom), then clicks a link to a view in LessonsController, the Intercom interface is still shown because turbolinks does not remove the Intercom include.

With activeadmin

How to disable intercom button on some pages? For example in activeadmin

Imported Date custom_data set to '1 Jan 1970'

I have custom data that returns a Date object. It works fine for users who log in to the system and have their intercom data sent along that way.

It doesn't work for importing old users.

Here's a user I inspected via the console:

1.8.7 :025 > u = User::Employer.find_by_email("[email protected]")
 => #<User::Employer ... >
1.8.7 :026 > proxy = IntercomRails::Proxy::User.new(u)
 => #<IntercomRails::Proxy::User:0xe4cbfb8 ...>
1.8.7 :027 > proxy.custom_data[:listing_last_published]
 => Wed, 09 Dec 2009 
1.8.7 :028 > proxy.custom_data[:listing_last_published].class
 => Date 

After I run the import script, the user's listing_last_published shows up as 1 Jan 1970.

Screen Shot 2013-04-19 at 11 32 17 AM

If I spoof the user to simulate them logging in, the listing_last_published field updates to the correct value.

Screen Shot 2013-04-19 at 11 33 13 AM

Changing config on a per-pageload basis

Hi, I run a multitenant app with 2 tenants. I want to configure Intercom to send data to 2 Intercom app_ids based on the subdomain of the current page load. Current, config/initializers/intercom.rb doesn't allow me to do this because config.app_id, config.api_secret, and config.api_key are set upon server startup, not upon page load. I was looking at https://github.com/intercom/intercom-rails/blob/master/lib/intercom-rails/config.rb#L22 to figure out if I could pass a Proc or a block to IntercomRails, but it doesn't look like I can.

Is there a way to set my Intercom credentials dynamically (i.e. similar to how current user gets set with a Proc), without just abandoning all the Rail-y config this app provides and going straight Javascript/helper?

Thanks.

-Ben

Running import rake task, getting 429 from Intercom

We were able to successfully run the import on a test account, but were trying to go live today and now getting the 429 error back from Intercom when running the rake import:intercom task. Should the task handle this already? Is this something we need to account for?

(This is gem version 0.2.26)

Cannot skip auto include filter

Further to issues 5 and 20, skipping the auto include filter appears to be broken again. Neither of these snippets work:

skip_after_filter IntercomRails::AutoIncludeFilter

skip_after_filter :intercom_rails_auto_include

Custom data assignments dont work with redirects

Hi!
The problem:
intercom_custom_data assignments is stored in controller's instance variable, that will be flushed after redirect.
I think the solution to this problem would be to store that data in session.

Adding a name for the current user?

If I were manually inserting the Javascript, I'd do this to add a "real name" for the current user:

var intercomSettings = {
  name: 'John Doe' // Optional: Replace with real name if available
};

But I don't see a way to do this with the gem. Am I missing something obvious? Or do I just pass it as custom data?

Only show messenger inbox to certain types of users

I have several types of user accessing my app: Teacher, Student, Parent, etc. I want to observe all types but only show the messenger inbox to Teacher types.

Is there a way to achieve this without having to code a custom intercom_script_tag?

I hoped there might be a config option for it, something like:

config.show_inbox = Proc.new { |user| user.type == 'Teacher' ? true : false }

I tried passing a similar Proc into config.inbox.style to switch to custom style (ie hide the icon) but it didn't work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.