views:

18

answers:

1

Hello everyone!

I am trying to find the most effective way to parse my System.Web.HTTPRequest.UrlReferrer to find the search term that was used to drive the visitor to my site assuming they arrived at my site via a search engine results page.

I am thinking (hoping) I'd use the HttpUtility.ParseQueryString method, but my problem comes in trying to figure out what variable to parse for. I have the following list that I am required to check for. The list shows the search engine and the param that each uses.

daum:q,
eniro:search_word,
naver:query,
images.google:q,
google:q,
yahoo:p,
msn:q,
bing:q,
aol:query,
aol:encquery,
lycos:query,
ask:q,
altavista:q,
netscape:query,
cnn:query,
about:terms,
mamma:query,
alltheweb:q,
voila:rdata,
virgilio:qs,
live:q,
baidu:wd,
alice:qs,
yandex:text,
najdi:q,
aol:q,
mama:query,
seznam:q,
search:q,
wp:szukaj,
onet:qt,
szukacz:q,
yam:k,
pchome:q,
kvasir:q,
sesam:q,
ozu:q,
terra:query,
mynet:q,
ekolay:q,
rambler:words

So while I could go through each and say something like

NameValueCollection query = HttpUtility.ParseQueryString(UrlReferrer);
var referrer = Request.UrlReferrer.ToString();

if(referrer.Contains("google.com")
     return (query["q"]);
else if(referrer.Contains("yahoo.com")
     return (query["p"]);

I'm thinking there must be a better way since I have this nice name/value pair to work against,and the power of the ParseQueryString method, but I'm drawing a blank.

A: 

Well, I am not happy with it, and I don't think I achieved efficiency, but this is what I ended up doing. I'd still love to hear from you guys with better approaches.

private string GetKeywords(string urlReferrer)
    {
        var searchQuery = string.Empty;
        var url = new Uri(urlReferrer);
        var query = HttpUtility.ParseQueryString(urlReferrer);
        switch (url.Host)
        {
            case "google":
            case "daum":
            case "msn":
            case "bing":
            case "ask":
            case "altavista":
            case "alltheweb":
            case "live":
            case "najdi":
            case "aol":
            case "seznam":
            case "search":
            case "szukacz":
            case "pchome":
            case "kvasir":
            case "sesam":
            case "ozu":
            case "mynet":
            case "ekolay":
                searchQuery = query["q"];
                break;
            case "naver":
            case "netscape":
            case "mama":
            case "mamma":
            case "terra":
            case "cnn":
                searchQuery = query["query"];
                break;
            case "virgilio":
            case "alice":
                searchQuery = query["qs"];
                break;
            case "yahoo":
                searchQuery = query["p"];
                break;
            case "onet":
                searchQuery = query["qt"];
                break;
            case "eniro":
                searchQuery = query["search_word"];
                break;
            case "about":
                searchQuery = query["terms"];
                break;
            case "voila":
                searchQuery = query["rdata"];
                break;
            case "baidu":
                searchQuery = query["wd"];
                break;
            case "yandex":
                searchQuery = query["text"];
                break;
            case "szukaj":
                searchQuery = query["wp"];
                break;
            case "yam":
                searchQuery = query["k"];
                break;
            case "rambler":
                searchQuery = query["words"];
                break;
            default:
                searchQuery = query["q"];
                break;
        }
        return searchQuery;
    }
ColoradoRockie