Using Customer.io with Rails
UpdatedThis tutorial helps you get set up with Customer.io on a standard Rails application and get people to show up in the campaign --SAMPLE-- Welcome campaign
behavioral campaign.
Add the customer.io gem to your Gemfile
gem 'customerio', :git => 'https://github.com/customerio/customerio-ruby'
Run bundle install
to install the gem then we’ll set up the configuration.
Configuration
We’ll add your Customer.io Site ID and API Key. We can do this in an initializer to save time and make sure we don’t accidentally commit sensitive data into source control.
config/initializers/customerio.rb
Rails.configuration.customerio = {
:site_id => ENV['CIO_SITE_ID'],
:api_key => ENV['CIO_API_KEY']
}
$customerio = Customerio::Client.new(Rails.configuration.customerio[:site_id], Rails.configuration.customerio[:api_key])
Identify People to Customer.io
Above we created the variable $customerio
in an initializer. We can now use that in our app to pass data to Customer.io.
Let’s identify people when they get created. Add a code snippet like this in your registrations controller:
$customerio.identify(
id: @id,
created_at: @created_at,
email: @email,
first_name: @first_name,
last_name: @last_name,
)
We’re using instance variables like @email defined in the controller.
*Note: created_at
is a very important attribute for Customer.io. To see people match the “Welcome” campaign they must have signed up within the past 24 hours.
Create a new user
If you now go back to your application and create a new user, you will see them queued up and ready to send in Customer.io
Deploying to Heroku
heroku create
heroku config:set CIO_SITE_ID=xxxxxxx CIO_API_KEY=yyyyyyy
git push heroku master
heroku open
Credits
Stripe’s amazing guide for Using Stripe’s Checkout and Rails inspired the creation and structure of this guide