Gem to track User Activity
Yes there are. Here is one, paper trail. https://github.com/airblade/paper_trail
Its main prupose is to versioning models but I think its also good for your use case since it registers who made each version.
For more gems search at ruby toolbox. Here are the categoties that you should check in this case
https://www.ruby-toolbox.com/categories/Active_Record_User_Stamping
https://www.ruby-toolbox.com/categories/Active_Record_Versioning
Maybe Userstamp is actually better for your use case.
How to track user activity in Rails?
You may get what you need out of using the Analytical gem
Analytical supports numerous services including kissmetrics and clicky, one of which should be an excellent fit for the data you are trying to gather.
whether the paper trail has a way to track the user activity based on a specific role
i just want to track the activity of a specific [user] role
You can turn PaperTrail off in your controller. From the documentation:
Turning PaperTrail Off
Per Request
Add a
paper_trail_enabled_for_controller
method to your controller.class ApplicationController < ActionController::Base
def paper_trail_enabled_for_controller
!(cool_user? || trustworthy?) # :) this part is up to you
end
end
If that doesn't work, there are many other ways to turn PaperTrail on/off described the documentation.
Log user activities in ROR
Ok this is what i did...
First create table
create_table "activity_logs", :force => true do |t|
t.string "user_id"
t.string "browser"
t.string "ip_address"
t.string "controller"
t.string "action"
t.string "params"
t.string "note"
t.datetime "created_at"
t.datetime "updated_at"
end
created function in application controller
def record_activity(note)
@activity = Activity_Log.new
@activity.user = current_user
@activity.note = note
@activity.browser = request.env['HTTP_USER_AGENT']
@activity.ip_address = request.env['REMOTE_ADDR']
@activity.controller = controller_name
@activity.action = action_name
@activity.params = params.inspect
@activity.save
end
then call above function from any controller needed
like this....
class AccountsController < ApplicationController
load_and_authorize_resource
# POST /accounts
# POST /accounts.json
def create
@account = Account.new(params[:account])
respond_to do |format|
if @account.save
record_activity("Created new account") #This will call application controller record_activity
format.js { head :ok }
else
format.js { render json: @account.errors, :error => true, :success => false }
end
end
end
Hence problem solved......
Thanks all for all their help....
How to track if user make any actions Rails app
It is certainly possible to put together a simple user tracking feature without using an external gem specifically built for this purpose. Here is a list of the required implementation steps:
1. Add a total_time_online
and a last_seen_at
field to User
total_time_online
will contain the number of seconds the user was seen onlinelast_seen_at
will hold the date and time the user last interacted with the site
2. Add an active_now!
method to User
This method will be called whenever the user is interacting with the site. It is responsible for incrementing the total_time_online
value and updating the last_seen_at
field:
class User
ActivityThreshold = 5.minutes
# ...
def active_now!
time_since_last_activity = [Time.now - last_seen_at, 0].max
if time_since_last_activity <= ActivityThreshold
self.total_time_online ||= 0
self.total_time_online += time_since_last_activity
end
self.last_seen_at = Time.now
save!
end
# ...
end
This will only increment the total_time_online
if the last interaction was less than 5 minutes ago.
3. Call active_now!
on the current user on every request
A global before_action
should do the trick:
class ApplicationController < ActionController::Base
# ...
before_action :record_user_activity
# ...
private
# ...
def record_user_activity
current_user.active_now! if current_user
end
end
Related Topics
Understand "Current_User" Concept When Creating a Login Session in Ruby
How to Download a File Over Http Using Ruby
Installing Pl/Ruby for Postgresql 8.3
Rails Sitemap_Generator Uninitialized Constant
Get All Local Variables or Available Methods from Irb
Error Installing JSON 1.8.3 with Ruby 2.4
Made a Mistake Installing Rvm with Sudo. How to Reverse
Activeadmin: Adding Delete for a Nested Resource
How to Make Single Digit Number a Two Digit Number in Ruby
Load Works on Local Path, Require Doesn'T
How to Extend a Class from an Initializer and Have It Reload in Development Environment
I Can't Remove Whitespaces from a String Parsed by Nokogiri
Rails 5, "Nil Is Not a Valid Asset Source"
Get Table Name from Activerecord
Rails Server Does Not Start -> Could Not Find a JavaScript Runtime