Forum Moderators: DixonJones
If I wanted to do this, I would have a setup where I log visitors myself in a database (which I do now).
I really wish I knew more about this. I realize all this information is in the log file, however I would like a nice visual. I have no clue how to do what you stated though. Its nice to have it automatically update in realtime.
This is my dumb way (which might happen to be the right way).
When someone hits your website from a search engine link, the search terms are usually in the URL.
So all you have to do is get the referer:
$referer = $_SERVER[HTTP_REFERER];
Then you parse it into parts to get the query string:
$parseurl = parse_url($referer);
So for a google search:
[google.com...]
$parseurl[host] = google.com
$parseurl[query] = q=widgets
Then you parse the query string into its individual variables to get the search term:
$string = parse_str($parseurl[query])
$string[q] = widgets
The you just update your db with the search engine (host) and the search term.
HTH,
Chip-
Function insertSearch(intDomain, strSearchString)
insertSearch = execute( "sp_insertReferSearch", Array( _
Array( "domainId", adVarChar, adParamInput, Len( intDomain ), intDomain ), _
Array( "searchString", adVarChar, adParamInput, Len( strSearchString ), strSearchString ), _
Array( "userIp", adVarChar, adParamInput, Len( Request.ServerVariables("REMOTE_ADDR") ), Request.ServerVariables("REMOTE_ADDR") )))
End Function
REM -- Grab the referer string
strReferer = replace(Request.ServerVariables("http_referer"), "http://", "")
strCurrentPage = replace(Request.ServerVariables("SERVER_NAME"), "http://", "") & Request.ServerVariables("SCRIPT_NAME") & "?" & Request.QueryString()
if Session("bInitialReferer") <> 2 then
bInitialHit = true
end if
REM -- only care if there is a? or if it is an internal search
if ((bInitialHit or instr(strCurrentPage, "www.widget.com/search.asp?")) AND (instr(strReferer, "?") and instr(strReferer, "www.widget.com") = 0) or (len(Request("widgetName")) > 0 and instr(strCurrentPage, "www.widget.com/search.asp?"))) then
if instr(strCurrentPage, "www.widget.com/search.asp?") then
arrSplitURL = Split(replace(strCurrentPage, "/search.asp", ""), "?")
else
arrSplitURL = Split(strReferer, "?")
end if
strRefURL = arrSplitURL(0)
strQS = arrSplitURL(1)
REM -- Regular Expression
set objRegEx = New RegExp
objRegEx.Global = true
objRegEx.IgnoreCase = true
REM -- used below to see if it is a matching Domain Name we care about
bStoredURL = True
Select Case strRefURL
case "www.google.com/search", "google.com/search"
objRegEx.Pattern = "q=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "q=", "")
next
intDomain = 1
case "search.yahoo.com/search", "search.yahoo.com/search"
objRegEx.Pattern = "p=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "p=", "")
next
intDomain = 2
REM -- Internal search
case "www.widget.com", "widget.com"
objRegEx.Pattern = "widgetName=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "widgetName=", "")
next
intDomain = 3
case "search.msn.com/results.asp", "search.msn.com/results.aspx"
objRegEx.Pattern = "q=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "q=", "")
next
intDomain = 4
case "www.business.com/search/rslt_default.asp", "www.business.com/search/rslt_websites.asp"
objRegEx.Pattern = "query=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "query=", "")
next
intDomain = 5
case "search.lycos.com/default.asp"
objRegEx.Pattern = "query=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "query=", "")
next
intDomain = 6
case "go.google.com/hws/search"
objRegEx.Pattern = "q=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "q=", "")
next
intDomain = 7
case "www.altavista.com/web/results"
objRegEx.Pattern = "q=.*&?"
set objREmatches = objRegEx.Execute(strQS)
for each REmatch in objREmatches
strSearchString = replace(REmatch, "q=", "")
next
intDomain = 8
case else
bStoredURL = false
end select
REM -- if is is a domain name we monitor, save the search
if bStoredURL then
arrRemoveAmp = Split(strSearchString, "&")
strFormat = replace(arrRemoveAmp(0), "+", " ")
insertSearch intDomain, strFormat
end if
end if