开发者

views count calculation

could you give me idea how to calculate unique views of page? If i will increase counter on each page load it will be no good, otherside storing information of viewed pages in session looks like not ideal solution.开发者_StackOverflow


If you don't want to increase the counter on every single hit, but only on the first time some user visists, you must store it somewhere. I would store it in a database table and store the session_id and the page in this table. Then I would increase the counter only if the current session haven't already visited the current page and then store it to the database.


use & search for the GOOGLE ANALYTICS.


This question is quite old, but what about is_visitable?


I've done similar things using a before filter and a page_views table.

Here's a migration to create the table:

class PageViews < ActiveRecord::Migration
  def change
    create_table :page_views do |t|
      t.string :controller
      t.string :action
      t.timestamps
    end
  end
end

And a model class: /app/models/page_view.rb:

class PageView < ActiveRecord::Base
  attr_accessible :action, :controller
end

Then in the application_controller add a before_filter to insert a record with every request:

class ApplicationController < ActionController::Base
  before_filter :track_page_request
  def track_page_request
    PageView.create({:controller => params[:controller], :action => params[:action]})
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