SmartSurf! (Or How to Suggest a Product Based on a Search Engine Query)
SmartSurf! (Or How to Suggest a Product Based on a Search Engine Query)

SmartSurf!

(Or How to Suggest a Product Based on a Search Engine Query)

by Jason Withrow

I was thinking one day, wouldn't it be nice if any given web site knew what I was specifically looking for when I arrived? I think it would be very helpful.

Then I started thinking about it a little more, and decided it was time to do something about it. At first, I sat down and wrote a letter to my congressman, but then decided he probably would have no idea what I was talking about, so I would have to resort to getting down and dirty with the code myself.

My Father-in-law runs a small e-commerce site that I designed. Which was a perfect platform to test out my new idea once completed. I sat down, wrote the code, uploaded it to the server and tested it out. Much to my chagrin, it worked perfectly, and proved to be a very good idea.

Most web trend articles say that, if a user does not find what they are looking for in 3 clicks or less, the majority of them will move on to greener pastures. This code will bring them to exactly where they want to go in 1 click.

Once you get this uploaded onto the server, make sure you check out my "Where Do They Come From?" article, as it will help you test out this code by giving you a list of search engine queries that have hit your site.

In my case, say a user searched Yahoo for Fireplace Accessories. When they followed the link from Yahoo to my Father-in-laws site, they would receive a page that looks like the screenshot below:

SmartSurf Sample Screen Shot

They could then click on any of the suggestive links to go directly to the part of the site that contains the information they are after.

The first thing you will need to do is to set up a session level variable in your global.asa file. Place it in you Sub Session_onStart() routine.




Sub Session_onStart()

Referer = Trim(Request.ServerVariables("HTTP_REFERER"))

    If Referer = "" Then

	    Referer = "None"

    End If

    Session("Referer") = Referer

End Sub

Now that we have that all set up, let's dive right into the meat of our application and create the internal functions and routines to perform the tasks at hand.

Since URL's are encoded, the first task at hand was to format the referrer's Host header Referrer data back into something useful. Let's start with a simple decode function (If you have read my other article "Where do they come from?", this may look a bit familiar).

Let's create a new ASP page and name it decode.asp, it will be the housing for our functions.

Our first function will be used to decode the encoded Refererer header.

It is pretty straightforward and quite lengthy so you probably just want to copy and paste it.




' Decode the urlEncoded Query String Referer data

Function URLDecode(str)



str = uCase(str)

str = Replace(str, "%20", " ")

str = Replace(str, "%21", "!")

str = Replace(str, "%22", Chr(34))

str = Replace(str, "%23", "#")

str = Replace(str, "%24", "$")

str = Replace(str, "%25", "%")

str = Replace(str, "%26", "&")

str = Replace(str, "%27", "'")

str = Replace(str, "%28", "(")

str = Replace(str, "%29", ")")

str = Replace(str, "%2A", "*")

str = Replace(str, "%2B", "+")

str = Replace(str, "%2C", ",")

str = Replace(str, "%2D", "-")

str = Replace(str, "%2E", ".")

str = Replace(str, "%2F", "/")

str = Replace(str, "%30", "0")

str = Replace(str, "%31", "1")

str = Replace(str, "%32", "2")

str = Replace(str, "%33", "3")

str = Replace(str, "%34", "4")

str = Replace(str, "%35", "5")

str = Replace(str, "%36", "6")

str = Replace(str, "%37", "7")

str = Replace(str, "%38", "8")

str = Replace(str, "%39", "0")

str = Replace(str, "%3A", ":")

str = Replace(str, "%3B", ";")

str = Replace(str, "%3C", "<")

str = Replace(str, "%3D", "=")

str = Replace(str, "%3E", ">")

str = Replace(str, "%3F", "?")

str = Replace(str, "%40", "@")

str = Replace(str, "%41", "A")

str = Replace(str, "%42", "B")

str = Replace(str, "%43", "C")

str = Replace(str, "%44", "D")

str = Replace(str, "%45", "E")

str = Replace(str, "%46", "F")

str = Replace(str, "%47", "G")

str = Replace(str, "%48", "H")

str = Replace(str, "%49", "I")

str = Replace(str, "%4A", "J")

str = Replace(str, "%4B", "K")

str = Replace(str, "%4C", "L")

str = Replace(str, "%4D", "M")

str = Replace(str, "%4E", "N")

str = Replace(str, "%4F", "O")

str = Replace(str, "%50", "P")

str = Replace(str, "%51", "Q")

str = Replace(str, "%52", "R")

str = Replace(str, "%53", "S")

str = Replace(str, "%54", "T")

str = Replace(str, "%55", "U")

str = Replace(str, "%56", "V")

str = Replace(str, "%57", "W")

str = Replace(str, "%58", "X")

