【问题标题】:Who's There? - Tracking Site Visits谁在那里? - 跟踪现场访问
【发布时间】:2015-10-16 09:51:52
【问题描述】:

我正在使用 sinatra 和 heroku 编写一个网站,我想找到一种方法来跟踪对我网站的每次访问。我看过实际的分析程序(例如google analytics)并选择不使用它们,因为我想自己学习如何做。

我对访问的定义:

当某人或某物(机器人)访问您的网站时,就会发生访问。它由一个或多个页面浏览量/点击量组成。一位访问者可以多次访问您的网站。

来源:http://www.opentracker.net/article/hits-or-pageviews

对于每次访问,我想跟踪:

  1. 访客IP地址
  2. 时间访问开始(页面已打开)
  3. 时间访问已结束(页面已关闭)

这个网站的访问频率不高,所以我想将每次访问记录在一个使用 activerecord 访问的 postgres 数据库中。 日志记录的工作方式是这样的:

  1. 用户访问页面
  2. 会话开始,ipmac_addresstimeview_id登录Visit
  3. 查看的每个页面都登录PageView
  4. 用户关闭页面
  5. 会话被清除,timeview_id登录Visit

数据库格式

  • 访问(表)
    • ip(列,字符串)
    • mac_address(列,字符串)
    • view_id(列,整数)
    • 时间(列、日期时间)
  • 页面浏览量(表格)
    • 页面(列,字符串)
    • 时间(列、日期时间)
    • view_id(列,整数)

示例迁移文件:

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

【问题讨论】:

  • 我认为未经他的许可,您将无法获得任何客户端 mac-add。
  • 扩展上述内容:MAC 地址是 layer-2 construct(仅限),您在路由互联网上看不到它。
  • 那么我可以向客户请求吗?还是我应该从问题中删除它?
  • 您可以在 javascript 中获取客户端的 MAC 地址,如果他们正在运行 Windows 并允许您安装 ActiveX 控件,请检查此stackoverflow.com/questions/5074139/…
  • @authprivate 谢谢,我已将其添加到问题中。

标签: postgresql activerecord logging sinatra


【解决方案1】:

对于每次访问,我想跟踪:

  1. 访问者 IP 地址
  2. 时间访问开始(页面已打开)
  3. 时间访问已结束(页面已关闭)

您之前在列表中也有 MAC 地址,但重申一下 - 它们不用于路由互联网,仅用于本地网络,因此即使您可以获取该信息,保存该信息也几乎没有意义。

HTTP 是一种无状态协议,这意味着#3 无法通过 HTTP 方法实现,但可以通过 javascript 实现。可能最简单的方法是以可接受的时间间隔进行轮询,更新时间。

#1 和 #2 已经被您的基本服务器日志捕获,它们将是我会使用的 - 为什么要重复努力? - 但我会通过模型添加如何使用 Sinatra 来完成。

如果您使用before 过滤器,您可以轻松捕获#1 和#2。 Request object 有一些你想要的东西,你需要时间,并确保它是该 ip 的唯一用户:

before do
  # this is pseudo code, Sequel style, you can work this bit out
  # for ActiveRecord
  user =
    if user_id = session[:user]
      User[user_id]
    else
      User.create
    end

  # you may want to check if there's an existing session for this page
  # as refreshes would run this again. It's up to you.
  user.add_visit Visit.create(page: request.path,ip: request.ip, start: Time.now.rfc2822])
  session[:analytics] = visit.id
  session[:user] = user.session_id # *don't* just bung the
                                   # user id in there
end

你需要一个路线来记录结束时间

patch "/analytics", :provides => :json do
  visit_id = session[:analytics]
  user = User[ :session_id => session[:user] ]
  visit = user.visits.find(:id => visit_id)
  visit.end = Rack::Utils.rfc2822(params[:end])
  visit.save
  halt 204 # take your pick of success numbers
           # you should also check for errors
           # and check the input is valid
           # and you may want to return some JSON to the
           # calling javascript.
  # Also think about how to restrict access to this
  # route to only authorised callers. Since you're providing the
  # javascript, you can place variables in them by generating
  # parts on the fly and serving it via a Sinatra route etc.
end

我不会写 javascript,那应该很简单。

请注意,我基本上是从我的背后提取了这段代码,因此请考虑其中的任何或全部可能会中断和不稳定,但它是为了让您明白这一点。就像我上面提到的那样,我可能会删除大部分内容并使用日志和一些明智的正则表达式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2011-06-23
    • 2015-11-18
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2011-03-19
    相关资源
    最近更新 更多