How to save http referer in rails
Asked Answered
P

3

17

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_referer

  def save_referer
    unless is_logged_in?
      session['referer'] = request.env["HTTP_REFERER"] unless session['referer']
    end
  end

Then when a user is created, it checks this session variable and sets it to nil. Sometimes this does not work and I'm worried there might be some unintended things happening with using session like this. Does anyone have a better way? Or some input perhaps?

EDIT: This is the logic I am using to save the referer:

def create     
    @user = User.new(params[:user])  
    if @user.save_with(session[:referer])
    ....
end

User

def save_with(referer)
    self.referer = referer unless referer == "null"
    self.save   
end

Is there any reason why this should not work?

Pearlstein answered 26/2, 2010 at 13:27 Comment(3)
"sometimes, this does not work" <- what does it do ? It's normal if you don't always have a referer. If the user enters your url directly, there's no referer.Stook
Was this in Rails 3 by any chance?Kendry
Also, would you mind posting the code you got working? As in, did you eliminate everything above - including the stuff in the create action and just replace it all with the code in the accepted answer in your application controller? Do you still use the method save_with(referer) for instance? I am trying to do the same thing, so would love to know how you were able to solve this, if you did. Thanks.Kendry
K
22

I think there's a flaw in your approach. As long as the user is hitting pages and is not logged in, the filter code will run. So the only way session['referer'] will not be nil is if they go straight to the signup page where they (presumably) post their login info and you check the session var.

I think you probably need to only check the referer once - to do this you'll have to modify your filter code.

def save_referer
  unless is_logged_in?
    unless session['referer']
      session['referer'] = request.env["HTTP_REFERER"] || 'none'
    end
  end
end

Now when you want to know what their referer is, it will either be a valid url or 'none'. Note that since it's in the session, it's not perfect: they could go to another url and come back and the session will still be valid.

Kaycekaycee answered 26/2, 2010 at 21:39 Comment(2)
Right, thanks for the help. I considered the stale session problem and am not sure what to do about it. It seems this would be something others would have tackled but I don't see any blogs or other info online about it.Pearlstein
How does he then retrieve the referer for use elsewhere? Does he have to save session['referer'] to his User model in the db, then retrieve it like User.referer when he wants it?Kendry
E
2
def save_referer
  session['referer'] = request.env["HTTP_REFERER"] || 'none' unless session['referer'] && !is_logged_in?
end

beautiful ;-)

Elfin answered 9/3, 2010 at 2:17 Comment(1)
nice. shouldnt that be unless is_logged_in? though and not !is_logged_in? I only want to save the referrer if they are not logged in.Pearlstein
C
0

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.

Causey answered 25/4, 2015 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.