How to store where a new user was referred? Using Rails + Devise

I have a rails app that uses devise. I'm curious to know, is it possible in the User table to somehow track where a new user came from, the HTTP referrer?

I'd like to know which came from Facebook, Twitter, LinkedIn, Google+ in order to track a viral loop.

Any ideas? Seen anyone do this? Possible? Where should this live in the rails app? Still very new. Thanks


Solution 1:

It could be done like this. May require some tweaking and fixing but You'll get an idea Make before filter for Application controller, you will call it for any action

def landing_filter
   if from_other_site(request.referrer) and !session[:referer].blank?
      session[:referer] = request.referrer #you don't want to delete first entrance 
   end
end

from_other_site should be the method which will check domain name in referrer url, if it match your then return false, otherwise true

in devise/registration/new.erb.html view add in form hidden field

<%= f.hidden_field :referrer, session[:referrer] %>

and don't forget to add migration with new database field for user