Tracking user navigation on a web site
Typically you put up a website and hope everything goes well and is used. It is often a good idea to track the pages that users actually go to, and from where. A novice developer would likely add code to every page to record this information. A better developer may add code to a master page to do the same. A better (and simpler) solution is to just drop a IHttpModule on to the website and have it record. If you produce many websites, then just compile it to a DLL and add it to each
The code is very simple, as shown below.
- using System;
- using System.Web;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- public class RequestTracking : IHttpModule
- {
- private HttpApplication httpApp;
- private string _Connection;
- public void Init(HttpApplication httpApp)
- {
- this.httpApp = httpApp;
- httpApp.BeginRequest += new EventHandler(httpApp_BeginRequest);
- _Connection = ConfigurationManager.ConnectionStrings["logdb"].ConnectionString;
- }
- void httpApp_BeginRequest(object sender, EventArgs e)
- {
- using (var sp = new SqlCommand("sp_LogRequest", new SqlConnection(_Connection)) { CommandType = CommandType.StoredProcedure })
- {
- sp.Parameters.AddWithValue("RelPath", httpApp.Context.Request.AppRelativeCurrentExecutionFilePath);
- if (httpApp.Context.Session != null)
- {
- sp.Parameters.AddWithValue("Session", httpApp.Context.Session.SessionID);
- }
- sp.Parameters.AddWithValue("IPAddress", httpApp.Request.UserHostAddress);
- try
- {
- sp.Connection.Open();
- sp.ExecuteNonQuery();
- }
- catch { }
- finally { sp.Connection.Close(); }
- }
- }
- public void Dispose()
- { }
- }
The amount of information capture is sparse:
- The requested file as a relative path
- The SessionId (if a session exists)
- The Client IP Address
You can add a lot more information if you wish. The information is inserted into a SQL with three tables, the page reference table:
- PageId Int Identity(1,1)
- PageUrl varchar(255)
The Session reference table:
- SId Int Identity(1,1)
- SessionId varchar(255)
- ClientIP varchar(22)
And into the log table:
- LogId int Identity (1,1)
- PageId
- Sid
- ReceivedTime Datetime
This allows you to see how long the client spent on each page, what page they go to next, etc. A useful exercise is often to create a chart of all of the pages and the percentage of time they go between each. For example, you can use Visio to generate a site map, now just add the % of times between each page to each link. The results may cause you to restructure the website or identify pages that are not being used as expected.
Or if you have access to your raw log files you could use this...
ReplyDeletehttp://www.smartertools.com/smarterstats/web-analytics-seo-software.aspx