How to Track Page Impressions in Ruby on Rails?
- Mischelle L
- May 23, 2018
- 1 min read
Many times, we need to track the page views of any page such as profile page, shopping sites page in a Ruby on Rails applications, for which we can make use of Impressionist gem to track those page details easily.
It is a lightweight plugin that logs impressions per action or manually per model. We can log impressions multiple times per request. And we can also attach it to a model.
Below are the simple steps needed to follow to implement it:
Step 1: Create a sample app using scaffold.
Step 2: Add “gem ‘impressionist’” gem to the Gemfile and install it by running bundle install command.
gem ‘impressionist’, git: ‘https://github.com/jordanhudgens/impressionist’
Step 3: Now generate the impressions table migration by running the below commands:
rails g impressionist
rake db:migrate
Step 4: Now we need to update our controller that we want to add the impression or page views counting system. So first we have add the following method in our controller with mentioning the list of actions that we want to count:

And then update the controller action with the impressionist method:

Step 5: We need to add the new counter cache column to the model that we are working with. Run the following command:
rails g migration add_impressions_count_to_blogs impressions_count:integer
rails db:migrate
Now we can update the model for tracking the hits:

Step 6: Now we can display how many views this Blog has.

Commentaires