Impressionist

#2 Impressionist
7/19/2015

Summary

A lightweight plugin that logs impressions per action or manually per model
5
view analytics rails

Summary

Gemfile     gem 'impressionist'
bash    bundle install
    rails g impressionist
    rake db:migrate
blogs_controller.rb    BlogsController < ApplicationController
      impressionist :actions=>[:show,:index]
    end

    def show
      @blog = Blog.find
      impressionist(@blog, "message...") # 2nd argument is optional
    end    
blog.rb    class Blog < ActiveRecord::Base
      is_impressionable
    end
index.html.erb    @blog.impressionist_count
7865030?v=3&s=64
Schwad said 4 months ago:

This is really handy! I had been using all sorts of mixpanel configurations, etc to try and track impressions on a site where all my visitors were anonymous, *not* users, and after watching this I think I'm going to have to give impressionist a go! Great screencast!

Login to Comment
Let's say we have a blog and we want to keep track of the number of users going into the show action of each blog entry. To do this we could create a new attribute in the blog model. However, this may not be the best route since you may have many different models and you want to keep this kind of tracking on each page. We can use the impressionist gem. The Impressionist gem is a lightweight plugin that logs impressions per action or manually per model. First, add the impressionist gem to the Gemfile
Gemfilegem 'impressionist'
Run `bundle` to install the gem. Next, we will need to generate the migrations required by impressionist.
bashrails g impressionist
Then migrate the database to create the new table and columns.
rake db:migrate
Within our Blogs controller, we can add in
blogs_controllerimpressionist :actions=>[:show]
However, the friendly_id gem doesn't necessarily play nicely with the impressionist gem. So we can add it within each action. We can call impressionist which will then create a record within our new table with information about the visit to this action.
blogs_controller.rb    def show
            @blog = Blog.find
            impressionist(@blog, "message...") # 2nd argument is optional
          end
We then need to go into our Blog model so that we can extend the methods available to our Blog.
blog.rb    class Blog < ActiveRecord::Base
            is_impressionable
          end
We can now use our new methods on the blog in our view.
index.html.erbblog.impressionist_count
Going back to our application, we can navigate into the show action a few times and refresh. Going back to our index, we now see that there has been a few counts on our views.