Setting up Impression Tracking with Rails using the Impressionist Gem

2 min readNov 6, 2023

Impression tracking is a valuable feature for many web applications as it allows you to keep a count of how many times a specific resource, like a post or a page, has been viewed. In this blog post, we will walk through the process of setting up impression tracking in a Ruby on Rails application using the Impressionist gem.

Step 1: Creating a New Rails Application

To get started, let’s create a new Rails application. You can do this with the following commands:

rails new impressionist_1_gem
cd impressionist_1_gem

Step 2: Creating the Question Model

In our example, let’s assume that we want to track impressions on a `Question` model. We’ll create a `Question` model`:

rails generate model Question title content:text
rake db:migrate

Step 3: Adding the Impressionist Gem

Now, let’s add the Impressionist gem to your Rails application. Include it in your `Gemfile` and run the following commands:

gem 'impressionist'
bundle install
rails generate impressionist
rails db:migrate

The Impressionist gem generates a migration to create an `impressions` table. Here’s an example of what the migration might look like:

class CreateImpressionsTable < ActiveRecord::Migration[7.0]
def self.up
create_table :impressions, :force => true do |t|
t.string :impressionable_type
t.integer :impressionable_id
t.integer :user_id
t.string :controller_name
t.string :action_name
t.string :view_name
t.string :request_hash
t.string :ip_address
t.string :session_hash
t.text :message
t.text :referrer
t.text :params
# Add necessary indexes

def self.down
drop_table :impressions

Step 4: Making the Question Model “Impressionable”

In your `Question` model, you need to make it “impressionable” by adding the `is_impressionable` method:

# app/models/question.rb
class Question < ApplicationRecord
# ...

Step 5: Using Impression Tracking in the Questions Controller

In your `QuestionsController`, you can use Impressionist to track impressions on specific actions. For example, to track impressions on the `show` and `index` actions:

# app/controllers/questions_controller.rb
class QuestionsController < ApplicationController
impressionist :actions => [:show, :index]
# ...

Step 6: Displaying Impression Counts

To display the impression counts in your view, you can use the `impressionist_count` method provided by the Impressionist gem:

<h1>All Questions</h1>
<th colspan="3">Actions</th>
<% @questions.each do |question| %>
<td><%= %></td>
<td><%= question.title %></td>
<td><%= question.content %></td>
<td><%= question.impressionist_count %></td>
<%= link_to 'Show', question_path( %> |
<%= link_to 'Destroy', question_path(, method: :delete %> |
<%= link_to 'Edit', edit_question_path( %>
<% end %>

This blog post provides a step-by-step guide on setting up impression tracking in a Ruby on Rails application using the Impressionist gem. It covers the installation of necessary gems, creating models, and configuring impression tracking. With this setup, you can keep track of views on specific resources in your application.

Happy Impressionist Gem in Ruby on Rails! 🚀 Please follow for more updates





Ruby on Rails Developer || React Js || Rspec || Node Js