简单点击计数器的轨道页面浏览量 [英] simple hit counter for page views in rails

查看:151
本文介绍了简单点击计数器的轨道页面浏览量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了这个问题的几个解决方案,例如railstat从这篇文章:

Rails中的页面浏览量



我有一堆文章和评论,我想要一个计数器过滤器通过独特的IP。完全像Stackoverflow这个职位。但是我并不真正关心railstat这样的解决方案,当谷歌分析已经为我做了这些工作并包括了大量的代码,跟踪了独特的IP等。我目前的想法是使用Garb或其他一些分析工具插件来拉动页面的统计信息,如果他们的年龄比说12小时更新某​​些表格,但我还需要一个cache_column。



我假设您可以从Google Analytics对于一个特定的页面,他们每12小时更新他们的统计信息?



我想知道这是不是一个好主意,或者如果有人有一个更好的解决方案?



谢谢

解决方案

/ strong>



此答案中的代码被用作 http://github.com/charlotte-ruby/impressionist
试一下






这可能会减少你的时间e将其编码到您的应用程序中,然后使用他们的API从Google Analytics中提取数据。这些数据很可能会更准确,您不必依赖外部依赖性。此外,您还可以实时获得统计信息,而不必等待Google Analytics数据12小时。 request.remote_ip 工作得很好。这是一个使用多态的解决方案。请注意,此代码未经测试,但应该接近。



创建新模型/迁移以存储您的网页浏览量(展示次数):

 类展示次数< ActiveRecord :: Base 
belongs_to:可变的,:polymorphic => true
end
$ b $ class CreateImpressionsTable< ActiveRecord :: Migration
def self.up
create_table:impressions,:force => true do | t |
t.string:impressionable_type
t.integer:impressionable_id
t.integer:user_id
t.string:ip_address
t.timestamps
end
end
$ b $ def self.down
drop_table:展示次数
结束
结束

为您的文章模型添加一行关联,并添加一个返回展示次数的方法:

  class文章< ActiveRecord :: Base 
has_many:展示次数,:as =>:可感染的

def impression_count
Impressions.size
结束

def unique_impression_count
#impressions.group(:ip_address).size giving => {'127.0.0.1'=> 9,'0.0.0.0'=> 1}
#因此从哈希中获取密钥并计算密钥数量
impressions.group(:ip_address)。 size.keys.length #TESTED
end
end

创建一个before_filter articles_controller上的show动作:

  before_filter:log_impression,:only => [:show] 

def log_impression
@article = Article.find(params [:id])
#假设您在认证系统中有一个current_user方法
@ article.impressions.create(ip_address:request.remote_ip,user_id:current_user.id)
end

然后您只需在您的视图中调用unique_impression_count

 <%= @ article.unique_impression_count%> 

如果你在一堆模型上使用它,你可能想干掉它。将before_filter def放在application_controller中,并使用如下动态内容:

  impressionable_class = controller_name.gsub(Controller,)。 constantize 
impressionable_instance = impressionable_class.find(params [:id])
impressionable_instance.impressions.create(ip_address:request.remote_ip,user_id:current_user.id)

同时将Article模型中的代码移动到将包含在ActiveRecord :: Base中的模块中。你可以把send包放在一个config / initializer中,或者如果你想变得疯狂,只需把整个事情变成一个rails引擎,这样你就可以在其他应用上重复使用。

 模块可展示的
def is_impressionable
has_many:展示次数,:as =>:可感染的
包含InstanceMethods
结束
模块InstanceMethods
def impression_count
impressions.size
end

def unique_impression_count
impressions.group(:ip_address).size
end
结束
结束

ActiveRecord :: Base.extend可印象


I've found several solutions for this problem, for example railstat from this post:

Page views in Rails

I have a bunch of articles and reviews which I would like a hit counter filtered by unique IPs. Exactly like Stackoverflow does for this post. But I don't really care for such a solution as railstat when google analytics is already doing this for me and including a whole lot of code, keeping track of unique IPs, etc.. My present thinking is to use Garb or some other Analytics plugin to pull the pages stats if they are older than say 12 hours updating some table, but I also need a cache_column.

I'm assuming you can pull stats from Analytics for a particular page and that they update their stats every 12 hours?

I'm wondering if there are any reasons why this would be a bad idea, or if someone has a better solution?

Thanks

解决方案

UPDATE

The code in this answer was used as a basis for http://github.com/charlotte-ruby/impressionist Try it out


It would probably take you less time to code this into your app then it would to pull the data from Analytics using their API. This data would most likely be more accurate and you would not have to rely an an external dependancy.. also you would have the stats in realtime instead of waiting 12 hours on Analytics data. request.remote_ip works pretty well. Here is a solution using polymorphism. Please note that this code is untested, but it should be close.

Create a new model/migration to store your page views (impressions):

class Impressions < ActiveRecord::Base
  belongs_to :impressionable, :polymorphic=>true 
end

class CreateImpressionsTable < ActiveRecord::Migration
  def self.up
    create_table :impressions, :force => true do |t|
      t.string :impressionable_type
      t.integer :impressionable_id
      t.integer :user_id
      t.string :ip_address
      t.timestamps
    end
  end

  def self.down
    drop_table :impressions
  end
end

Add a line to your Article model for the association and add a method to return the impression count:

class Article < ActiveRecord::Base
  has_many :impressions, :as=>:impressionable

  def impression_count
    impressions.size
  end

  def unique_impression_count
    # impressions.group(:ip_address).size gives => {'127.0.0.1'=>9, '0.0.0.0'=>1}
    # so getting keys from the hash and calculating the number of keys
    impressions.group(:ip_address).size.keys.length #TESTED
  end
end

Create a before_filter for articles_controller on the show action:

before_filter :log_impression, :only=> [:show]

def log_impression
  @article = Article.find(params[:id])
  # this assumes you have a current_user method in your authentication system
  @article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id)
end

Then you just call the unique_impression_count in your view

<%=@article.unique_impression_count %>

If you are using this on a bunch of models, you may want to DRY it up. Put the before_filter def in application_controller and use something dynamic like:

impressionable_class = controller_name.gsub("Controller","").constantize
impressionable_instance = impressionable_class.find(params[:id])
impressionable_instance.impressions.create(ip_address:request.remote_ip,user_id:current_user.id)

And also move the code in the Article model to a module that will be included in ActiveRecord::Base. You could put the send include in a config/initializer.. or if you want to get crazy, just turn the whole thing into a rails engine, so you can reuse on other apps.

module Impressionable
  def is_impressionable
    has_many :impressions, :as=>:impressionable
    include InstanceMethods
  end
  module InstanceMethods
    def impression_count
      impressions.size
    end

    def unique_impression_count
      impressions.group(:ip_address).size
    end
  end
end

ActiveRecord::Base.extend Impressionable

这篇关于简单点击计数器的轨道页面浏览量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