In fact, in most of the Internet web products, we usually use Baidu statistics or Google statistical analysis system, through the introduction of specific JS script in the program, and then you can see in these statistical systems of their own site page specific access. But sometimes, due to some special circumstances, we need to design our own statistical system. Because of the business needs of the previous period of time, I also tried the next, this article provides a basic idea, statistical system is relatively simple.
several basic statistical requirements:1. Count the number of Web pages per page user visits2. Statistics of user visitor's and IP address information3. Jump situation between pages4. Access Peak time period Server structure:database table Design:The above is just my simple list of needs, here is a simple table, and this table can meet the demand is not only the above four basic needs, you can see the demand situation to do the corresponding business processing. Tb_visit_count_log
| Id |
IP (varchar) IP address |
Title (varchar) |
Cur_page (varchar) current page |
From_page (varchar) source page |
Time (DateTime) datetime |
App (varchar) app |
Data acquisition:we submit a JS script that captures the data we need on the current page, and then stores it through cross-domain requests to our statistics server for subsequent statistical analysis of the operation of the business. JS cross-domain: we all know that when developing Web applications, we often use <script src= "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></ Script> to request script resources from other servers, SRC is the abbreviation for source, points to the location of the external resource, and the content that is pointed to is embedded in the document where the current label is located. So using this, we can dynamically create <script> tags through JS, and crawl page related information, set the label SRC address as a remote statistics server address, add it to the current document, the label will automatically send the request to the specified server, The server can then parse the parameters and request information in the request and store it in the library. Example code:
(function() { vartitle = document.getElementsByTagName ("title") [0].innerhtml,//Page Titleurl = window.location.href,//current request path namesite = Window.location.host,//Site Host //Get the refere parameter from the header here, get a look at the current web programref = ' <%=request.getheader ("Referer")%> ', param= "? title=" +title+ "&url=" +url+ "&ref=" +ref,//Reference page AddressScript = document.createelement ("Script"); SCRIPT.SRC= "Http://example.com/analysis" +param; document.getElementsByTagName ("Head") [0].appendchild (script); })();Note: A JSP tag is used in the example code to get the Referer parameter from the header. server-side:This example server framework uses SPRINGMVC, but the get parameters are the same. Get Parameters:
/*** Record Site statistics *@paramRequest*/@RequestMapping ("/analysis") @ResponseBody Public voidVisitorlogger (HttpServletRequest request) {Try{Websitevisitcount Visitor=NewWebsitevisitcount (); Visitor.seturl (Request.getparameter ("url")); String title= Request.getparameter ("title"); if(Title! =NULL) {Visitor.settitle (NewString (Title.getbytes ("Iso-8859-1" ), "Utf-8")); } //User IPVisitor.setfromurl (Request.getparameter ("ref")); Visitor.setuserip (Getremortip (request)); Visitor.setapp (Request.getparameter ("Site")); //Storing DataWebsitevisitcountservice.addvisitor (visitor); } Catch(Exception e) {logger.error ("Websitevisitcountcontroller.visitorlogger ():" +Request.getrequesturi (), E.getmessage ()); } } The above code does not respond to the client any executable JS script, of course, if necessary, this is completely no problem, in the cross-domain other requirements, can fully respond to the server JS execution script. get access IP:
/*** Get client IP address *@paramRequest *@return */ PublicString Getremortip (httpservletrequest request) {if(Request.getheader ("x-forwarded-for") = =NULL ) { returnrequest.getremoteaddr (); } returnRequest.getheader ("X-forwarded-for" ); }The above is the core code part of the statistics, the data can be processed in the corresponding business, to obtain the data you want to know. When deploying, simply add the JS capture script to the page you want to monitor.
ziv
A programmer who focuses on Java technology, and also has a high interest in JS development. Love music, reading, FM and so on.
This article is copyright to the author and the blog Park, Welcome to reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to the original link.
If there is a problem, you can mail: [email protected]
Weibo: Ziv Mini-wei