用ASP.NET MVC 3实现一个访问统计系统

开发 后端
我们将介绍用ASP.NET MVC 3实现一个访问统计系统,包括统计代码和Cookie等方面,希望对大家有所帮助。

  运营网站,我们经常需要分析用户的行为、用户的习惯,用户看重网站的哪一部分,哪一部分是对用户有用的之类的信息,这些信息从哪里来,这时我们就需要用到访问统计系统了。 网上已经有很多的统计系统,如站长统计、百度统计、谷歌分析之类的,别人的东西始终是别人的,为什么我们不自己实现统计的功能呢,而且自己写的可以实现一些特殊的功能,如登录,下单行为,能够更好的融合自己的系统!

  下面我们就用ASP.NET MVC 3来实现一个访问统计系统!首先,使用程序生成一段js代码,包括读写Cookie,及写入一个唯一值到Cookie中,用来判断独立访客者,再写入访问次数及上一个访问页面ID值;然后就是获取客户端时间,分辨率之类的。

  1.   string guid = Guid.NewGuid().ToString();  
  2.   #region 生成js  
  3.   html.Append("function WriteCookie(cookieName, cookieValue, expiry) {"); //js写cookie  
  4.   html.Append(" var expDate = new Date();");  
  5.   html.Append(" if (expiry) {");  
  6.   html.Append(" expDate.setTime(expDate.getTime() + expiry);");  
  7.   html.Append(" document.cookie = cookieName + \"=\" + escape(cookieValue) + \"; path=/;expires=\" + expDate.toGMTString();");  
  8.   html.Append(" }");  
  9.   html.Append(" else {");  
  10.   html.Append(" document.cookie = cookieName + \"=\" + escape(cookieValue) + \"; path=/;\"");  
  11.   html.Append(" }");  
  12.   html.Append("}");  
  13.   html.Append("function ReadCookie(name) {"); //js读取cookie  
  14.   html.Append(" var cookieValue = \"\";");  
  15.   html.Append(" var search = name + \"=\";");  
  16.  html.Append(" if (document.cookie.length >0) {");  
  17.   html.Append(" offset = document.cookie.indexOf(search);");  
  18.   html.Append(" if (offset != -1) {");  
  19.   html.Append(" offset += search.length;");  
  20.   html.Append(" end = document.cookie.indexOf(\";\", offset);");  
  21.   html.Append(" if (end == -1)");  
  22.   html.Append(" end = document.cookie.length;");  
  23.   html.Append(" cookieValue = unescape(document.cookie.substring(offset, end))");  
  24.   html.Append(" }");  
  25.   html.Append(" }");  
  26.   html.Append(" return cookieValue;");  
  27.   html.Append("}");  
  28.   html.Append("DateFormatString = function(datetime, format) {");  
  29.   html.Append(" var year = datetime.getFullYear();");  
  30.   html.Append(" var month = datetime.getMonth()+1;");  
  31.   html.Append(" var date = datetime.getDate();");  
  32.   html.Append(" var hour = datetime.getHours();");  
  33.   html.Append(" var minutes = datetime.getMinutes();");  
  34.   html.Append(" var second = datetime.getSeconds();");  
  35.   html.Append(" format = format.replace(/YY/g, year).replace(/MM/g, month).replace(/DD/g, date).replace(/hh/g, hour).replace(/mm/g, minutes).replace(/ss/g, second);");  
  36.   html.Append(" return format;");  
  37.   html.AppendLine("}");  
  38.   html.Append("var cookieEnabled = (navigator.cookieEnabled) ? true : false;"); //判断浏览器是否支持cookie  
  39.   html.Append("if (typeof navigator.cookieEnabled == \"undefined\" &&!cookieEnabled) {");  
  40.   html.Append(" document.cookie = \"testcookie\";");  
  41.   html.Append(" cookieEnabled = (document.cookie == \"testcookie\") ? true : false;");  
  42.   html.Append(" document.cookie = \"\";");  
  43.   html.Append("}");  
  44.   html.Append("var firstshow;");  
  45.   html.Append("var visittotal;");  
  46.  html.Append("var isLogin;");  
  47.   html.Append("var loginName;");  
  48.   html.Append("if (cookieEnabled == true) {");  
  49.   html.Append(" var HBCountShowCookie = ReadCookie(\"HBCountShowCookie\");"); //访客浏览器唯一ID  
  50.   html.Append(" var HBCountVisitTotalCookie = ReadCookie(\"HBCountVisitTotalCookie\");"); //访客浏览次数  
  51.   html.Append(" var HBClientIDCookie = ReadCookie(\"HBClientIDCookie\");"); //访客上一个点击页面ID  
  52.   html.Append(" var HBLoginNameCookie = ReadCookie(\"HBLoginNameCookie\");"); //记录登录的用户名  
  53.   html.Append(" if (HBCountShowCookie == \"\") {");  
  54.   html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); //有效期为一年  
  55.   html.Append(" WriteCookie('HBCountShowCookie', \"" + guid + "\", sparetime);");  
  56.   html.Append(" HBCountShowCookie = ReadCookie(\"HBCountShowCookie\");");  
  57.   html.Append(" firstshow = 1;");  
  58.   html.Append(" }");  
  59.   html.Append(" else {");  
  60.   html.Append(" firstshow = 0;");  
  61.   html.Append(" }");  
  62.   html.Append(" if (HBCountVisitTotalCookie == \"\") {"); //游客统计  
  63.   html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;");  
  64.   html.Append(" WriteCookie('HBCountVisitTotalCookie', 1, sparetime);");  
  65.   html.Append(" visittotal = 1;");  
  66.   html.Append(" }");  
  67.   html.Append(" else {");  
  68.   html.Append(" if (firstshow == 1) {");  
  69.   html.Append(" visittotal = parseInt(HBCountVisitTotalCookie) + 1;");  
  70.   html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;");  
  71.   html.Append(" WriteCookie('HBCountVisitTotalCookie', visittotal, sparetime);");  
  72.   html.Append(" }");  
  73.   html.Append(" else {");  
  74.   html.Append(" visittotal = parseInt(HBCountVisitTotalCookie);");  
  75.   html.Append(" }");  
  76.   html.Append(" }");  
  77.   html.Append(" try {");  
  78.   html.Append(" if (HBStat != undefined || HBStat.isLogin != undefined || HBStat.loginName != undefined) {"); //此为登录用户统计  
  79.   html.Append(" if (HBStat.isLogin == 1 &&HBStat.loginName != \"\") {");  
  80.   html.Append(" sparetime = 1000 * 60 * 60 * 24 * 120;");  
  81.   html.Append(" WriteCookie('HBLoginNameCookie', HBStat.loginName, sparetime);");  
  82.   html.Append(" HBLoginNameCookie = ReadCookie(\"HBLoginNameCookie\");");  
  83.   html.Append(" }");  
  84.   html.Append(" isLogin=HBStat.isLogin;");  
  85.   html.Append(" }");  
  86.   html.Append(" else{isLogin=0;}");  
  87.   html.Append(" }");  
  88.   html.Append(" catch (e) { isLogin=0; }");  
  89.   html.Append(" loginName=HBLoginNameCookie;");  
  90.   html.Append("} else {"); //不支持cookie  
  91.   html.Append(" firstshow = 0;");  
  92.   html.Append(" visittotal = 1;");  
  93.  html.Append(" var HBCountShowCookie = \"\";");  
  94.   html.Append(" isLogin=0;");  
  95.   html.Append(" loginName=\"\";");  
  96.   html.Append("}");  
  97.   html.Append("var ly = escape(document.referrer);");  
  98.   html.Append("var currweb = escape(location.href);");  
  99.   html.Append("var d = new Date();");  
  100.   html.Append("var currdate=DateFormatString(d,'YY-MM-DD hh:mm:ss');");  
  101.   html.Append("var screenwidth=screen.width;");  
  102.   html.Append("var screenheight=screen.height;");  
  103.   html.Append("var screencolordepth=screen.colorDepth;");  
  104.   html.Append("document.write('<script src=\"" + websiteUrl + "CountGet/?SiteID=" + SiteID + "&assort=" + assort + "&isLogin='+isLogin+'&loginName='+loginName+'&FirstShow='+firstshow+'&VisitTotal='+visittotal+'&Ly='+ly+'&CurrWeb='+currweb+'&cookid='+HBCountShowCookie+'&screenwidth=' + screenwidth + '&screenheight=' + screenheight + '&screencolordepth=' + screencolordepth + '&currdate='+currdate+'&ranstr=' + Math.random() + '\"></script>');");  
  105.   #endregion 

  生成之后再去执行下一个Action,用来写入一些统计数据,如客户端IP,浏览器信息,访问页面来源,当前页、访问时间、离开时间等!根据上一个访问页面地址、当前地址及上一个访问页面ID来判断是否是刷新操作还是新的访问页面,如果是刷新操作则写入刷新时间,否则写入上一个页面的离开时间,由于js的跨域问题,

  1.   if (assort.Equals(0))  
  2.   {  
  3.   ly = Request.QueryString["Ly"]; //获取来源URL  
  4.   currWeb = Request.QueryString["CurrWeb"]; //获取当前URL  
  5.   }  
  6.   else  
  7.   {  
  8.   ly = Request.ServerVariables["Http_Referer"];  
  9.   currWeb = ly;  
  10.   }  
  11.   string firstShow = Request.QueryString["FirstShow"]; //是否第一次访问  
  12.   string visitTotal = Request.QueryString["VisitTotal"]; //获取浏览次数  
  13.   string ip = Helper.Utils.ClientIP(); //获取客户端IP地址  
  14.   string strAgent = Request.ServerVariables["HTTP_USER_AGENT"];  
  15.   bool isAlexa = false;  
  16.   if (strAgent.IndexOf("alexa") >-1) //判断是否安装alexa工具栏  
  17.   isAlexa = true;  
  18.   string browerName = Request.Browser.Browser; //浏览器名称  
  19.   string browerVersion = Request.Browser.Version; //浏览器版本  
  20.   string os = Helper.Utils.GetClientOS(); //客户端操作系统  
  21.   string langage = Helper.Utils.GetLangage(); //客户端语言  
  22.   string spider = Helper.Utils.GetSpiderBot(); //搜索引擎爬虫信息  
  23.   bool isSpider = false;  
  24.   if (!string.IsNullOrEmpty(spider))  
  25.   isSpider = true;  
  26.   HttpContext.Application.Lock();  
  27.   ClickDataAccess clientBasic = new ClickDataAccess();  
  28.  //刷新操作 需要根据上一个地址和当前地址 来判断  
  29.   if (HttpContext.Application["HBCurrWeb_" + SiteID] != null  
  30.   &&HttpContext.Application["HBCurrWeb_" + SiteID].ToString().Equals(currWeb)  
  31.   &&HttpContext.Application["HBLy_" + SiteID] != null  
  32.   &&HttpContext.Application["HBLy_" + SiteID].ToString().Equals(ly)  
  33.   &&HttpContext.Application["HBClientId_" + SiteID] != null)  
  34.   {  
  35.   clientBasic.UpdateForRefresh(HttpContext.Application["HBClientId_" + SiteID]);  
  36.   //Helper.Utils.CreateFile("~/Log/", DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_" + SiteID + "刷新.txt", writetext.ToString());  
  37.   }  
  38.   else  
  39.   {  
  40.   if (HttpContext.Application["HBClientId_" + SiteID] != null) //如果存在上一个页面的ID值则写入离开时间  
  41.   clientBasic.UpdateForLeaveTime(HttpContext.Application["HBClientId_" + SiteID]);  
  42.   ClickAndVisitorsDataAccess clientData = new ClickAndVisitorsDataAccess();  
  43.   string[] outParam = clientData.Save(client);  
  44.   if (!string.IsNullOrEmpty(outParam[0]))  
  45.   HttpContext.Application["HBPublicId_" + SiteID] = outParam[0];  
  46.   if (!string.IsNullOrEmpty(outParam[1]))  
  47.   {  
  48.   HttpContext.Application["HBClientId_" + SiteID] = outParam[1];  
  49.   javaScript = "WriteCookie('HBClientIDCookie'," + int.Parse(outParam[1]) + ");";  
  50.   }  
  51.   HttpContext.Application["HBCurrWeb_" + SiteID] = currWeb;  
  52.   HttpContext.Application["HBLy_" + SiteID] = ly;  
  53.   }  
  54.   HttpContext.Application.UnLock();  
  55.   #endregion 

  这里使用Application来存储对象,当然也可以使用缓存类来做.

  暂时没有解决离开当前网站时的离开时间,当然如果访问统计系统的域名和被统计的网站域名是同一个顶级域名,则可以做到这点!

  访问统计系统的后台使用存储过程来统计分析数据,暂时只写了一部分功能,访问统计系统也暂时只是一个demo,仅供大家学习参考!

  项目源代码:http://download.csdn.net/detail/jobily/3671987

