Last Updated: February 25, 2016
·
11.46K
· sergiotapia

Track page views the smart way with the Impressionist gem.

When you're a web developer many times a client will ask you to show a Views text somewhere.

For example, how many views does this Profile have? How many views does this Car listing have?

In Ruby on Rails, there's a very clean and simple Gem that allows you to quickly and correctly add this functionality to your website.

Impressionist Gem

First, you can find the official documentation here.

What does this thing do?

Logs an impression... and I use that term loosely. It can log page
impressions (technically action impressions), but it is not limited to
that. You can log impressions multiple times per request. And you can
also attach it to a model. The goal of this project is to provide
customizable stats that are immediately accessible in your application
as opposed to using Google Analytics and pulling data using their API.
You can attach custom messages to impressions. No reporting yet.. this
thingy just creates the data.

We're going to add Impressionist to an existing application to track how many views an Auction has. Impressionist has a very nice feature of ignoring views from over 1200 bots, all taken from this comprehensive list.

Add Gem to Gemfile.

Add the Gem to your Gemfile and run the bundle install command to install it.

gem 'impressionist'

$ bundle install

Run the Impressionist generator.

This will generate all of the fields Impressionist needs to keep track of hits.

rails g impressionist

Then run a database migration to make sure everything is run against your database.

rake db:migrate

Tie your model into the Impressionist functionality.

We're going to add the is_impressionable property to our Auction model to make sure Impressionist can track it's hits.

class Auction < ActiveRecord::Base
  is_impressionable
end

Tell your controller what to keep track of.

In your controller, you can tell Impressionist what actions to count as a View.

class AuctionsController < ApplicationController
  impressionist actions: [:show], unique: [:session_hash]
end

Here we're only going to count a View when someone fires the :show action, and we're also making use of the :session_hash to keep track of unique views only.

Pretty nifty to keep those wget trolls at bay.

Display your view count in your View.

Now we can display how many Views this auction has.

<%= "#{@auction.impressionist_count} views so far!" %>

3 Responses
Add your response

Have you figured out how to add the unique views? Trying to sort by ip_address, but having some problems. Thanks.

over 1 year ago ·

Don't go that route...there's nothing unique about an IP address, it could link to thousands of users (think about cellular networks, or wifi hotspots); and most are not static.

If you a had a more unique attribute you could do something like you're talking about, but only GROUP BY, not sort by. Very close!

What about a session?

over 1 year ago ·

what is the database column name in the above given example

over 1 year ago ·