How to save http referer in rails

后端 未结 3 706
悲哀的现实
悲哀的现实 2020-12-14 03:36

I\'m trying to save the site that a user came from when they sign up. Right now I have a before_filter in my ApplicationController:

before_filter :save_refe         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 03:43

    Jonathan:s answer works when using sessions. In case you wan't better information you should also use cookies. User can visit your site from a link (with a referer) then go away for a day and return directly to your site again (now without a referer). Would be better to save information also to a cookie in following style

    def save_referer
      if cookies[:referer].blank?
        cookies.permanent[:referer] = request.env["HTTP_REFERER"] || 'none'
      end
    end
    

    There is also a gem https://github.com/holli/referer_tracking to help handle these and other trackings automatically.

提交回复
热议问题