http://down.51cto.com/data/263059

  还是提供csdn就 51cto的下载地址,其他地址以后再附上,网站数据访问的框架及一些存储过程经过很长的时间积累而成,花了我很多的心思,希望大家不要吝啬自己的那点分,绝对值得你学习,此项目很多类及插入更新的存储过程都是另一个项目自动生成滴,关于自动生成、灵活性及多数据库支持将会在以后文章中公开,敬请期待。

原文:http://www.cnblogs.com/jobily/archive/2011/10/10/2206602.html

【编辑推荐】

  1. 详解ASP.NET MVC 3新的Layout布局系统
  2. ASP.NET MVC中很酷的jQuery验证插件
  3. ASP.NET MVC 3新特性全解析
  4. ASP.NET MVC 3让你疯狂的五大理由
  5. 详解ASP.NET MVC 3 beta新特性
责任编辑:彭凡 来源: 博客园
相关推荐

2012-08-27 10:11:43

ASP.NET

2010-01-26 13:15:42

ASP.NET MVC

2009-04-23 09:42:39

FubuMVCASP.NET MVCMVC

2009-07-20 15:44:32

ASP.NET MVC

2009-09-11 09:18:17

ASP.NET MVC

2009-07-30 15:50:49

ASP.NET中网站访

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2011-04-18 09:35:59

ASP.NET MVC

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-07-28 14:47:18

ASP.NET MVC

2009-07-24 11:55:29

ASP.NET MVC

2011-01-15 23:07:59

2010-10-14 09:05:36

ASP.NET MVC

2010-10-12 09:52:02

ASP.NET MVC

2009-09-10 09:50:47

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC
点赞
收藏

51CTO技术栈公众号