To integrate analytics into Rails application analytics-ruby gem can be used. By using this gem we can identify users and track their activity.
To install into gemfile:
gem 'analytics-ruby', :require => "segment"
After that initialize the gem with your Segment source’s Write Key and an optional error handler in config/initializers/analytics_ruby.rb
require 'segment/analytics'
Analytics = Segment::Analytics.new({
write_key: 'PUT_YOUR_WRITE_KEY',
on_error: Proc.new { |status, msg| print msg }
})
Now identify the user using identify method. It includes a unique User ID and any optional traits about the user.
Analytics.identify(
{
user_id: user.id,
traits: {
email: user.email,
first_name: user.first_name,
last_name: user.last_name
}
}
)
To track a user event
Analytics.track(
{
user_id: user.id,
event: 'Logged in'
}
)
0 Comment(s)