views:

169

answers:

2

I want to create dynamic content based on this. I know it's somewhere, as web analytics engines can get this data to determine how people got to your site (referrer, search terms used, etc.), but I don't know how to get at it myself.

+8  A: 

You can use the "referer" part of the request that the user sent to figure out what he searched for. Example from Google:

http://www.google.no/search?q=stack%20overflow

So you must search the string (in ASP(.NET) this can be found be looking in Request.Referer) for "q=" and then URLDecode the contents.

Also, you should take a look at this article that talks more about referrers and also other methods to track your visitors:

http://www.15seconds.com/issue/021119.htm

Espo
A: 

This is some code to backup the idea of using a querystring method and if that's not available using the UrlReferrer property of the Request object. This can then be stashed in a session object (or somewhere else if that works better for you) so that you can track the source between pages. (Page_Load doesn't seem to be formatted correctly inside the code sample here)

public void Page_Load(Object Sender, EventArgs E) {
      if (null == Session["source"] || Session["source"].ToString().Equals(string.Empty)) {
       if (Request.QueryString["src"] != null) {
        Session["source"] = Server.UrlDecode(Request.QueryString["src"].ToString());
       } else {
        if (Request.UrlReferrer != null) {
         Session["source"] = Request.UrlReferrer.ToString();
        } else {
         Session["source"] = string.Empty;
        }
       }
      }}
Jared