Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version
Go to top

Page Tracking in ASP.NET

, 23 Jun 2004
This articles explains how to track visitors to your web site.
using System;
using System.Collections;
using System.Web;

using EAD.Constant;

namespace EAD.Utility
{
	/// <summary>
	/// Summary description for PageTracker.
	/// </summary>
	[Serializable]
	public class PageTracker
	{
		#region ctor
		public PageTracker(HttpContext ctx)
		{
			string KEY = ctx.Session.SessionID;

			pages = new ArrayList();
			expires = DateTime.Now.AddYears(1);
			sessionReferrer = (ctx.Request.UrlReferrer == null) ? string.Empty : ctx.Request.UrlReferrer.ToString();
			sessionURL = (ctx.Request.Url == null) ? string.Empty : ctx.Request.Url.ToString();
			if (SessionConst.Page[KEY] == null)
			{
				visitCount = 1;
				originalReferrer = sessionReferrer;
				originalURL = sessionURL;
				SessionConst.Page.Add(KEY,new PageHashData(visitCount,sessionReferrer,sessionURL));
			}
			else
			{
				PageHashData pageHashData = (PageHashData) SessionConst.Page[KEY];
				pageHashData.PageCount++;
				SessionConst.Page.Remove(KEY);
				SessionConst.Page.Add(KEY,pageHashData);

				visitCount = ((PageHashData)SessionConst.Page[KEY]).PageCount;
				originalReferrer = ((PageHashData)SessionConst.Page[KEY]).Referrer;
				originalURL = ((PageHashData)SessionConst.Page[KEY]).Url;
			}
			userHostAddress = ctx.Request.UserHostAddress.ToString();
			userAgent = ctx.Request.UserAgent.ToString();
			browser = ctx.Request.Browser.Browser;
			crawler = ctx.Request.Browser.Crawler.ToString();
			ipCity = ctx.Request.Params["IPCity"];
			ipRegion = ctx.Request.Params["IPRegion"];
			ipCountry = ctx.Request.Params["IPCountry"];
			if (ipCity == null || ipCity.Length == 0)
			{
				ipCity = string.Empty; 
			}
			if (ipRegion == null || ipRegion.Length == 0)
			{
				ipRegion = string.Empty; 
			}
			if (ipCountry == null || ipCountry.Length == 0)
			{
				ipCountry = string.Empty; 
			}
		}
		#endregion

		#region Methods
		public void AddPage(string pageName)
		{
			PageData pti = new PageData();
			pti.PageName = pageName;
			pti.Time = DateTime.Now;
			pages.Add(pti);
		}
		#endregion

		#region Private Variables
		private DateTime expires = DateTime.Now;

		private int visitCount = 0;
		string originalReferrer = string.Empty, originalURL = string.Empty, sessionURL = string.Empty;
		string userHostAddress = string.Empty, userAgent = string.Empty, sessionReferrer = string.Empty;
		string browser = string.Empty, crawler = string.Empty;
		string ipCity = string.Empty, ipRegion = string.Empty, ipCountry = string.Empty; 
		ArrayList pages = null;
		#endregion

		#region Properties
		public int VisitCount
		{
			get
			{
				return visitCount;
			}
		}
		
		public string OriginalReferrer
		{
			get
			{
				return originalReferrer;
			}
		}

		public string OriginalURL
		{
			get
			{
				return originalURL;
			}
		}

		public string SessionReferrer
		{
			get
			{
				return sessionReferrer;
			}
		}

		public string SessionURL
		{
			get
			{
				return sessionURL;
			}
		}

		public string SessionUserHostAddress
		{
			get
			{
				return userHostAddress;
			}
		}

		public string SessionUserAgent
		{
			get
			{
				return userAgent;
			}
		}

		public string Browser
		{
			get
			{
				return browser;
			}
		}
		public string Crawler
		{
			get
			{
				return crawler;
			}
		}

		public ArrayList Pages
		{
			get
			{
				return pages;
			}
		}
		public string IPCity
		{
			get
			{
				return ipCity;
			}
		}
		public string IPRegion
		{
			get
			{
				return ipRegion;
			}
		}
		public string IPCountry
		{
			get
			{
				return ipCountry;
			}
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

vikramk
Web Developer
United States United States
Vikram is an Enterprise Application Architect specializing in EAI, ETL, all relational databases and transforming legacy applications to Microsoft .Net environment. Vikram works for a consulting firm in Research Triangle Park, NC. Vikram has expertise in all relational databases, Cobol, mainframe, OO programming, C, Perl and Linux. C# is a newfound craze for Vikram.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 24 Jun 2004
Article Copyright 2004 by vikramk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid