LoginSignup
0
0

More than 1 year has passed since last update.

【ruby on rails】 gem impressionistで追加したPV数カウント機能が動作しない場合がある

Last updated at Posted at 2021-12-03

0.概要

文章を投稿できるアプリをruby on rails で作り
投稿の詳細画面でpv数をカウントする機能を導入済み

しかし、一部の詳細画面ではPV数が追加されない

1. 前提(カウント機能追加までに実施したこと)

Gemfileに以下を導入しbundle install

gem 'impressionist', '~>1.6.1'

さらにターミナルで以下実施

rails g impressionist

以下が作成される

db/migrate/〇〇〇〇〇〇〇〇〇〇〇〇〇_create_impressions_table.rb
class CreateImpressionsTable < ActiveRecord::Migration[5.2]
  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_index :impressions, [:impressionable_type, :message, :impressionable_id], :name => "impressionable_type_message_index", :unique => false, :length => {:message => 255 }
    add_index :impressions, [:impressionable_type, :impressionable_id, :request_hash], :name => "poly_request_index", :unique => false
    add_index :impressions, [:impressionable_type, :impressionable_id, :ip_address], :name => "poly_ip_index", :unique => false
    add_index :impressions, [:impressionable_type, :impressionable_id, :session_hash], :name => "poly_session_index", :unique => false
    add_index :impressions, [:controller_name,:action_name,:request_hash], :name => "controlleraction_request_index", :unique => false
    add_index :impressions, [:controller_name,:action_name,:ip_address], :name => "controlleraction_ip_index", :unique => false
    add_index :impressions, [:controller_name,:action_name,:session_hash], :name => "controlleraction_session_index", :unique => false
    add_index :impressions, [:impressionable_type, :impressionable_id, :params], :name => "poly_params_request_index", :unique => false, :length => {:params => 255 }
    add_index :impressions, :user_id
  end

  def self.down
    drop_table :impressions
  end
end

さらにカウントさせる投稿(posts)テーブルに「impressions_count」カラムを追加

db/migrate/〇〇〇〇〇〇〇〇〇〇〇〇〇_add_impressions_count_to_posts.rb
class AddImpressionsCountToPosts < ActiveRecord::Migration[5.2]
  def change
    add_column :posts, :impressions_count, :integer, default: 0
  end
end

上記で

rails db:migrate

さらにカウント対象のpostsモデル、コントローラーに記述を追加する

app/models/post.rb
class Post < ApplicationRecord

  is_impressionable counter_cache: true 

end
app/controllers/posts_controller.rb
class PostsController < ApplicationController

 def index # 一覧画面
    @posts = Post.all
 end

 def show # 詳細画面
    @post = Post.find(params[:id])
    impressionist(@post, nil, unique: [:session_hash.to_s]) 
 end
....
end

show画面閲覧時にカウントさせる
to_sメソッド記載はSessionIdエラー防止のため

具体的なPV数は以下のようなindex画面で質問の概要と並べる形で表示させる

app/views/posts/index.html.erb

<% @posts.each do |post| %>
(~質問一覧~省略~)
 <%= post.impressions_count %>
(~~省略~)
<% end %>

2.PV更新有無

show画面確認→index画面へ戻るとPV数が1追加されてるが
一部更新されないものも存在
以下はpostのshow.html閲覧時にPV数が通常更新されるデータ(投稿id(post.id)が28のもの)を
使用データベース(自分の場合mysql)上で確認した結果である

※「rails g impressionist」でテンプレテーブルを作ったため
 使わないカラムが多いが、impressionable_idがpostテーブルのid(今回の場合28)を表す

