Click here to Skip to main content
15,889,595 members
Articles / Web Development / ASP.NET
Article

Custom Web Visitor Tracking using C#

Rate me:
Please Sign up or sign in to vote.
1.29/5 (19 votes)
6 Sep 20042 min read 70.8K   1.2K   27   5
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.

SQL
/*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 ,
 [BrowserVersion] [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



Comments and Discussions

 
QuestionHow to Create Website Issue Tracking Application Pin
Member 1070825516-Sep-14 0:15
Member 1070825516-Sep-14 0:15 
GeneralIt is a good trick Pin
Nirosh23-Oct-06 23:13
professionalNirosh23-Oct-06 23:13 
QuestionWhat i can is bad explaination Pin
vivekthangaswamy17-Sep-04 21:56
professionalvivekthangaswamy17-Sep-04 21:56 
GeneralCollect User Data?! Pin
Anonymous7-Sep-04 8:04
Anonymous7-Sep-04 8:04 
GeneralRe: Collect User Data?! Pin
Haja7-Sep-04 18:21
Haja7-Sep-04 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.