我想知道,如何在Rails中获得这样的统计数据?
由于我想在这些页面下显示这些信息,我不想使用任何外部库,我希望我们可以有一些红宝石可以做到这一点。
谢谢
编辑1-我可以看到像https://github.com/jkrall/analytical这样的1-2个宝石,但仍然想问SO社区。
编辑2-我不认为上面的创业板是我正在寻找的,它看起来只是插入第三方分析到现有的应用程序。
发布于 2013-03-02 12:28:00
将地编码器 gem添加到Gemfile和bundle install中。
创建一个Visit模型,其中包含属性page、ip_address和location。
对于有问题的页面,在相关控制器中放置一个前置筛选器,或者如果您想记录对每个页面的访问,请将其放在您的ApplicationController中:
def record_visit
Visit.create(page: request.fullpath, ip_address: request.ip, location: request.location.country_code)
endGeocoder将location方法添加到请求对象中,因此,如果您需要的不仅仅是国家代码,就在文档中读取。
然后,您可以将以下内容插入控制器(同样是在before_filter中),从而显示特定页面上的视图数,但这必须在前面的筛选器之后运行:
def count_views
@views = Visit.where(page: request.fullpath).count
end由于您将大量运行此查询,在创建访问模型时,您可能希望在页面属性上添加一个索引。
add_index :visits, :page独特的视图是很棘手的,因为您当然可以拥有来自同一个IP地址的多个访问者。您可以将cookie设置为record_visit方法的一部分,如果存在cookie,则不创建新的访问。
def record_visit
if cookies['app-name-visited']
return
else
cookies['app-name-visited'] = true
Visit.create(page: request.fullpath, ip_address: request.ip, location: request.location.country_code)
end
endhttps://stackoverflow.com/questions/15174109
复制相似问题