Giter Club home page Giter Club 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)

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 as easy as possible, 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.

Disabling automatic insertion

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

  skip_after_filter :intercom_rails_auto_include

Troubleshooting

If it's not working make sure:

  • 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 }

Feel free to mail us: [email protected], if you're still having trouble.

Configuration

API Secret

If you want to use secure mode, ensure you set your API secret in config/initializers/intercom.rb:

  config.api_secret = '123456'

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?
  }

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.

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 }

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?
}

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 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 %>

Importing your users

To get started faster with Intercom, IntercomRails includes a Rake task that will do an initial import of your users:

rake intercom:import

Any custom data defined in config/initializers/intercom.rb will also be sent.

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/

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-2012 Intercom, Inc. All rights reserved.

intercom-rails's People

Contributors

allaire avatar antidis avatar benmcredmond avatar ciaranlee avatar danielvartanov avatar darragh avatar dchapman1988 avatar drnic avatar eoghanintercom avatar eoin avatar erskingardner avatar jgwhite avatar josler avatar jscheel avatar jsierles avatar kinopyo avatar lorcan avatar marks avatar nickbender avatar owyongsk avatar raphaelcm avatar sashich avatar thbar avatar weppos avatar wiracocha avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.