Tuesday, May 18, 2004

Web Stats Tracking Script

Here's a groovy little script if you'd rather log your web stats to your own database than use a third party tracking script:


var asUrl = 'http://www.mysite.com/myscript.php?' +
'&url=' + escape(document.location.href) + '&pv=1' +
'&r=' + escape(document.referrer) +
'&dt=' + new Date().valueOf();
var bug = new Image();
bug.src = asUrl;


Paste that script onto your pages (or, preferably, put it into a javscript include file and use the SRC property of the SCRIPT tag to include it onto the page. It sends the URL of the page and the referrer to your script.

Here's a handy function that extracts the search keywords from the referrer if the referrer is a search engine. It captures all of the major search engines (Google, Yahoo, MSN, etc.) and most of the smaller ones. For your convenience I've included both the PHP and ASP.NET versions.

PHP Version

function getSearchKeywords($url)
{
$keywords = "";
$qvar = "";

if (strlen($url) == 0)
{
return "";
}

$url = trim($url);

$qloc = strpos($url, "?");
if (strpos($url, "/search/web"))
{
$keywords = substr($url, strpos($url, "/search/web/") + 12);
if (strpos($keywords, "/"))
{
$keywords = substr($keywords, 0, strpos($keywords, "/"));
}
$keywords = urldecode($keywords);
$keywords = str_replace(",", "", $keywords);
$keywords = str_replace(".", "", $keywords);
$keywords = str_replace("-", "", $keywords);
$keywords = str_replace("+", " ", $keywords);
$keywords = str_replace('"', "", $keywords);
return $keywords;
}
elseif (substr($url, 0, 24) == "http://search.yahoo.com/")
{
if (strpos($url, "&p=", $kloc + 1))
{
$kloc = strpos($url, "&p=", $kloc + 1);
}
if (!$kloc)
{
$kloc = strpos($url, "?p=");
}
$qvar = "p";
}
elseif (strpos($url, "&query=") | strpos($url, "?query="))
{
$kloc = strpos($url, "&query=");
if (strpos($url, "&query=", $kloc + 1))
{
$kloc = strpos($url, "$query=", $kloc + 1);
}
if (!$kloc)
{
$kloc = strpos($url, "?query=");
}
$qvar = "query";
}
elseif (strpos($url, "&searchfor=") | strpos($url, "?searchfor="))
{
$kloc = strpos($url, "&searchfor=");
if (strpos($url, "&searchfor=", $kloc + 1))
{
$kloc = strpos($url, "&searchfor=", $kloc + 1);
}
if (!$kloc)
{
$kloc = strpos($url, "?searchfor=");
}
$qvar = "searchfor";
}
else
{
$kloc = strpos($url, "&q=");
if (strpos($url, "&q=", $kloc + 1))
{
$kloc = strpos($url, "&q=", $kloc + 1);
}
if (!$kloc)
{
$kloc = strpos($url, "?q=");
}
$qvar = "q";
}

if (!$qloc | !$kloc)
{
return "";
}

$nextloc = strpos($url, "&", $kloc + 1);
if (!$nextloc)
{
$nextloc = strlen($url);
}

$keywords = substr($url, $kloc + strlen($qvar) + 2,
$nextloc - ($kloc + strlen($qvar) + 2));
$keywords = urldecode($keywords);
$keywords = str_replace(" ", " ", $keywords);
$keywords = str_replace(",", "", $keywords);
$keywords = str_replace(".", "", $keywords);
$keywords = str_replace("-", "", $keywords);
$keywords = str_replace("+", " ", $keywords);
$keywords = str_replace('"', "", $keywords);
$keywords = trim($keywords);

return $keywords;

}


ASP.NET Version

Public Function GetSearchKeywords(ByVal Url As String) As String

Dim keywords As String = ""
Dim kloc As Int32
Dim qloc As Int32
Dim nextloc As Int32
Dim qvar As String = ""

If Url Is Nothing Then
Return ""
End If

Url = Url.Trim()

qloc = Url.IndexOf("?")
If Url Like "*/search/web/*" Then
keywords = Url.Substring(Url.IndexOf("/search/web/") + 12)
If keywords.IndexOf("/") > -1 Then
keywords = keywords.Substring(0, keywords.IndexOf("/"))
End If
keywords = System.Web.HttpUtility.UrlDecode(keywords)
keywords = keywords.Replace(",", "")
keywords = keywords.Replace(".", "")
keywords = keywords.Replace("-", "")
keywords = keywords.Replace("+", " ")
keywords = keywords.Replace(Chr(34), "")
Return keywords
ElseIf Url.StartsWith("http://search.yahoo.com/") Then
kloc = Url.IndexOf("&p=")
If Url.IndexOf("&p=", kloc + 1) > -1 Then
kloc = Url.IndexOf("&p=", kloc + 1)
End If
If kloc = -1 Then
kloc = Url.IndexOf("?p=")
End If
qvar = "p"
ElseIf Url.IndexOf("&query=") > -1 Or _
Url.IndexOf("?query=") > -1 Then
kloc = Url.IndexOf("&query=")
If Url.IndexOf("&query=", kloc + 1) > -1 Then
kloc = Url.IndexOf("&query=", kloc + 1)
End If
If kloc = -1 Then
kloc = Url.IndexOf("?query=")
End If
qvar = "query"
ElseIf Url.IndexOf("&searchfor=") > -1 Or _
Url.IndexOf("?searchfor=") > -1 Then
kloc = Url.IndexOf("&searchfor=")
If Url.IndexOf("&searchfor=", kloc + 1) > -1 Then
kloc = Url.IndexOf("&searchfor=", kloc + 1)
End If
If kloc = -1 Then
kloc = Url.IndexOf("?searchfor=")
End If
qvar = "searchfor"
Else
kloc = Url.IndexOf("&q=")
If Url.IndexOf("&q=", kloc + 1) > -1 Then
kloc = Url.IndexOf("&q=", kloc + 1)
End If
If kloc = -1 Then
kloc = Url.IndexOf("?q=")
End If
qvar = "q"
End If

If qloc = -1 Or kloc = -1 Then
Return ""
End If

nextloc = Url.IndexOf("&", kloc + 1)
If nextloc = -1 Then
nextloc = Url.Length
End If

keywords = System.Web.HttpUtility.UrlDecode( _
Url.Substring(kloc + Len(qvar) + 2, _
nextloc - (kloc + Len(qvar) + 2))).Replace(" ", " ")
keywords = keywords.Replace(",", "")
keywords = keywords.Replace(".", "")
keywords = keywords.Replace("-", "")
keywords = keywords.Replace("+", " ")
keywords = keywords.Replace(Chr(34), "")
keywords = keywords.Trim

Return keywords

End Function