intercom / intercom-rails

The easiest way to install Intercom in a Rails app.
https://developers.intercom.io/reference
MIT License
281 stars 108 forks source link

Create users based on email, not user_id #270

Open heldopslippers opened 6 years ago

heldopslippers commented 6 years ago

Hi There,

We are trying to create users based on email, and nog on user_id. Tried a couple of things:

For example, just setting the user_id to nil

config.user.custom_data = {
    email: :email,
    user_id: Proc.new { nil}, 
    language: :language,
    name: Proc.new { |user| user.full_name }
  }

Why do we want this? We would like to match users with different account in different db's in the same intercom account.

rubendinho commented 6 years ago

Any update to this? We're looking to customize which id column we sent to Intercom.

stevenharman commented 6 years ago

We too need to customize the user_id and company_id values used to send the ping to Intercom. In our case, we have two different kinds of users, each working for a different kind of company. That is, we have:

class Employee < ActiveRecord::Base
  belongs_to :restaurant
end

class Restaurant < ActiveRecord::Base
  has_many :employees
end

# and...

class Technician < ActiveRecord::Base
  belongs_to :vendor
end

class Vendor < ActiveRecord::Base
  has_many :technicians
end

So the Intercom User could be either an Employee or Technician, and the Intercom Company either a Restaurant or Vendor. Given that, we cannot rely on the current intercom-rails behavior of sending along the #id attributes of whatever object the config.user.current and config.company.current work out to be. Instead we need to send something like "employee:#{user.id}" or "technician:#{user.id}" for the Intercom User. And similarly either "restaurant:#{company.id}" or "vendor:#{company.id}" for the Intercom Company.

We've been able to do this by leveraging the custom data, but it feels like a total hack and depends on internal details of how IntercomRails::Proxy#to_hash merges together the "standard data" with custom_data. What might be better would be a config value allowing us to specify a method or Proc to use to determine the ids to send.

Or perhaps some guidance on how to solve this, generally. e.g., by building our own proxy objects (via SimpleDelegator, for instance) which have custom #id methods that build the "right" id value? For example:

config.user.current = proc { current_user && IntercomUser.new(current_user) }

# elsewhere define the IntercomUser

class IntercomUser < SimpleDelegator
  def id
    [user_kind, user_real_id].join(':')
  end

  private

  def user_kind
    __getobj__.class.name.downcase
  end

  def user_real_id
    __getobj__.id
  end
end
tisba commented 6 years ago

That's an interesting workaround, @stevenharman, thanks!

Regardless of the solution, I think it would still be nice, if intercom-rails would allow to configure what data is being used for id, email, …

michiels commented 5 years ago

This is interesting. We have a platform with a seamloss login integration between multiple apps. So technically they are separate apps, but for the user they are the same platform. We would also like to have one user in Intercom with the same email instead of having them duplicated because they are present in both apps.

The workaround by @stevenharman looks interesting. Does it still work?

stevenharman commented 5 years ago

We're still using proxy objects to accomplish this, yes.