在Rails中设置跟踪(永久)cookie的位置?

时间:2013-04-04 09:21:29

标签: ruby-on-rails ruby cookies

我正在尝试跟踪“匿名”用户的操作,并最终在注册后将其与帐户相关联。我想我会通过设置一个永久性cookie来做到这一点。我最好的选择是在ApplicationController中包含以下内容:

class ApplicationController < ActionController::Base
  before_filter :set_tracking_cookie
  def set_tracking_cookie
    cookies.permanent[:user_uuid] = SecureRandom.uuid unless cookies[:user_uuid]
  end
end

这是正确的方法还是有更好的解决方案?

1 个答案:

答案 0 :(得分:7)

看起来很好,永久性cookie在将来(20年左右)到期,所以只要用户没有手动删除它,你就应该能够跟踪他。

我在很多地方都使用过这样的结构,它就像魅力一样。如果您包含要通过此操作加载的内容(典型的跟踪像素),您甚至可以在外部着陆页上使用它。

相关问题