ASP. net mvc 3 implements an access statistics system and mvc access statistics
When operating a website, we often need to analyze user behavior and user habits. What part of the website is important to users, and what information is useful to users? Where can this information come from, then we need to use the access statistics system. There are already many statistical systems on the Internet, such as webmaster statistics, Baidu statistics, and Google analysis. Other people's things are always others'. Why don't we implement the statistical function on our own, in addition, you can write your own code to implement some special functions, such as logon and order behavior, to better integrate your system!
Next we will use ASP. net mvc 3 to implement an access statistics system! First, use the program to generate a piece of js Code, including reading and writing cookies, and write a unique value into the Cookie to determine the number of independent visitors, and then write the number of visits and the ID value of the previous access page; then you can obtain the client time, resolution, and so on.
String guid = Guid. newGuid (). toString (); # region generates js html. append ("function WriteCookie (cookieName, cookieValue, expiry) {"); // js writes 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 (); "); ht Ml. append ("}"); html. append ("else {"); html. append ("document. cookie = cookieName + \ "= \" + escape (cookieValue) + \ "; path =/; \" "); html. append ("}"); html. append ("}"); html. append ("function ReadCookie (name) {"); // js reads 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 = f Unction (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 (/M M/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; "); // determines whether the browser supports 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 \ ");"); // unique visitor browser ID html. append ("var HBCountVisitTotalCookie = ReadCookie (\" HBCountVisi TTotalCookie \ ");"); // visitor views html. append ("var HBClientIDCookie = ReadCookie (\" HBClientIDCookie \ ");"); // visitor's previous click Page ID html. append ("var HBLoginNameCookie = ReadCookie (\" HBLoginNameCookie \ ");"); // record the username html for logon. append ("if (HBCountShowCookie =\" \") {"); html. append ("sparetime = 1000*60*60*24*3650;"); // valid for one year html. append ("WriteCookie ('hbcountshowcooker', \" "+ 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 = \" \ ") {"); // Visitor Statistics 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 (HBCountVisi TTotalCookie); "); html. append ("}"); html. append ("}"); html. append ("try {"); html. append ("if (HBStat! = Undefined | HBStat. isLogin! = Undefined | HBStat. loginName! = Undefined) {"); // This is the statistics of logon users in 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. A Ppend ("loginName = HBLoginNameCookie;"); html. append ("} else {"); // cookie html is not supported. 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 Da Te (); "); 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
Then, execute the next Action to write statistics, such as the Client IP address, browser information, access page source, current page, access time, and departure time! Determines whether a refresh operation is performed or a new access page based on the previous access page address, current address, and the ID of the previous access page. If a refresh operation is performed, the refresh time is written, otherwise, the departure time of the previous page is written. Due to cross-origin issues of js,
If (assort. equals (0) {ly = Request. queryString ["Ly"]; // obtain the source URL currWeb = Request. queryString ["CurrWeb"]; // obtain the current URL} else {ly = Request. serverVariables ["Http_Referer"]; currWeb = ly;} string firstShow = Request. queryString ["FirstShow"]; // whether to access string visitTotal = Request for the first time. queryString ["VisitTotal"]; // obtain the number of browsing times string ip = Helper. utils. clientIP (); // obtain the Client IP address string strAgent = Request. serverVari Ables ["HTTP_USER_AGENT"]; bool isAlexa = false; if (strAgent. indexOf ("alexa")>-1) // determine whether to install alexa toolbar isAlexa = true; string browerName = Request. browser. browser; // Browser name string browerVersion = Request. browser. version; // browser Version string OS = Helper. utils. getClientOS (); // client operating system string langage = Helper. utils. getLangage (); // client language string spider = Helper. utils. getSpiderBot (); // search engine crawler information bool isSpider = f Alse; if (! String. isNullOrEmpty (spider) isSpider = true; HttpContext. application. lock (); ClickDataAccess clientBasic = new ClickDataAccess (); // The refresh operation must judge 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 + "Refresh .txt", writetext. toString ();} else {if (HttpContext. application ["HBClientId _" + SiteID]! = Null) // If the ID value of the previous page exists, the write departure time is 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 ('hbclientidcookier'," + int. parse (outParam [1]) + ");";} HttpContext. application ["HBCurrWeb _" + SiteID] = currWeb; HttpContext. application ["HBLy _" + SiteID] = ly;} HttpContext. application. unLock (); # endregion
Here, Application is used to store objects. Of course, cache classes can also be used.
The departure time for leaving the current website is not fixed for the moment. Of course, if the domain name accessing the statistics system and the website domain name being counted are the same top-level domain name, this can be done!
The background of the access statistics system uses stored procedures for statistical analysis. Currently, only some functions are provided for your reference!