日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用ASP.NETMVC3實(shí)現(xiàn)一個訪問統(tǒng)計(jì)系統(tǒng)

運(yùn)營網(wǎng)站,我們經(jīng)常需要分析用戶的行為、用戶的習(xí)慣,用戶看重網(wǎng)站的哪一部分,哪一部分是對用戶有用的之類的信息,這些信息從哪里來,這時我們就需要用到訪問統(tǒng)計(jì)系統(tǒng)了。 網(wǎng)上已經(jīng)有很多的統(tǒng)計(jì)系統(tǒng),如站長統(tǒng)計(jì)、百度統(tǒng)計(jì)、谷歌分析之類的,別人的東西始終是別人的,為什么我們不自己實(shí)現(xiàn)統(tǒng)計(jì)的功能呢,而且自己寫的可以實(shí)現(xiàn)一些特殊的功能,如登錄,下單行為,能夠更好的融合自己的系統(tǒng)!

  下面我們就用ASP.NET MVC 3來實(shí)現(xiàn)一個訪問統(tǒng)計(jì)系統(tǒng)!首先,使用程序生成一段js代碼,包括讀寫Cookie,及寫入一個唯一值到Cookie中,用來判斷獨(dú)立訪客者,再寫入訪問次數(shù)及上一個訪問頁面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\");"); //訪客瀏覽次數(shù)
  51.   html.Append(" var HBClientIDCookie = ReadCookie(\"HBClientIDCookie\");"); //訪客上一個點(diǎn)擊頁面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 == \"\") {"); //游客統(tǒng)計(jì)
  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) {"); //此為登錄用戶統(tǒng)計(jì)
  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('');");
  105.   #endregion

  生成之后再去執(zhí)行下一個Action,用來寫入一些統(tǒng)計(jì)數(shù)據(jù),如客戶端IP,瀏覽器信息,訪問頁面來源,當(dāng)前頁、訪問時間、離開時間等!根據(jù)上一個訪問頁面地址、當(dāng)前地址及上一個訪問頁面ID來判斷是否是刷新操作還是新的訪問頁面,如果是刷新操作則寫入刷新時間,否則寫入上一個頁面的離開時間,由于js的跨域問題,

 
 
 
 
  1.   if (assort.Equals(0))
  2.   {
  3.   ly = Request.QueryString["Ly"]; //獲取來源URL
  4.   currWeb = Request.QueryString["CurrWeb"]; //獲取當(dāng)前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"]; //獲取瀏覽次數(shù)
  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(); //客戶端操作系統(tǒng)
  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.  //刷新操作 需要根據(jù)上一個地址和當(dāng)前地址 來判斷
  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來存儲對象,當(dāng)然也可以使用緩存類來做.

  暫時沒有解決離開當(dāng)前網(wǎng)站時的離開時間,當(dāng)然如果訪問統(tǒng)計(jì)系統(tǒng)的域名和被統(tǒng)計(jì)的網(wǎng)站域名是同一個頂級域名,則可以做到這點(diǎn)!

  訪問統(tǒng)計(jì)系統(tǒng)的后臺使用存儲過程來統(tǒng)計(jì)分析數(shù)據(jù),暫時只寫了一部分功能,訪問統(tǒng)計(jì)系統(tǒng)也暫時只是一個demo,僅供大家學(xué)習(xí)參考!

  項(xiàng)目源代碼:http://download.csdn.net/detail/jobily/3671987

http://down./data/263059

  還是提供csdn就 51cto的下載地址,其他地址以后再附上,網(wǎng)站數(shù)據(jù)訪問的框架及一些存儲過程經(jīng)過很長的時間積累而成,花了我很多的心思,希望大家不要吝嗇自己的那點(diǎn)分,絕對值得你學(xué)習(xí),此項(xiàng)目很多類及插入更新的存儲過程都是另一個項(xiàng)目自動生成滴,關(guān)于自動生成、靈活性及多數(shù)據(jù)庫支持將會在以后文章中公開,敬請期待。

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


新聞名稱:用ASP.NETMVC3實(shí)現(xiàn)一個訪問統(tǒng)計(jì)系統(tǒng)
文章鏈接:http://www.dlmjj.cn/article/dpdjieg.html