Custom event tracking with ActiveSupport::Notifications and Audited

Track Your Custom Events With Audited & ActiveSupport::Notifications

When it comes to logging or tracking changes in Ruby on Rails models, We typically tend to use either paper_trail or audited gems as these are most popular and widely used gems in Ruby on Rails applications for tracking changes in Rails models, but what if you want to track the custom events on controller level(like following) in addition to model changes? 

  • User login and logout 
  • Password change success or fail
  • User login failed or succeeded
  • OR any other user activity

In this post I will share my learnings on how we can track controller level custom events with the help of model changes tracking gem audited and ActiveSupport::Notifications.

Brief about ActiveSupport::Notifications

ActiveSupport::Notifications gives an APIs for event instrumentation which works on pub/sub model i.e. you can publish an event notification/trigger for the particular subscriber which in turn takes necessary actions for the given event trigger.

For example, publishing and subscribing of a ‘login_success‘ event

Publishing an event

ActiveSupport::Notifications.instrument('login_failed', event_data)<br>
#This triggers the notification for login_failed event<br>

Subscribing to an event

<p>ActiveSupport::Notifications.subscribe('login_failed') do |*args|<br>
  event =*args)<br>
  #Take necessary actions on this event<br>

Using Audited for saving custom events

I have created an EventHandler service which publishes/creates events and is used by controllers and subscribers.

class EventHandler<br>
  def initialize(event)<br>
    @event = event<br>
<p>  def process!<br>
    audit_data = {<br>
      username: @event.payload['email'] || @event.payload['username'],<br>
      comment: @event.payload[:comment]<br>
<p>    audit_data[:auditable] = @event.payload[:auditable] if @event.payload[:auditable]<br>
    audit_data[:user] = @event.payload[:user] if @event.payload[:user]<br>
<p>  def self.trigger_event_audit(event_name, params, resource, comment=nil)<br>
    event_data = {}<br>
    event_data.merge!(auditable: resource, user: resource, comment: comment)<br>
    ActiveSupport::Notifications.instrument(event_name, event_data)<br>

Using in controller action

Let’s say I have the following piece of code in my controller which allows the user to login.

class Users::SessionsController &lt; Devise::SessionsController<br>
  #POST /resource/sign_in<br>
  def create<br>
    self.resource = warden.authenticate(auth_options)<br>
    if resource.present?<br>
      EventHandler.trigger_event_audit('login_success', params, resource)<br>
      respond_with resource, location: after_sign_in_path_for(resource)<br>
      EventHandler.trigger_event_audit('login_failed', params, resource, 'invalid email/username or password address')<br>
      respond_with resource, location: root_path<br>

Subscribing to events

  ActiveSupport::Notifications.subscribe('login_failed') do |*args|<br>
    event =*args)<br>!<br>
<p>  ActiveSupport::Notifications.subscribe('login_success') do |*args|<br>
    event =*args)<br>!<br>


Rails ActiveSupport Instrumentation is very handy whenever we need to write the event based triggers for notifications, event tracking, data logging etc. The aim of this post is to demonstrate how we can use the Rails ActiveSupport Instrumentation with audited gem, Specially if implementation of audited gem is in place and on top of it we need to track the custom events without creating any new database schema.

If you have any suggestions or feedback, please feel free to add them in comments section.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.