| Work | Research and Development (R&D) | Growth | Data and Analytics | How to add Analytics to Rails (Ahoy)
How to add Analytics to Rails (Ahoy)

Install Ahoy

Add to your Gemfile

gem 'ahoy_matey', '~> 3.0', '>= 3.0.1'

Install the gem

bundle install
rails generate ahoy:install
rails db:migrate

Enable Server Side Tracking

Track every action on every controller for signed in and anonymous users. Ahoy automatically attaches the current_user method used in authentication gems such as Devise to all events.

Add track_action method to app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  after_action :track_action

  protected
  def track_action
    if user_signed_in?
      ahoy.track("Server Requested #{request.path}", {
          category: "server-#{params[:controller]}-#{params[:action]}",
          path: request.path,
          path_parameters: request.path_parameters,
          query_string: Rack::Utils.parse_nested_query(request.query_string) 
        }
      )
    else
      ahoy.track("Server Requested #{request.path}", {
          anonymous_id: Digest::SHA256.hexdigest(session.id),
          category: "server-#{params[:controller]}-#{params[:action]}",
          path: request.path,
          path_parameters: request.path_parameters,
          query_string: Rack::Utils.parse_nested_query(request.query_string) 
        }
      )
    end
  end
end

Testing Server Side Tracking

Open an incognito window and visit your homepage and sign up. You'll want to test that your visits, events and being logged, but more importantly that actions taken by logged out users are eventually associated correctly once the users logs in.

In the example below, you'll notice that the visitid for both events are from the same visit/session and only after a user signs up is the userid added to the event.

rails console
Ahoy::Visit.last.events
 #<Ahoy::Event:0x00007fa4222213e8
  id: 3,
  visit_id: 1,
  user_id: nil,
  name: "Server Requested /signup",
  ...
 #<Ahoy::Event:0x00007fa4222211e0
  id: 4,
  visit_id: 1,
  user_id: 24,
  name: "Server Requested /",
  ...

On your Users model add:

app/models/user.rb

has_many :ahoy_visits, class_name: "Ahoy::Visit"
has_many :ahoy_events, class_name: "Ahoy::Event"

rails console
## see when a user last visited
User.last.ahoy_visits
## see a user's actions
User.last.ahoy_events

Enabling Client Side Tracking

First, enable the Ahoy API:

config/initializers/ahoy.rb

Ahoy.api = true
Ahoy.server_side_visits = :when_needed

In application.js

//= require ahoy
ahoy.trackAll();

Testing Client Side Tracking

Open an incognito window and visit your homepage and sign up. You'll want to test that your visits, events and being logged, but more importantly that actions taken by logged out users are eventually associated correctly once the users logs in.

In the example below, you'll notice that the visitid for both events are from the same visit/session and only after a user signs up is the userid added to the event.

rails console
Ahoy::Visit.last.events