本篇文章為大家展示了如何用ASP.NET MVC 3實(shí)現(xiàn)一個(gè)訪問(wèn)統(tǒng)計(jì)系統(tǒng),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來(lái)公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過(guò)去的10多年時(shí)間我們累計(jì)服務(wù)了上千家以及全國(guó)政企客戶,如封陽(yáng)臺(tái)等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過(guò)硬的技術(shù)實(shí)力獲得客戶的一致稱揚(yáng)。
運(yùn)營(yíng)網(wǎng)站,我們經(jīng)常需要分析用戶的行為、用戶的習(xí)慣,用戶看重網(wǎng)站的哪一部分,哪一部分是對(duì)用戶有用的之類的信息,這些信息從哪里來(lái),這時(shí)我們就需要用到訪問(wèn)統(tǒng)計(jì)系統(tǒng)了。 網(wǎng)上已經(jīng)有很多的統(tǒng)計(jì)系統(tǒng),如站長(zhǎng)統(tǒng)計(jì)、百度統(tǒng)計(jì)、谷歌分析之類的,別人的東西始終是別人的,為什么我們不自己實(shí)現(xiàn)統(tǒng)計(jì)的功能呢,而且自己寫的可以實(shí)現(xiàn)一些特殊的功能,如登錄,下單行為,能夠更好的融合自己的系統(tǒng)!
下面我們就用ASP.NET MVC 3來(lái)實(shí)現(xiàn)一個(gè)訪問(wèn)統(tǒng)計(jì)系統(tǒng)!首先,使用程序生成一段js代碼,包括讀寫Cookie,及寫入一個(gè)唯一值到Cookie中,用來(lái)判斷獨(dú)立訪客者,再寫入訪問(wèn)次數(shù)及上一個(gè)訪問(wèn)頁(yè)面ID值;然后就是獲取客戶端時(shí)間,分辨率之類的。
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\");"); //訪客上一個(gè)點(diǎn)擊頁(yè)面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)計(jì) 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)計(jì) 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í)行下一個(gè)Action,用來(lái)寫入一些統(tǒng)計(jì)數(shù)據(jù),如客戶端IP,瀏覽器信息,訪問(wèn)頁(yè)面來(lái)源,當(dāng)前頁(yè)、訪問(wèn)時(shí)間、離開(kāi)時(shí)間等!根據(jù)上一個(gè)訪問(wèn)頁(yè)面地址、當(dāng)前地址及上一個(gè)訪問(wèn)頁(yè)面ID來(lái)判斷是否是刷新操作還是新的訪問(wèn)頁(yè)面,如果是刷新操作則寫入刷新時(shí)間,否則寫入上一個(gè)頁(yè)面的離開(kāi)時(shí)間,由于js的跨域問(wèn)題,
if (assort.Equals(0)) { ly = Request.QueryString["Ly"]; //獲取來(lái)源URL currWeb = Request.QueryString["CurrWeb"]; //獲取當(dāng)前URL } else { ly = Request.ServerVariables["Http_Referer"]; currWeb = ly; } string firstShow = Request.QueryString["FirstShow"]; //是否第一次訪問(wèn) 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(); //客戶端語(yǔ)言 string spider = Helper.Utils.GetSpiderBot(); //搜索引擎爬蟲(chóng)信息 bool isSpider = false; if (!string.IsNullOrEmpty(spider)) isSpider = true; HttpContext.Application.Lock(); ClickDataAccess clientBasic = new ClickDataAccess(); //刷新操作 需要根據(jù)上一個(gè)地址和當(dāng)前地址 來(lái)判斷 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) //如果存在上一個(gè)頁(yè)面的ID值則寫入離開(kāi)時(shí)間 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來(lái)存儲(chǔ)對(duì)象,當(dāng)然也可以使用緩存類來(lái)做.
暫時(shí)沒(méi)有解決離開(kāi)當(dāng)前網(wǎng)站時(shí)的離開(kāi)時(shí)間,當(dāng)然如果訪問(wèn)統(tǒng)計(jì)系統(tǒng)的域名和被統(tǒng)計(jì)的網(wǎng)站域名是同一個(gè)頂級(jí)域名,則可以做到這點(diǎn)!
上述內(nèi)容就是如何用ASP.NET MVC 3實(shí)現(xiàn)一個(gè)訪問(wèn)統(tǒng)計(jì)系統(tǒng),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:如何用ASP.NETMVC3實(shí)現(xiàn)一個(gè)訪問(wèn)統(tǒng)計(jì)系統(tǒng)
文章URL:http://muchs.cn/article8/piopop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、全網(wǎng)營(yíng)銷推廣、響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)