ASP.NET MVC 3實現(xiàn)訪問統(tǒng)計系統(tǒng)
運營網(wǎng)站,我們經(jīng)常需要分析用戶的行為、用戶的習(xí)慣,用戶看重網(wǎng)站的哪一部分,哪一部分是對用戶有用的之類的信息,這些信息從哪里來,這時我們就需要用到訪問統(tǒng)計系統(tǒng)了。 網(wǎng)上已經(jīng)有很多的統(tǒng)計系統(tǒng),如站長統(tǒng)計、百度統(tǒng)計、谷歌分析之類的,別人的東西始終是別人的,為什么我們不自己實現(xiàn)統(tǒng)計的功能呢,而且自己寫的可以實現(xiàn)一些特殊的功能,如登錄,下單行為,能夠更好的融合自己的系統(tǒng)!
下面我們就用ASP.NET MVC 3來實現(xiàn)一個訪問統(tǒng)計系統(tǒng)!首先,使用程序生成一段js代碼,包括讀寫Cookie,及寫入一個唯一值到Cookie中,用來判斷獨立訪客者,再寫入訪問次數(shù)及上一個訪問頁面ID值;然后就是獲取客戶端時間,分辨率之類的。
string guid = Guid.NewGuid().ToString(); #region 生成js html.Append("function WriteCookie(cookieName, cookieValue, expiry) {"); //js寫cookie html.Append(" var expDate = new Date();"); html.Append(" if (expiry) {"); html.Append(" expDate.setTime(expDate.getTime() + expiry);"); html.Append(" document.cookie = cookieName + \"=\" + escape(cookieValue) + \"; path=/;expires=\" + expDate.toGMTString();"); html.Append(" }"); html.Append(" else {"); html.Append(" document.cookie = cookieName + \"=\" + escape(cookieValue) + \"; path=/;\""); html.Append(" }"); html.Append("}"); html.Append("function ReadCookie(name) {"); //js讀取cookie html.Append(" var cookieValue = \"\";"); html.Append(" var search = name + \"=\";"); html.Append(" if (document.cookie.length >0) {"); html.Append(" offset = document.cookie.indexOf(search);"); html.Append(" if (offset != -1) {"); html.Append(" offset += search.length;"); html.Append(" end = document.cookie.indexOf(\";\", offset);"); html.Append(" if (end == -1)"); html.Append(" end = document.cookie.length;"); html.Append(" cookieValue = unescape(document.cookie.substring(offset, end))"); html.Append(" }"); html.Append(" }"); html.Append(" return cookieValue;"); html.Append("}"); html.Append("DateFormatString = function(datetime, format) {"); html.Append(" var year = datetime.getFullYear();"); html.Append(" var month = datetime.getMonth()+1;"); html.Append(" var date = datetime.getDate();"); html.Append(" var hour = datetime.getHours();"); html.Append(" var minutes = datetime.getMinutes();"); html.Append(" var second = datetime.getSeconds();"); 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);"); html.Append(" return format;"); html.AppendLine("}"); html.Append("var cookieEnabled = (navigator.cookieEnabled) ? true : false;"); //判斷瀏覽器是否支持cookie html.Append("if (typeof navigator.cookieEnabled == \"undefined\" &&!cookieEnabled) {"); html.Append(" document.cookie = \"testcookie\";"); html.Append(" cookieEnabled = (document.cookie == \"testcookie\") ? true : false;"); html.Append(" document.cookie = \"\";"); html.Append("}"); html.Append("var firstshow;"); html.Append("var visittotal;"); html.Append("var isLogin;"); html.Append("var loginName;"); html.Append("if (cookieEnabled == true) {"); html.Append(" var HBCountShowCookie = ReadCookie(\"HBCountShowCookie\");"); //訪客瀏覽器唯一ID html.Append(" var HBCountVisitTotalCookie = ReadCookie(\"HBCountVisitTotalCookie\");"); //訪客瀏覽次數(shù) html.Append(" var HBClientIDCookie = ReadCookie(\"HBClientIDCookie\");"); //訪客上一個點擊頁面ID html.Append(" var HBLoginNameCookie = ReadCookie(\"HBLoginNameCookie\");"); //記錄登錄的用戶名 html.Append(" if (HBCountShowCookie == \"\") {"); html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); //有效期為一年 html.Append(" WriteCookie('HBCountShowCookie', \"" + guid + "\", sparetime);"); html.Append(" HBCountShowCookie = ReadCookie(\"HBCountShowCookie\");"); html.Append(" firstshow = 1;"); html.Append(" }"); html.Append(" else {"); html.Append(" firstshow = 0;"); html.Append(" }"); html.Append(" if (HBCountVisitTotalCookie == \"\") {"); //游客統(tǒng)計 html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); html.Append(" WriteCookie('HBCountVisitTotalCookie', 1, sparetime);"); html.Append(" visittotal = 1;"); html.Append(" }"); html.Append(" else {"); html.Append(" if (firstshow == 1) {"); html.Append(" visittotal = parseInt(HBCountVisitTotalCookie) + 1;"); html.Append(" sparetime = 1000 * 60 * 60 * 24 * 3650;"); html.Append(" WriteCookie('HBCountVisitTotalCookie', visittotal, sparetime);"); html.Append(" }"); html.Append(" else {"); html.Append(" visittotal = parseInt(HBCountVisitTotalCookie);"); html.Append(" }"); html.Append(" }"); html.Append(" try {"); html.Append(" if (HBStat != undefined || HBStat.isLogin != undefined || HBStat.loginName != undefined) {"); //此為登錄用戶統(tǒng)計 html.Append(" if (HBStat.isLogin == 1 &&HBStat.loginName != \"\") {"); html.Append(" sparetime = 1000 * 60 * 60 * 24 * 120;"); html.Append(" WriteCookie('HBLoginNameCookie', HBStat.loginName, sparetime);"); html.Append(" HBLoginNameCookie = ReadCookie(\"HBLoginNameCookie\");"); html.Append(" }"); html.Append(" isLogin=HBStat.isLogin;"); html.Append(" }"); html.Append(" else{isLogin=0;}"); html.Append(" }"); html.Append(" catch (e) { isLogin=0; }"); html.Append(" loginName=HBLoginNameCookie;"); html.Append("} else {"); //不支持cookie html.Append(" firstshow = 0;"); html.Append(" visittotal = 1;"); html.Append(" var HBCountShowCookie = \"\";"); html.Append(" isLogin=0;"); html.Append(" loginName=\"\";"); html.Append("}"); html.Append("var ly = escape(document.referrer);"); html.Append("var currweb = escape(location.href);"); html.Append("var d = new Date();"); html.Append("var currdate=DateFormatString(d,'YY-MM-DD hh:mm:ss');"); html.Append("var screenwidth=screen.width;"); html.Append("var screenheight=screen.height;"); html.Append("var screencolordepth=screen.colorDepth;"); 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>');"); #endregion
生成之后再去執(zhí)行下一個Action,用來寫入一些統(tǒng)計數(shù)據(jù),如客戶端IP,瀏覽器信息,訪問頁面來源,當(dāng)前頁、訪問時間、離開時間等!根據(jù)上一個訪問頁面地址、當(dāng)前地址及上一個訪問頁面ID來判斷是否是刷新操作還是新的訪問頁面,如果是刷新操作則寫入刷新時間,否則寫入上一個頁面的離開時間,由于js的跨域問題,
if (assort.Equals(0)) { ly = Request.QueryString["Ly"]; //獲取來源URL currWeb = Request.QueryString["CurrWeb"]; //獲取當(dāng)前URL } else { ly = Request.ServerVariables["Http_Referer"]; currWeb = ly; } string firstShow = Request.QueryString["FirstShow"]; //是否第一次訪問 string visitTotal = Request.QueryString["VisitTotal"]; //獲取瀏覽次數(shù) string ip = Helper.Utils.ClientIP(); //獲取客戶端IP地址 string strAgent = Request.ServerVariables["HTTP_USER_AGENT"]; bool isAlexa = false; if (strAgent.IndexOf("alexa") >-1) //判斷是否安裝alexa工具欄 isAlexa = true; string browerName = Request.Browser.Browser; //瀏覽器名稱 string browerVersion = Request.Browser.Version; //瀏覽器版本 string os = Helper.Utils.GetClientOS(); //客戶端操作系統(tǒng) string langage = Helper.Utils.GetLangage(); //客戶端語言 string spider = Helper.Utils.GetSpiderBot(); //搜索引擎爬蟲信息 bool isSpider = false; if (!string.IsNullOrEmpty(spider)) isSpider = true; HttpContext.Application.Lock(); ClickDataAccess clientBasic = new ClickDataAccess(); //刷新操作 需要根據(jù)上一個地址和當(dāng)前地址 來判斷 if (HttpContext.Application["HBCurrWeb_" + SiteID] != null &&HttpContext.Application["HBCurrWeb_" + SiteID].ToString().Equals(currWeb) &&HttpContext.Application["HBLy_" + SiteID] != null &&HttpContext.Application["HBLy_" + SiteID].ToString().Equals(ly) &&HttpContext.Application["HBClientId_" + SiteID] != null) { clientBasic.UpdateForRefresh(HttpContext.Application["HBClientId_" + SiteID]); //Helper.Utils.CreateFile("~/Log/", DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_" + SiteID + "刷新.txt", writetext.ToString()); } else { if (HttpContext.Application["HBClientId_" + SiteID] != null) //如果存在上一個頁面的ID值則寫入離開時間 clientBasic.UpdateForLeaveTime(HttpContext.Application["HBClientId_" + SiteID]); ClickAndVisitorsDataAccess clientData = new ClickAndVisitorsDataAccess(); string[] outParam = clientData.Save(client); if (!string.IsNullOrEmpty(outParam[0])) HttpContext.Application["HBPublicId_" + SiteID] = outParam[0]; if (!string.IsNullOrEmpty(outParam[1])) { HttpContext.Application["HBClientId_" + SiteID] = outParam[1]; javaScript = "WriteCookie('HBClientIDCookie'," + int.Parse(outParam[1]) + ");"; } HttpContext.Application["HBCurrWeb_" + SiteID] = currWeb; HttpContext.Application["HBLy_" + SiteID] = ly; } HttpContext.Application.UnLock(); #endregion
這里使用Application來存儲對象,當(dāng)然也可以使用緩存類來做.
暫時沒有解決離開當(dāng)前網(wǎng)站時的離開時間,當(dāng)然如果訪問統(tǒng)計系統(tǒng)的域名和被統(tǒng)計的網(wǎng)站域名是同一個頂級域名,則可以做到這點!
訪問統(tǒng)計系統(tǒng)的后臺使用存儲過程來統(tǒng)計分析數(shù)據(jù),暫時只寫了一部分功能,僅供大家學(xué)習(xí)參考!
- asp.net 圖片驗證碼的HtmlHelper
- C# MVC模式中應(yīng)該怎樣區(qū)分應(yīng)用程序邏輯(Controller層)和業(yè)務(wù)邏輯(Model層)?
- Asp.net?mvc實現(xiàn)上傳頭像加剪裁功能
- 初識ASP.NET Mvc5+EF7的奇妙之旅
- ASP.NET MVC中圖表控件的使用方法
- windows2003 IIS6 部署MVC3和MVC4程序的方法
- ASP.NET Mvc開發(fā)之EF延遲加載
- ASP.NET Mvc開發(fā)之查詢數(shù)據(jù)
- ASP.NET Mvc開發(fā)之刪除修改數(shù)據(jù)
- ASP.NET MVC中HtmlHelper控件7個大類中各個控件使用詳解
相關(guān)文章
.NET 6開發(fā)TodoList應(yīng)用之實現(xiàn)查詢排序
這篇文章主要介紹了如何通過.NET 6實現(xiàn)查詢排序功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí).NET 6有一定的幫助,感興趣的同學(xué)可以了解一下2022-01-01利用ASP.NET MVC+EasyUI+SqlServer搭建企業(yè)開發(fā)框架
本文主要介紹使用asp.net mvc4、sqlserver、jquery2.0和easyui1.4.5搭建企業(yè)級開發(fā)框架的過程,希望能夠幫到大家。2016-04-04如何使用ASP.NET MiniAPI 調(diào)試未匹配請求路徑
ASP.NET MiniAPI是一個輕量級的Web API框架,它可以讓我們快速地構(gòu)建和部署RESTful服務(wù),本文給大家介紹使用ASP.NET MiniAPI 調(diào)試未匹配請求路徑的方法,感興趣的朋友一起看看吧2024-01-01MVC使用Controller代替Filter完成登錄驗證(Session校驗)學(xué)習(xí)筆記5
這篇文章主要介紹了MVC使用Controller代替Filter完成登錄驗證即Session校驗,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09解決.net framework 4.0環(huán)境下遇到版本不同編譯不通過的方法詳解
本篇文章是對.net framework 4.0環(huán)境下遇到版本不同編譯不通過的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05URL重寫及干掉ASP.NET試圖狀態(tài)的實現(xiàn)方法
URL重寫已經(jīng)很普遍了,但基本上大部分的URL重寫都不支持頁面的相對路徑,所有如果想在已經(jīng)開發(fā)好的項目中添加還是有壓力的,第二就是例如微軟的那個URL重寫是根據(jù)正則表達(dá)式來處理的,那樣是很好,但也有不足之處,就是不方便定位到某個頁面只能有哪些參數(shù)2011-11-11