欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-13 14:04:28
运营网站,我们经常需要分析用户的行为、用户的习惯,用户看重网站的哪一部分,哪一部分是对用户有用的之类的信息,这些信息从哪里来,这时我们就需要用到访问统计系统了。 网上已经有...

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

下面我们就用asp.net mvc 3来实现一个访问统计系统!首先,使用程序生成一段js代码,包括读写cookie,及写入一个唯一值到cookie中,用来判断独立访客者,再写入访问次数及上一个访问页面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\");"); //访客浏览次数 
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 == \"\") {"); //游客统计 
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) {"); //此为登录用户统计 
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 

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

if (assort.equals(0)) 
{ 
ly = request.querystring["ly"]; //获取来源url 
currweb = request.querystring["currweb"]; //获取当前url 
} 
else 
{ 
ly = request.servervariables["http_referer"]; 
currweb = ly; 
} 
string firstshow = request.querystring["firstshow"]; //是否第一次访问 
string visittotal = request.querystring["visittotal"]; //获取浏览次数 
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(); //客户端操作系统 
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(); 
 //刷新操作 需要根据上一个地址和当前地址 来判断 
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来存储对象,当然也可以使用缓存类来做.

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

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

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