DEV Community

Bhartee Rameshwar Sahare
Bhartee Rameshwar Sahare

Posted on

# Setting up Impression Tracking with Rails using the Impressionist Gem

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
Enter fullscreen mode Exit fullscreen mode

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 and set up the necessary associations:

rails generate model Question title content:text
rake db:migrate
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
bundle install
rails generate impressionist
rails db:migrate
Enter fullscreen mode Exit fullscreen mode

Step 4: Setting Up the Impressionist Table

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
      t.timestamps
    end
    # Add necessary indexes
  end

  def self.down
    drop_table :impressions
  end
end
Enter fullscreen mode Exit fullscreen mode

Step 5: 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
  is_impressionable
  # ...
end
Enter fullscreen mode Exit fullscreen mode

Step 6: 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]
  # ...
end
Enter fullscreen mode Exit fullscreen mode

Step 7: 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>
<table>
  <thead>
    <tr>
      <th>Id</th>
      <th>Title</th>
      <th>Content</th>
      <th>Views</th>
      <th colspan="3">Actions</th>
    </tr>
  </thead>
  <tbody>
    <% @questions.each do |question| %>
      <tr>
        <td><%= question.id %></td>
        <td><%= question.title %></td>
        <td><%= question.content %></td>
        <td><%= question.impressionist_count %></td>
        <td>
          <%= link_to 'Show', question_path(question.id) %> |
          <%= link_to 'Destroy', question_path(question.id), method: :delete %> |
          <%= link_to 'Edit', edit_question_path(question.id) %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)