str = Replace(str, "%59", "Y")

str = Replace(str, "%5A", "Z")

str = Replace(str, "%5B", "[")

str = Replace(str, "%5C", "\")

str = Replace(str, "%5D", "]")

str = Replace(str, "%5E", "^")

str = Replace(str, "%5F", "_")

str = Replace(str, "%60", "`")

str = Replace(str, "%61", "a")

str = Replace(str, "%62", "b")

str = Replace(str, "%63", "c")

str = Replace(str, "%64", "d")

str = Replace(str, "%65", "e")

str = Replace(str, "%66", "f")

str = Replace(str, "%67", "g")

str = Replace(str, "%68", "h")

str = Replace(str, "%69", "i")

str = Replace(str, "%6A", "j")

str = Replace(str, "%6B", "k")

str = Replace(str, "%6C", "l")

str = Replace(str, "%6D", "m")

str = Replace(str, "%6E", "n")

str = Replace(str, "%6F", "o")

str = Replace(str, "%70", "p")

str = Replace(str, "%71", "q")

str = Replace(str, "%72", "r")

str = Replace(str, "%73", "s")

str = Replace(str, "%74", "t")

str = Replace(str, "%75", "u")

str = Replace(str, "%76", "v")

str = Replace(str, "%77", "w")

str = Replace(str, "%78", "x")

str = Replace(str, "%79", "y")

str = Replace(str, "%7A", "z")

str = Replace(str, "%7B", "{")

str = Replace(str, "%7C", "|")

str = Replace(str, "%7D", "}")

str = Replace(str, "%7E", "~")

str = Replace(str, "%io", "h")

str = Replace(str, "%A0", " ")

str = Replace(str, "%A1", "¡")

str = Replace(str, "%A2", "¢")

str = Replace(str, "%A3", "£")

str = Replace(str, "%A4", "¤")

str = Replace(str, "%A5", "¥")

str = Replace(str, "%A6", "¦")

str = Replace(str, "%A7", "§")

str = Replace(str, "%A8", "¨")

str = Replace(str, "%A9", "©")

str = Replace(str, "%AA", "ª")

str = Replace(str, "%AB", "«")

str = Replace(str, "%AC", "")

str = Replace(str, "%AD", "­")

str = Replace(str, "%AE", "®")

str = Replace(str, "%AF", "¯")

str = Replace(str, "%B0", "°")

str = Replace(str, "%B1", "±")

str = Replace(str, "%B2", "²")

str = Replace(str, "%B3", "³")

str = Replace(str, "%B4", "´")

str = Replace(str, "%B5", "µ")

str = Replace(str, "%B6", "")

str = Replace(str, "%B7", "·")

str = Replace(str, "%B8", "¸")

str = Replace(str, "%B9", "¹")

str = Replace(str, "%BA", "º")

str = Replace(str, "%BB", "»")

str = Replace(str, "%BC", "¼")

str = Replace(str, "%BD", "½")

str = Replace(str, "%BE", "¾")

str = Replace(str, "%BF", "¿")

str = Replace(str, "%C0", "À")

str = Replace(str, "%C1", "Á")

str = Replace(str, "%C2", "Â")

str = Replace(str, "%C3", "Ã")

str = Replace(str, "%C4", "Ä")

str = Replace(str, "%C5", "Å")

str = Replace(str, "%C6", "Æ")

str = Replace(str, "%C7", "Ç")

str = Replace(str, "%C8", "È")

str = Replace(str, "%C9", "É")

str = Replace(str, "%CA", "Ê")

str = Replace(str, "%CB", "Ë")

str = Replace(str, "%CC", "Ì")

str = Replace(str, "%CD", "Í")

str = Replace(str, "%CE", "Î")

str = Replace(str, "%CF", "Ï")

str = Replace(str, "%D0", "Ð")

str = Replace(str, "%D1", "Ñ")

str = Replace(str, "%D2", "Ò")

str = Replace(str, "%D3", "Ó")

str = Replace(str, "%D4", "Ô")

str = Replace(str, "%D5", "Õ")

str = Replace(str, "%D6", "Ö")

str = Replace(str, "%D7", "×")

str = Replace(str, "%D8", "Ø")

str = Replace(str, "%D9", "Ù")

str = Replace(str, "%DA", "Ú")

str = Replace(str, "%DB", "Û")

str = Replace(str, "%DC", "Ü")

str = Replace(str, "%DD", "Ý")

str = Replace(str, "%DE", "Þ")

str = Replace(str, "%DF", "ß")

str = Replace(str, "%E0", "à")

str = Replace(str, "%E1", "á")

str = Replace(str, "%E2", "â")

str = Replace(str, "%E3", "ã")

str = Replace(str, "%E4", "ä")

str = Replace(str, "%E5", "å")

str = Replace(str, "%E6", "æ")

str = Replace(str, "%E7", "ç")

str = Replace(str, "%E8", "è")

str = Replace(str, "%E9", "é")

str = Replace(str, "%EA", "ê")

str = Replace(str, "%EB", "ë")

str = Replace(str, "%EC", "ì")

str = Replace(str, "%ED", "í")

str = Replace(str, "%EE", "î")

str = Replace(str, "%EF", "ï")

str = Replace(str, "%F0", "ð")

str = Replace(str, "%F1", "ñ")

str = Replace(str, "%F2", "ò")

str = Replace(str, "%F3", "ó")

str = Replace(str, "%F4", "ô")

str = Replace(str, "%F5", "õ")

str = Replace(str, "%F6", "ö")

str = Replace(str, "%F7", "÷")

str = Replace(str, "%F8", "ø")

str = Replace(str, "%F9", "ù")

str = Replace(str, "%FA", "ú")

str = Replace(str, "%FB", "û")

str = Replace(str, "%FC", "ü")

str = Replace(str, "%FD", "ý")

str = Replace(str, "%FE", "þ")

str = Replace(str, "%FF", "ÿ")

str = Replace(str, "+", " ")



    URLDecode = lCase(str)

End Function

Wow, I was starting to think that would never end.

The next thing we want to do is to separate the query string portion of the referrer header, since that is where the information we are most concerned with will be.




' Function to just get the query String part of the referring URL

Function isProduct(pStr)

    If pStr <> "" And lCase(pStr) <> "none" Then

        'Search the string backwards

        temp = inStrRev(pStr, "/")



        'get the position of the directory separator

        tempStr = Right(pStr, temp)



        'get the length data we are concerned with

        temp2 = Len(pStr)



        'Get the string of query string data

        pStr = Mid(pStr, temp, temp2)



        'set the return value of the function

        isProduct = pStr

    Else

        isProduct = ""

    End If

End Function

Ok, Now we have just the query string part of the referring website. Now what? Well, I am glad you asked! Now we need to set up some specific criteria to look for in that string.

We accomplish that by setting up a static 2 dimensional array. One dimension to hold the string to search for in the referrer data. The other to hold the link to our product for that data.




' Array TO Hold your Product and Link Data

' The 0 Indexes are the text to match with the referrer data

' and is also what will be displayed to the user.

' The 1 Indexes are the url to build for that match.

Dim pArray(41, 1)

pArray(0, 0) = "Fireplace Accessories" 

pArray(0, 1) = "list.asp?frmSearch=fireplace" 

pArray(1, 0) = "Glass Doors" 

pArray(1, 1) = "list.asp?frmSearch=Glass+Doors"

pArray(2, 0) = "Fireplace Screen" 

pArray(2, 1) = "list.asp?frmSearch=Fireplace+Screens"

pArray(3, 0) = "Hearth Tools" 

pArray(3, 1) = "list.asp?frmSearch=tools" 

pArray(4, 0) = "Fireplace Damper" 

pArray(4, 1) = "list.asp?frmSearch=Dampers"

pArray(5, 0) = "FirePlace" 

pArray(5, 1) = "list.asp?frmSearch=fireplace"

pArray(6, 0) = "Damper" 

pArray(6, 1) = "list.asp?frmSearch=Dampers" 

pArray(7, 0) = "Fireplace Tools" 

pArray(7, 1) = "list.asp?frmSearch=Tools"

pArray(8, 0) = "Log Rack" 

pArray(8, 1) = "list.asp?frmSearch=Log+Rack" 

pArray(9, 0) = "Woodstove Supplies" 

pArray(9, 1) = "list.asp?frmSearch=woodstove"

pArray(10, 0) = "Blowers" 

pArray(10, 1) = "list.asp?frmSearch=blower" 

pArray(11, 0) = "Chimney Liner" 

pArray(11, 1) = "chim_liners.asp"

pArray(12, 0) = "Firebacks" 

pArray(12, 1) = "list.asp?end=2#Firebacks%2D+Cast+Iron+Pennsylvania"

pArray(13, 0) = "Chimney Cap" 

pArray(13, 1) = "list.asp?frmSearch=Rain+Caps"

'People Misspell this Frequently

pArray(14, 0) = "Fireplace Accesories"

pArray(14, 1) = "list.asp?frmSearch=fireplace"

pArray(15, 0) = "Fireplace Rug" 

pArray(15, 1) = "list.asp?frmSearch=rug"

pArray(16, 0) = "Woodstove Accessories" 

pArray(16, 1) = "list.asp?frmSearch=woodstove"

'People Misspell this Frequently

pArray(17, 0) = "Woodstove Accesories"

pArray(17, 1) = "list.asp?frmSearch=woodstove"

pArray(18, 0) = "Gas Log" 

pArray(18, 1) = "list.asp?frmSearch=gas+log"

pArray(19, 0) = "Hearth Rug" 

pArray(19, 1) = "list.asp?frmSearch=rug"

pArray(20, 0) = "Rugs" 

pArray(20, 1) = "list.asp?frmSearch=rug"

pArray(21, 0) = "Glass Fronts" 

pArray(21, 1) = "list.asp?frmSearch=glass+door"

pArray(22, 0) = "Bellows"   

pArray(22, 1) = "list.asp?frmSearch=Bellow" 

pArray(23, 0) = "Rain Caps"

pArray(23, 1) = "list.asp?frmSearch=Rain+Cap" 

pArray(24, 0) = "Rain Covers"  

pArray(24, 1) = "list.asp?frmSearch=Rain+Cap" 

pArray(25, 0) = "Pipe Heaters"

pArray(25, 1) = "list.asp?frmSearch=heater" 

pArray(26, 0) = "Downdrafts"   

pArray(26, 1) = "list.asp?frmSearch=Downdrafts"  

pArray(27, 0) = "Downdraft Caps"  

pArray(27, 1) = "list.asp?frmSearch=Downdrafts" 

pArray(28, 0) = "Stop Downdrafts"  

pArray(28, 1) = "list.asp?frmSearch=Downdrafts" 

pArray(29, 0) = "Downdraft Smoking" 

pArray(29, 1) = "list.asp?frmSearch=Downdrafts" 

pArray(30, 0) = "Back Puffing"

pArray(30, 1) = "list.asp?frmSearch=Downdrafts" 

pArray(31, 0) = "Puffing Back" 

pArray(31, 1) = "list.asp?frmSearch=Downdrafts" 

pArray(32, 0) = "Log Racks"

pArray(32, 1) = "list.asp?frmSearch=Log+Rack" 

pArray(33, 0) = "Log Holders"

pArray(33, 1) = "list.asp?frmSearch=wood+holder" 

pArray(34, 0) = "Wood holders"

pArray(34, 1) = "list.asp?frmSearch=wood+holder" 

pArray(35, 0) = "Log Tools"   

pArray(35, 1) = "list.asp?frmSearch=tools"

pArray(36, 0) = "Chimney Cover"   

pArray(36, 1) = "list.asp?frmSearch=rain+caps"      

pArray(37, 0) = "Spark Arrestor"   

pArray(37, 1) = "list.asp?frmSearch=rain+caps"

pArray(38, 0) = "Fireplace Doors"   

pArray(38, 1) = "list.asp?frmSearch=doors"

pArray(39, 0) = "Heater"   

pArray(39, 1) = "list.asp?frmSearch=Heater"

pArray(40, 0) = "Fatwood"   

pArray(40, 1) = "list.asp?frmSearch=Fatwood"

Now all that is left is to write the function that will search the referrer data, looking for matches from the 1st dimension in our array.




'Pass you Array into this function and look for with the

'query string data received from the referrer

Function Finder(byRef prodList, byVal refList)

    ' Make search case insensitive

    refList = lCase(refList)



    ' Loop Thru Array, Pulling out matches as they are found

    For i = 0 To uBound(prodList) - 1

        If inStr(refList, lCase(prodList(i, 0))) Then

            'Found a Match!

            tHolder = tHolder & "<b>Are You looking For " _

                & "<a href=" & prodList(i, 1) & ">" _

                & prodList(i, 0) & "</a></b><br>"

        End If

    'Loop thru next array index

    Next



    'return results

    Finder = tHolder

End Function

Now all we have to do is include our decode.asp page in any page we want to have this functionality on and place function calls.

Like so:




<!--#include file="decode.asp"-->



'If the referrer header is Not empty, Call the functions

If lCase(Session("Referer")) <> "none" OR Session("Referer") <> "" Then

' Pass array into Finder Function, along with decoded

' isProduct (query String part of referrer)



' Parsed referrer data

    Response.Write vbCrLf & "<center><p><font color=green>" _

        & Finder(pArray, URLDecode(isProduct(Session("Referer")))) _

        & "</font></p></center>" & vbCrLf

End If

That's it. Download the zipped files or follow along and write it yourself.

Note: If you use my other article "Where Do They Come From?" to build an application that you can use to check the search engine referrer hits on your site, and also to test this code out, you will have to copy the search engine link from the siteLog.asp page, and then close your browser before navigating to the copied URL. This has to do with the way sessions are managed in IIS and is not a coding issue.

Regards, - Jason Withrow

Close    To Top
  • Prev Article-Web Design:
  • Next Article-Web Design:
  • Now: Tutorial for Web and Software Design > Web Design > ASP > Web Design Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction