65.9K
CodeProject is changing. Read more.
Home

Get search key word from the referrer url.

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (16 votes)

Nov 15, 2010

CPOL
viewsIcon

70943

This is a very simple way to do actually. Web developers are very much familiar to work with the referrer url. The HTTP_REFERER server variable return the referrer url. In this tips I will show how easily we can get the search key word from the referrer url.

Well, lets take an example, you have a web site and the people come to your site from a search engin (Google, Bing) and now you want to know which word are used for the search and which search engin are used as well.
public string GetSearchKeyWords(string strQuery)
{
    string result = "";
    string pattern = "\\b\\w*p=(?!u)\\w*\\b|\\b\\w*q=(?!u)\\w*\\b|\\b\\w*qs=(?!u)\\w*\\b"
            + "|\\b\\w*encquery=(?!u)\\w*\\b|\\b\\w*k=(?!u)\\w*\\b\\b\\w*qt=(?!u)\\w*\\b"
            + "|\\b\\w*query=(?!u)\\w*\\b|\\b\\w*rdata=(?!u)\\w*\\b|\\b\\w*search_word=(?!u)\\w*\\b"
            + "|\\b\\w*szukaj|terms=(?!u)\\w*\\b\\b\\w*text=(?!u)\\w*\\b|\\b\\w*wd=(?!u)\\w*\\b|\\b\\w*words=(?!u)\\w*\\b";
    foreach (Match m in Regex.Matches(strQuery, pattern)) {
        // get the matched string
        string x = m.ToString();
        x = x.Substring(1, x.Length - 1);
        // collect all text
        result += x;
    }
    return result.Replace("=", "");
}

Input: http://www.google.com/url?sa=t&source=web&cd=7&ved=0CEQQFjAG&url=http%3A%2F%2Fwww.example.com%2F&rct=j&q=codeproject.com=uSLVTMm0JYXNhAewqbzjBA&usg=AFQjCNFtfSFIuj4fxnl8bD_bR2NgzDePGw&cad=rja

Output: 
codeproject.com