Get PV (views) using Impressionist ~ Rails

Target

In this article, we will use a gem called Impressionist to implement PV (page view) number acquisition and PV number ranking of posts.

Click here for details ~

Development environment

・ Ruby: 2.6.2 Rails: 6.0.3 ・ OS: windows

Premise

A set of posting functions has been created with the Tweet model.

Implementation

1. Introduced ʻimpressionist`

Add the following line to the gemfile.

Gemfile


gem 'impressionist'

Terminal


$ bundle install

Next, create a table that counts the number of PVs.

Terminal


$ rails g impressionist

Terminal


$ rails db:migrate

You will have the following imperssions table.

schema.rb


create_table "impressions", force: :cascade 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.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index"
  t.index ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index"
  t.index ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index"
  t.index ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index"
  t.index ["impressionable_type", "impressionable_id", "params"], name: "poly_params_request_index"
  t.index ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index"
  t.index ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index"
  t.index ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index"
  t.index ["user_id"], name: "index_impressions_on_user_id"
end

2. Add count column to Tweets table

Terminal


$ rails g migration AddImpressionsCountToTweets impressions_count:integer

Add default: 0 to the following migration file.

~_add_impressions_count_to_tweets.rb


class AddImpressionsCountToTweets < ActiveRecord::Migration[6.0]
  def change
    # 「default:Added "0"
    add_column :users, :impressions_count, :integer, default: 0
  end
end

Terminal


$ rails db:migrate

3. Edit the model

tweet.rb


#Postscript
is_impressionable counter_cache: true

is_impressionable ➡︎ Enables ʻimpressionist` to be used in Tweet models.

counter_cache: true ➡︎ Make sure the impressions_count column is updated.

4. Edit controller

tweets_controller.rb



def index
  @tweets = Tweet.all
  @rank_tweets = Tweet.order(impressions_count: 'DESC') #Added sorting function
end

def show
  @tweet = User.find(params[:id])
  impressionist(@tweet, nil, unique: [:ip_address]) #Postscript
end

Tweet.order(impressions_count: 'DESC') ➡︎ Sort the tweet list in descending order of the number of PVs.

impressionist(@user, nil, unique: [:ip_address]) ➡︎ When you access the tweet details page, the number of PVs increases by one.

** * This time, the number of PV will be counted by ip_address so that the number of PV cannot be increased voluntarily. ** **

** * If you try rails s on localhost, ip_address will have the default :: 1, so the number of PV will not exceed 1. However, please be assured that we have confirmed that it works properly after deployment. ** **

5. Edit the view

erb:app/tweets/index.html.erb



<h3>Post list</h3>
<% @tweets.each do |t| %>
  <%= t.~ %>
  
  #Number of PV
  <%= t.impressions_count %>
<% end %>


<h3>PV number ranking</h3>
<% @rank_tweets.each do |t| %>
  <%= t.~ %>
  
  #Number of PV
  <%= t.impressions_count %>
<% end %>

Recommended Posts

Get PV (views) using Impressionist ~ Rails
[Rails] Implementation of PV number ranking using impressionist
Get Youtube channel information with Rails app (using Yt gem)
Get UserAgent in [Rails] controller
Search function using [rails] ransack
Try using view_component with rails
SNS authentication using Rails google
[Rails] Save images using carrierwave
Japaneseize using i18n with Rails
[Rails] Japanese localization using rails-i18n
Get Twitter images using twitter4j
[Rails] Test code using Rspec
Organize Rails routing using draw
Ajax bookmark function using Rails
Error when using rails capybara
[Rails] Try using Faraday middleware
Detailed tips when using Rails
[Rails 6] Star-shaped review using Raty.js