I am writing a website with sinatra and heroku, and I want to find a way to track every visit to my site. I have seen actual analytics programs (e.g. google analytics) and have chosen not to use them because I would like to learn how to do this myself.
My definition of a visit:
A visit happens when someone or something (robot) visits your site. It consists of one or more page views/ hits. One visitor can make multiple visits to your site.
Source: http://www.opentracker.net/article/hits-or-pageviews
For each visit, I would like to track:
- Visitor IP address
- Time visit began (page was opened)
- Time visit ended (page was closed)
This website is not viewed very often and so I would like to log each visit in a postgres database accessed with activerecord. The way that logging would work would be this:
- User accesses page
- Session is started,
ip
,mac_address
,time
, andview_id
are logged inVisit
- Each page viewed is logged in
PageView
- User closes page
- Session is cleared,
time
andview_id
are logged inVisit
DATABASE FORMAT
- Visits (Table)
- ip (Column, string)
- mac_address (Column, string)
- view_id (Column, int)
- time (Column, datetime)
- PageViews (Table)
- page (Column, string)
- time (Column, datetime)
- view_id (Column, int)
Sample Migration File:
class Main < ActiveRecord::Migration
def change
create_table :visits do |item|
item.string :ip
item.string :mac_address
item.datetime :time
item.int :visit_id
end
create_table :pageviews do |item|
item.int :visit_id
item.string :page
item.datetime :time
end
end