Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom Web Visitor Tracking using C#

0.00/5 (No votes)
6 Sep 2004 4  
When the IIS log is not sufficient for your web analytics , you can use this simple technique to track the extra data about visitors of your web page

Introduction

When you want to collect more data about your website visitors and client platform which is not provided by IIS log files you can use this simple technique to do that. If you are able to collect the data from the server side and if you have full control over the server side page creation code probably this technique will not be required at all for you.

Background

I started this simple task when I wanted to track a web document ID which the IIS logs did not log when user is requesting the pages. (By the way our site had dynamic content in JSP, each page with unique id). Whenever a user navigates through the pages in our site we want to track the document id he / she is visiting. The collected data is stored in SQL Server.

Using the code

1.

Decide what kind of data you want to collect. (Client browser information and platform information are provided by IIS logs. You can run a basic web log tool to extract this information.) In our case document ID, timestamp, and URL referrer is the data that we want to collect.

2.

Create a database table with the data fields which you are capturing. You Also need to create a stored procedure to insert the data.

3.

Include the following line of html in your web pages which you are trying to track the information. <img src='http://servername/dirname/webform.aspx?param1=val1&param2=val2' width="0" height="0">. The height and width attributes are 0 here because the source is not an actual image. We are tricking the browser to request this page with an image tag.

4.

Write an aspx application and in the Form load function extract the query string values and insert them into the database.

/*Script for generating DB object for the sample project 
  attached. Copy paste the following  section in to your sql server
  enterprise manager and run it. */
    if exists (
        select * from dbo.sysobjects 
        where id = object_id(N'[dbo].[usr_log]') 
        and OBJECTPROPERTY(id, N'IsProcedure') = 1)
            drop procedure [dbo].[usr_log]

GO

    if exists (
        select * from dbo.sysobjects 
        where id = object_id(N'[dbo].[UsrLog]') 
        and OBJECTPROPERTY(id, N'IsUserTable') = 1)
            drop table [dbo].[UsrLog]

GO

CREATE TABLE [dbo].[UsrLog] (
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [timesmp] [datetime] NULL ,
 [Platform] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [ClrVersion] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [IsCrawler] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
 [RemoteHost] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL 
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER ON 
GO

SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [usr_log] 
  @Platform varchar(256),
  @BowserVersion varchar(256),
  @ClrVersion varchar(256),
  @IsCrawler varchar(256),
  @RemoteHost varchar(256)
AS
BEGIN
 insert into UsrLog (Platform, 
                     BrowserVersion,
                     ClrVersion,
                     IsCrawler,
                     RemoteHost) 
 values (@Platform , 
         @BowserVersion, 
         @ClrVersion,
         @IsCrawler, 
         @RemoteHost)
END
GO

SET QUOTED_IDENTIFIER OFF 
GO

SET ANSI_NULLS ON 
GO

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here