Putting it all together

We're now at the final stage of this tutorial. So far we have looked at the different ways we can interact with the Intercom Platform including creating and editing users, leads and companies, segmenting with tags and segments, using events, and using webhooks to be notified about them.

Now let's put some of this together to see if we can create a useful real life example.

Custom CSV importer

One thing that may be useful is the ability to create an app with some sample data or to import your users data in one file.

For example, in our earlier tutorial on "Creating Users" we created a method to import a CSV file with some basic user data. Wouldn't it be helpful if we could add other data to this CSV such as all our standard attributes, some custom attributes, tags and events? This way you could import your user data in one go or you could create some sample data for your test app.

To see what our sample CSV file would look like you can check the file we have in our GitHub repo for this tutorial series:

Script to import sample data

Now we need some code to run through this script, look at the data and add the relevant data to the platform. The code here reuses the code we worked on throughout the tutorial so hopefully you will recognize it. If you have any issues then feel free to refer back to the relevant tutorial to refresh your memory.

require 'csv'
require './user_tasks'
require './tag_tasks'
require './event_tasks'

def sample_data(csv_file, token)
  #Initialize Intercom with your credentials
  IntercomClient.new(token)
  #Instantiate your user class
  usr = UserTasks.new
  tag = TagTasks.new
  event = EventTasks.new

  #List of standard user attribtues we can create via the CSV
  user_attributes = ["email","name","user_id","id","signed_up_at",
                     "last_seen_ip","last_seen_user_agent",
                     "companies","last_request_at"]

  #Iterate through each row and check for user attributes
  CSV.foreach(csv_file, headers:true) do |row|
    begin
      user_data = {}
      for attrib in user_attributes
        if not row[attrib].nil?
          user_data[attrib.to_sym] = row[attrib]
        end
      end
      if not row['custom_attributes'].nil?
        #puts row.inspect
        custom_data = {}
        for line in row['custom_attributes'].split(';')
          vals = line.split(':')
          custom_data[vals[0].to_sym] = vals[1]
        end
        user_data['custom_attributes'] = custom_data
      end
      #Create a user with these attributes
      usr.create_user(user_data)
      puts user_data
      puts "Creating user:#{row['email']}"

      #Create tags for the users
      if not row['tags'].nil?
        for user_tag in row['tags'].split('::')
          tag.tag_user(row['user_id'], user_tag)
        end
      end

      #Create example events for users
      if not row['events'].nil?
        events = row['events'].split('::')
        3.times {event.submit_event(events.sample , Time.now.to_i, row['user_id'])}
      end
    rescue NoMethodError, Intercom::BadRequestError => e
      puts "ERROR Creating Users #{e.message}"
    end
  end
end

sample_data(ARGV[0], ARGV[1], ARGV[2])

Running the script

You can run this script by simply providing the source CSV file, your Access Token

> ruby sample_data.rb "sample_data.csv" "<ACCESS TOKEN>"
{:email=>"[email protected]", :name=>"Soren", :user_id=>"1", "custom_attributes"=>{:school=>"existentialism", :books=>"4"}}
Creating user:[email protected]
{:email=>"[email protected]", :name=>"Fred", :user_id=>"2", "custom_attributes"=>{:school=>"existentialism", :books=>"10", :character=>"zarathustra"}}
Creating user:[email protected]
{:email=>"[email protected]", :name=>"Martin", :user_id=>"3", "custom_attributes"=>{:occupation=>"philosopher", :book=>"beingandtime"}}
Creating user:[email protected]
{:email=>"[email protected]", :name=>"Jean", :user_id=>"4", "custom_attributes"=>{:meetup=>"cafedeflore", :partner=>"SimonedeBeauvoir"}}
Creating user:[email protected]

You should then see some information relating to the users the script is creating. You can check this to ensure the script is making progress in creating your users. You can then check your app to see that the users were created successfully and verify that there is event and tag information now available in your app.

Congratulations! You've made it to the end of the Onboarding data tutorial. You should now have a good understanding of how to get your data into the Intercom Platform, and the kind of things you can do with it once you have.

Look out for more tutorial series coming soon, covering the different Intercom products - if you have any questions or feedback on this one please do let us know, so we can continue making them better for you.