Using JS cross-domain to make a simple page access statistics system

Source: Internet
Author: User

In fact, in most of the Internet web products, we generally use Baidu statistics or Google statistical analysis system, through the introduction of specific JS script in the program, and then can be in these statistical system to see their site page detailed access to the situation. But sometimes, because of 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 also relatively simple.

 several basic statistical requirements:1. Count the number of user visits per page of the Web2. Statistics of user access and IP address information3. Jump situation between pages4. Access to peak time periods 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 main requirements, can see the needs of the 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 collection: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 within 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 the page related information, set the tag src address for the remote Statisticsserver address, add it to the current document, The tag will voluntarily send the request to the specified server, and the server can parse the parameters in the request and request information and store it in the library.   Demo Sample code:  
(function() {            vartitle = document.getElementsByTagName ("title") [0].innerhtml,//Page Titleurl = window.location.href,//current request path namesite = Window.location.host,//Site Host                //here is a refere reference from the header to get a detailed 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 demo sample code to get the Referer parameter from the header. Server side:This demo sample server framework uses SPRINGMVC, but gets the same number of references. get the number of references:
     /*** Record website 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, no matter what the JS script can be run, of course, if necessary, this is completely no problem, in the cross-domain other requirements, fully able to respond to the Serverjs run script. get access to IP:
      /*** Get clientip 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, you just need to add the JS collection script to the page you want to monitor.

Using JS cross-domain to make a simple page access statistics system

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.