Adding tags

Tagging users

Now that we have some users in the Platform you can start using some basic functions to segment them.

For example, you might want to use some test data when you're trying to understand the Intercom Platform but may not want to keep the data that you import at this point permanently. You can identify these users as 'test' users using a feature called tagging.

Tags are identifiers that you apply manually to your users. You can read more about them here.
Now, let's add some tags to your users to identify them as 'test' users.

List current tags

First, we can check what tags currently exist in our app. It'd be a good idea to create another file for tag-related tasks so we keep it separate from user tasks. We'll set it up in a similar way to the user_tasks.rb and create our first method to simply list the tags for your app.

require './intercom_client'
require './user_tasks'

class TagTasks < IntercomClient
  def initialize()
  end
  
  def show_tags()
    puts "Tag Name ---- Tag ID"
    @@intercom.tags.all.each {|tag| puts "#{tag.name} - #{tag.id}" }
  end
  
  end

You may need to restart your IRB and run the below commands:

require './tag_tasks'
=> true
> intercom = IntercomClient.new(<YOUR-ACCESS-TOKEN>)
=> #<IntercomClient:0x00000001d3e270>
> tags = TagTasks.new()
=> #<TagTasks:0x00000001cd5d60>
> tags.show_tags()
Tag Name ---- Tag ID
>

Tag individual users

Now we want to tag our users. When we do this using the SDK, we need to find the user and then tag that user. We can put this in one method and reuse the method we have already created from user_tasks.rb

def tag_user(criteria, tag)
    user = UserTasks.new()
    usr = user.find_user(criteria)
    tag = @@intercom.tags.tag(name: tag, users: [{email: usr.email}])
  end
require './intercom_client'
require './user_tasks'

  class TagTasks < IntercomClient
  def initialize()
  end

  def show_tags()
    puts "Tag Name ---- Tag ID"
    @@intercom.tags.all.each {|tag| puts "#{tag.name} - #{tag.id}" }
  end

  def tag_user(criteria, tag)
    user = UserTasks.new()
    usr = user.find_user(criteria)
    tag = @@intercom.tags.tag(name: tag, users: [{email: usr.email}])
  end
end

We can then tag one of our users as follows (we can give this user two tags, as users can have multiple tags associated with them):

tags.tag_user("[email protected]", "Test")
=> #<Intercom::Tag:0x00000001f1a6c0
> tags.show_tags()
Tag Name ---- Tag ID
Test - 342056
tags.tag_user("[email protected]", "New")
=> #<Intercom::Tag:0x000000027e3bd0
> tags.show_tags()
Tag Name ---- Tag ID
New - 342058
Test - 342056

List tags for a specific user

You can check the tags associated with a particular user. Tags are actually part of the user model so you just need to find the user and list their tags. Tags are treated like other standard attributes of the User Model, so you can use the method to check for other attributes other than just tags:

def show_attrib(criteria, attrib )
      #First we check whether this is a standard attribute
      user = find_user(criteria)
      user.send(attrib.to_sym)
  end
require './intercom_client'
require 'csv'

class UserTasks < IntercomClient
  def initialize()
  end

  def create_user(args)
    #Create a new user with list of values passed on setup
    user = @@intercom.users.create(args)
  end

  def find_user(criteria)
    begin
      #Check for users via the unique ID
      user = @@intercom.users.find(:id => criteria)
    rescue Intercom::ResourceNotFound
      begin
        #Check for users via user id if we receive not found error
        user = @@intercom.users.find(:user_id=> criteria)
      rescue Intercom::ResourceNotFound
        #Check for users via email address
        user = @@intercom.users.find(:email => criteria)
      end
    end
  end
  
  def submit_bulk_job(user_params)
    #Simple wrapper to create bulk job
    @@intercom.users.submit_bulk_job(create_items: user_params )
	end
  
  def bulk_create(csv_file)
    #Check to make sure the CSV file eixsts
    if File.exist?(csv_file)
      file = File.open(csv_file, "rb")
      body = file.read

      #Need to add to the CSV model to handle empty fields
      CSV::Converters[:blank_to_nil] = lambda do |field|
        field && field.empty? ? nil : field
      end
      csv = CSV.new(body, :headers => true, :header_converters => :symbol, :converters => [:all, :blank_to_nil])
      csv_data = csv.to_a.map {|row| row.to_hash }

      @@intercom.users.submit_bulk_job(create_items: csv_data )
    else
      puts("No CSV file found")
    end
  end

  def show_attrib(criteria, attrib )
      #First we check whether this is a standard attribute
      user = find_user(criteria)
      user.send(attrib.to_sym)
  end
end

Run the following commands to check the tags for one of your users:

user.show_attrib("[email protected]", "tags")
=> [#<Intercom::Tag:0x000000012fd400 @changed_fields=#<Set: {}>, @id="342056", @name="Test">,
 #<Intercom::Tag:0x000000012f5fc0 @changed_fields=#<Set: {}>, @id="342058", @name="New">]

This is returned as a list so you could iterate over this to just show the name of the tags:

> tags = user.show_attrib("[email protected]", "tags")
=> [#<Intercom::Tag:0x00000001d8d410 @changed_fields=#<Set: {}>, @id="342056", @name="Test">,
 #<Intercom::Tag:0x00000001d8ca60 @changed_fields=#<Set: {}>, @id="342058", @name="New">]
> tags.each  {|val| puts val.name}
Test
New

Tag multiple users

If you want to tag multiple users at a time you can re-use some of the functions you created already:

def tag_users(users, tag)
    users.each {|criteria| tag_user(criteria, tag)}
  end
require './intercom_client'
require './user_tasks'

  class TagTasks < IntercomClient
  def initialize()
  end

  def show_tags()
    puts "Tag Name ---- Tag ID"
    @@intercom.tags.all.each {|tag| puts "#{tag.name} - #{tag.id}" }
  end

  def tag_user(criteria, tag)
    user = UserTasks.new()
    usr = user.find_user(criteria)
    tag = @@intercom.tags.tag(name: tag, users: [{email: usr.email}])
  end

  def tag_users(users, tag)
    users.each {|criteria| tag_user(criteria, tag)}
  end
    
end

Then run these commands to tag multiple users:

> tags.tag_users(["[email protected]", "[email protected]"], "multiples")
=> ["[email protected]", "[email protected]"]
> tags.show_tags()
Tag Name ---- Tag ID
multiples - 342159
New - 342058
Test - 342056

 tags = user.show_attrib("[email protected]", "tags")
=> [#<Intercom::Tag:0x00000002330430 @changed_fields=#<Set: {}>, @id="342159", @name="multiples">]

Now let's move on to adding other information to users through custom and standard attributes.