asp.net MVC 3 Implementation Access Statistics System _ practical skills

Source: Internet
Author: User
Tags httpcontext servervariables unique id

Operating the site, we often need to analyze the user's behavior, user habits, users value the site of which part, which part is useful to users, such as information, where the information comes from, then we need to access the statistical system. There are already a lot of statistical systems on the web, such as webmaster statistics, Baidu Statistics, Google Analysis, and so on, other people's things are always others, why we do not realize the function of statistics, and their own write can achieve some special functions, such as login, the next single behavior, to better integrate their own system!

Here we will use ASP.net MVC 3来 to implement an access statistics system! First, use the program to generate a section of JS code, including read and write cookies, and write a unique value to the cookie, used to judge the independent visitor, and then write the number of visits and the previous access page ID value, and then get the client time, resolution and so on.

String guid = Guid.NewGuid (). 
ToString (); #region Generate 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 ();"); Html. 
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 = 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; "); 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\); "); Visitor browser Unique ID HTML. Append ("var hbcountvisittotalcookie = Readcookie (\" hbcountvisittotalcookie\); "); Visitor browsing times HTML. Append ("var hbclientidcookie = Readcookie (\" hbclientidcookie\); "); Visitors last click on page ID HTML. Append ("var hbloginnamecookie = Readcookie (\" hbloginnamecookie\); "); Log the user name HTML for the login. 
Append ("if" (Hbcountshowcookie = =) {"); Html. Append ("sparetime = 1000 * 60 * 60 * 24 * 3650;"); Valid for one year of 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 = =) {"); 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 (Hbcountvisittotalcookie);"); Html. 
Append ("}"); Html. 
Append ("}"); Html. 
Append ("try {"); Html. Append ("If" Hbstat!= undefined | | Hbstat.islogin!= Undefined | | Hbstat.loginname!= undefined) {"); This is a logon user statistic
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 is not supported. 
Append ("firstshow = 0;"); Html. 
 Append ("visittotal = 1;"); Html. 
Append ("var hbcountshowcookie = \"; "); Html. 
Append ("islogin=0;"); Html. 
Append ("loginname=\" or ";"); 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= ' + ' &screenwidth= ' + screenwidth + ' &screenheight= ' + screenheight + ' &screencolordepth= ' + screencolordepth + ' 
&currdate= ' +currdate+ ' &ranstr= ' + math.random () + ' ></script> ');  #endregion

After the build, execute the next action to write some statistics, such as client IP, browser information, access to the page source, current page, access time, departure time, and so on! Based on the previous access page address, current address and previous access page ID to determine whether the refresh operation or the new access page, if the refresh operation is the write refresh time, otherwise write the last page of the departure time, due to the cross-domain problem of JS,

if (assort. 
Equals (0)) {ly = request.querystring["ly"];//get source URL Currweb = request.querystring["Currweb"];//get current URL} else { 
ly = request.servervariables["Http_referer"]; 
Currweb = ly; } string firstshow = request.querystring["Firstshow"]; Whether to access string visittotal = request.querystring["Visittotal" for the first time; Get browse number String IP = Helper.Utils.ClientIP (); 
Gets the client IP address string stragent = request.servervariables["Http_user_agent"]; 
BOOL Isalexa = false; 
if (Stragent.indexof ("Alexa") >-1)//Determine if the Alexa toolbar is installed 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 = FALSE; if (!string. 
IsNullOrEmpty (spider)) Isspider = true; 
HttpContext.Application.Lock (); 
 Clickdataaccess clientbasic = new clickdataaccess (); The refresh operation needs to be judged based on the previous address and current addressBreak 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 is present, write away time Clientbasic.updateforleav 
ETime (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


Here you use application to store objects, and of course you can use caching classes to do so.

Temporarily do not solve the departure time away from the current site, of course, if access to the statistical system of the domain name and the site is counted as the same top-level domain name, you can do this!

Access to the background of the statistical system using stored procedures to statistical analysis of data, temporarily only wrote a part of the function, only for everyone to learn the reference!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.