0

I have a website with google Analytics, which provides information about from where visitors are coming.

My website has a registration form, and I would like to know from where the new register is coming (but without asking him).

For example, suppose a user were searching "cars" in google.com, clicked my link (not adwords), and were registering in my website. How and from where can I read and save that this user was looking for "cars" in google.com and clicked in my link? Are there any cookies generated by google?

sawa
  • 165,429
  • 45
  • 277
  • 381
Joseph
  • 55
  • 2
  • 7

1 Answers1

1

You can store the referrer where the user came from in its session:

# in application_controller.rb
before_filter :store_referrer

private
def store_referrer
  referer = request.referer.presence
  if referer && !referer.start_with?('http://your.domain')
    session[:referer] ||= referer 
  end
end

When the user signs up, you just pass that value to the user:

User.new(params[:user].merge(:referer => session[:referer]))

You User should have a referer= method that handles that url.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • And then, the OP needs to parse `session[:referer]`, and if the host is `google.com` or related, then take out the query value corresponding to `q`. – sawa Jul 19 '14 at 10:52
  • Is it possible to use <%= raw request.referer %> ? or request.env['HTTP_REFERER'] ? – Joseph Jul 19 '14 at 13:04
  • The functions request.referer and request.env['HTTP_REFERER`] is working inside of my localhost:3090 but if I go to google.com and I write in my URL bar localhost:3090 the request.referer and HTTP_REFERER are empty... this funciones are only working inside of my domain 'localhost:3090' ??? – Joseph Jul 19 '14 at 13:12
  • `request.referer` and `request.env['HTTP_REFERER']` are only set if you click a link. If you enter the URL manually into the address bar then the referrer is always nil. Since there are no link to localhost on Google, you cannot test this behavior. – spickermann Jul 19 '14 at 13:51
  • OK I think that I am going for the good way... only a last question, how is it possible to know the request.referer that is OUTSIDE of your domain. I mean to take only the value when the URL is not your domain. – Joseph Jul 19 '14 at 14:04
  • @Joseph You check that the referer does not start with your domain name: `unless request.referer.start_with?('http://your.domain')` – spickermann Jul 19 '14 at 14:09
  • seems that start_with and end_with is out of date as I read in this article https://github.com/rails/rails/commit/c0bb4c6ed2f5a5676569746e7bfd70405346ef8f#activesupport/lib/active_support/core_ext/string/starts_ends_with.rb – Joseph Jul 19 '14 at 14:42