Click here to Skip to main content
Licence 
First Posted 6 Sep 2004
Views 43,657
Bookmarked 27 times

Custom Web Visitor Tracking using C#

By | 6 Sep 2004 | Article
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
 
Part of The SQL Zone sponsored by
See Also

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 ,
 [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

About the Author

Haja

Web Developer

India India

Member

Me, Haja Maidee, people calls me Haja painter by heart, electronics engineer by studies, software engineer by profession. Married and blessed with lovely wife and daughter. Working as Software Dev Manager, in Intel Bangalore for last 5 years, Got 9+ yeaers experince in microsoft technologies for enterprise application integration and business to business integration.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIt is a good trick PinmemberNirosh23:13 23 Oct '06  
QuestionWhat i can is bad explaination Pinmembervivekthangaswamy21:56 17 Sep '04  
GeneralCollect User Data?! PinsussAnonymous8:04 7 Sep '04  
GeneralRe: Collect User Data?! PinmemberHaja18:21 7 Sep '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 7 Sep 2004
Article Copyright 2004 by Haja
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid