Search code examples
jqueryruby-on-railsanalyticsweb-traffic

It it possible to detect the source of traffic using Rails/jQuery?


I want to detect whether a user came to my site directly or via a link, and if they used a link then I want to detect what url they came from.

This must be possible somehow, because analytics software can do it. It it achievable with Rails or jQuery?


Solution

  • Yes and no.

    Some clients send a header called referer (Yes its a misspelling). You can access the header through document.referer in javascript or request.referer in Rails.

    However you cannot rely on the presence of the header as many clients do not send it. For a very good reason as it can be used to track users - often without their knowledge or consent.

    The referer header can also be spoofed so using it in any kind of authentication scheme is not a good idea.

    Another approach is by attaching a query parameter to links with a tracking code. This requires you to be in control of the incoming links.