MySQL [アプリ名]> select * from  impressions where impressionable_id=28;
+-----+---------------------+-------------------+---------+-----------------+-------------+-----------+------------------------------------------------------------------+----------------+----------------------------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------+---------------------+---------------------+
| id  | impressionable_type | impressionable_id | user_id | controller_name | action_name | view_name | request_hash                                                     | ip_address     | session_hash                     | message | referrer                                                                                                                                                                                   | params | created_at          | updated_at          |
+-----+---------------------+-------------------+---------+-----------------+-------------+-----------+------------------------------------------------------------------+----------------+----------------------------------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------+---------------------+---------------------+
| 312 | Post                |                28 |    NULL | posts           | show        | NULL      | 9238b0b3c610130cbc0b3334e16a4c1eb838330b596d2c6bbed5714cfd52b6a6 | 116.83.115.249 | 248e8c24dc2cdeacfa9832404436eb26 | NULL    | https://warerano3594.com/public/posts?utf8=%E2%9C%93&range=PV%E6%95%B0%E3%81%AE%E5%A4%9A%E3%81%84%E9%A0%86%E3%81%AB&commit=%E5%9B%9E%E7%AD%94%E4%B8%A6%E3%81%B9%E6%9B%BF%E3%81%88          | NULL   | 2021-12-02 06:30:57 | 2021-12-02 06:30:57 |
| 352 | Post                |                28 |    NULL | posts           | show        | NULL      | b37e953bd673cbbac98bf507882f4637eebd007f42890d41dfdb474ae2921cda | 116.83.115.249 | 54b8e2634cec17f36079eeca7a0a8bd0 | NULL    | https://warerano3594.com/public/posts?utf8=%E2%9C%93&range=%E6%8A%95%E7%A8%BFNo%E3%81%8C%E5%8F%A4%E3%81%84%E9%A0%86%E3%81%AB&commit=%E5%9B%9E%E7%AD%94%E4%B8%A6%E3%81%B9%E6%9B%BF%E3%81%88 | NULL   | 2021-12-02 07:26:06 | 2021-12-02 07:26:06 |
| 449 | Post                |                28 |    NULL | posts           | show        | NULL      | c3204d34ca373f27dcc7940014c3a8cbcac6ae01317edc14ca2853cc59a89f3a | 116.83.115.249 | 9c84102ac332dd37558a1e68f930055d | NULL    | https://warerano3594.com/public/posts?utf8=%E2%9C%93&range=PV%E6%95%B0%E3%81%AE%E5%B0%91%E3%81%AA%E3%81%84%E9%A0%86%E3%81%AB&commit=%E5%9B%9E%E7%AD%94%E4%B8%A6%E3%81%B9%E6%9B%BF%E3%81%88 | NULL   | 2021-12-02 08:47:53 | 2021-12-02 08:47:53 |

更新されない投稿内容画面(post/投稿id)の特徴を確認したところ、最新更新日時のレコードでpv数カウントに使用する「session_hash」カラムがnullとなっているものだと分かった

MySQL [アプリ名]> select *  from  impressions where session_hash is null;
+-----+---------------------+-------------------+---------+-----------------+-------------+-----------+------------------------------------------------------------------+---------------+--------------+---------+----------+--------+---------------------+---------------------+
| id  | impressionable_type | impressionable_id | user_id | controller_name | action_name | view_name | request_hash                                                     | ip_address    | session_hash | message | referrer | params | created_at          | updated_at          |
+-----+---------------------+-------------------+---------+-----------------+-------------+-----------+------------------------------------------------------------------+---------------+--------------+---------+----------+--------+---------------------+---------------------+
| 478 | Post                |                67 |    NULL | posts           | show        | NULL      | dfac460ae80d0693781edfe0de46c576ae612735c52907b3de2a85abf890407c | 34.219.180.21 | NULL         | NULL    | NULL     | NULL   | 2021-12-02 09:28:20 | 2021-12-02 09:28:20 |
| 479 | Post                |                69 |    NULL | posts           | show        | NULL      | 0f009e99f0ac6885adb6dadd69ef5487cc4c79facfce65e7a5be872ffd240903 | 54.187.149.38 | NULL         | NULL    | NULL     | NULL   | 2021-12-02 09:29:04 | 2021-12-02 09:29:04 |
| 480 | Post                |                74 |    NULL | posts           | show        | NULL      | cd99ffbbf1e06c9426dd24c1f14ca775cb400b67fd3ebb6f094370ec1abb4e03 | 34.220.223.62 | NULL         | NULL    | NULL     | NULL   | 2021-12-02 09:30:02 | 2021-12-02 09:30:02 |
| 484 | Post                |                68 |    NULL | posts           | show        | NULL      | 582e752304d947291ab514ddbc2bb602b585b7b89d741706c3cd5f67b1fdd573 | 52.26.112.150 | NULL         | NULL    | NULL     | NULL   | 2021-12-02 09:32:55 | 2021-12-02 09:32:55 |

3.対処

12/7現在session_hashが何故nullとなってしまうのか根本原因は未だわかっていない
(ツイッターから該当画面へのリンクを辿ることで発生することは確認)ため
対処療法となるが以下のように「session_hash」カラムがnullとなっているデータを削除

delete from  impressions where session_hash is null;

上記実施後に確認したところ、閲覧数カウントが止まっていたものが、再度増えていくようになった。

4.対処法2 (12/7追加)

コントローラファイルから
「unique: [:session_hash.to_s]」を消す方法でもPV数はカウントされるようになる。

作りが単純になるため
ぺージをリロードすると何回でも数えられてしまう状態とはなるが
PV数がカウントされない事象は解決される

app/controllers/posts_controller.rb
class PostsController < ApplicationController

 def index # 一覧画面
    @posts = Post.all
 end

 def show # 詳細画面
    @post = Post.find(params[:id])
    impressionist(@post, nil) # ←unique: [:session_hash.to_s] 削除
  end
....
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0