Visit Classification Statistics (QQ, Weixin, Weibo, Web Page, Website APP, Others)

Keywords: Java Mobile Windows network

Just ready to hit the keyboard, suddenly remembered today has been Friday, a little excitement, a week and so happy end, can rest, and so on. I seem to be writing a Java blog, how did it become a diary, well, back to the point.

I don't know if you have ever met such a demand: statistics of article browsing and analysis of the details, namely QQ, Weixin, Weibo, Web pages, website APP, other visits ratio, our company met such demand some time ago, then I will explain its specific implementation.

Firstly, we need to count the visits to an article. The visits here are precise (the number of visits by the landing user is counted as one person, and the number of visitors is counted as one person according to the IP statistics). The following is the code implementation of obtaining ip:

public static String getIpAddr(Request request) {


	    /*  String ip  =  request.getHeader( " x-forwarded-for " );
           if (ip  ==   null   ||  ip.length()  ==   0   ||   " unknown " .equalsIgnoreCase(ip))  {
	          ip  =  request.getHeader( " Proxy-Client-IP " );
	      }
	       if (ip  ==   null   ||  ip.length()  ==   0   ||   " unknown " .equalsIgnoreCase(ip))  {
	          ip  =  request.getHeader( " WL-Proxy-Client-IP " );
	      }
	       if (ip  ==   null   ||  ip.length()  ==   0   ||   " unknown " .equalsIgnoreCase(ip))  {
	         ip  =  request.getRemoteAddr();
	     }
	       if(ip.contains(",")){
	    	 ip =  ip.substring(0,  ip.indexOf(","));
	       }
	      return  ip;  */
        String ipAddress = null;
        //ipAddress = this.getRequest().getRemoteAddr();
        ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.remoteAddress();
            if (ipAddress.equals("127.0.0.1")) {
                //Getting IP Configured Locally Based on Network Card
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ipAddress = inet.getHostAddress();
            }

        }
        //In the case of multiple proxies, the first IP is a real IP for the client, and multiple IP is divided according to','.
        if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

  

The second step is to insert the access source when saving each access record, such as qq or wechat, etc. The following is the specific code implementation to obtain its access source:

 /**
     * Get the type of browser, 0-page, 1;//Weibo 2;//qq 5;//Weixin 6;//APP 7;//other
     *@author LongJin
     * @return
     */
    public static int getFromPc(String client) {
        Http.Context context = ctx();
        Http.Request request = context.request();
        String head = request.getHeader("user-agent").toLowerCase();
        play.Logger.info("head = " + request.getHeader("user-agent"));
        int from = 0;//pc terminal
        boolean isPc = HttpRequestDeviceUtils.isPcDevice(request);//Judging whether it comes from pc
        if (!isPc) {
            if (head.contains("weibo")) {
                from = 1;//micro-blog
            } else if (getQq(head)) {//qq wrote a separate judgment method
                from = 2;//qq
            } else if (head.contains("micromessenger")) {
                from = 5;//WeChat
            } else if (StringUtils.isNotBlank(client)) {
                from = 6;//APP
            } else {
                from = 7;//Other
            }
        }
        return from;
    }

Does the judgment come from QQ:?

/**
     * Get QQ
     *
     * @param str
     * @return
     */
    public static boolean getQqCount(String str) {
        Pattern p = Pattern.compile("\\bqq\\b", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(str);
        boolean isQq=  false;
        while (m.find()) {
           isQq = true;
        }
        return isQq;

    }
The HttpRequestDeviceUtils.isPcDevice(request) determines whether the source pc side is implemented as follows:
/**Wap Specific Description Information in Gateway Via Header Information*/
    private static String mobileGateWayHeaders[]=new String[]{
    "ZXWAP",//ZTE provides via information for wap gateways, such as Via = ZXWAP Gateway ZTE Technologies,
    "chinamobile.com",//Nokia wap gateway of China Mobile, such as Via=WTP/1.1 GDSZ-PB-GW003-WAP07.gd.chinamobile.com (Nokia WAP Gateway 4.1 CD1/ECD13_D/4.1.04)
    "monternet.com",//Mobile Dreamnet Gateway, e.g. Via=WTP/1.1 BJBJ-PS-WAP1-GW08.bj1.monternet.com. (Nokia WAP Gateway 4.1 CD1/ECD13_E/4.1.05)
    "infoX",//wap gateways provided by Huawei, such as Via=HTTP/1.1 GDGZ-PS-GW011-WAP2 (infoX-WISG Huawei Technologies), or Via=infoX WAP Gateway V300R001 Huawei Technologies
    "XMS 724Solutions HTG",//wap Gateway of Foreign Telecom Operators
    "wap.lizongbo.com",//Header information simulated during self-testing
    "Bytemobile",//It seems to be a solution for mobile Internet to improve network efficiency, such as Via=1.1 Bytemobile OSN WebProxy/5.1
    };
    /**User-Agent keywords such as IE or Firefox browsers on computers*/
    private static String[] pcHeaders=new String[]{
    "Windows 98",
    "Windows ME",
    "Windows 2000",
    "Windows XP",
    "Windows NT",
    "Ubuntu",
    "Macintosh",
    "Linux"
    };
    /**Key words in User-Agent of mobile browser*/
    private static String[] mobileUserAgents=new String[]{
    "Nokia",//Nokia, there's a copycat machine that writes this, it's still a mobile phone, Mozilla/5.0 (Nokia 5800 Xpress Music), UC Apple Webkit (like Gecko) Safari/530
    "SAMSUNG",//Samsung Mobile SAMSUNG-GT-B7722/1.0+SHP/VPP/R5+Dolfin/1.5+Nextreaming+SMM-MMS/1.2.0+profile/MIDP-2.1+configuration/CLDC-1.1
    "MIDP-2",//j2me2.0,Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1 /110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML like Gecko) Safari/413
    "CLDC1.1",//M600/MIDP2.0/CLDC1.1/Screen-240X320
    "SymbianOS",//Saipan system,
    "MAUI",//MTK Shanzhai Machine Default ua
    "UNTRUSTED/1.0",//The ua of a suspected copycat can basically be determined to be a mobile phone
    "Windows CE",//Windows CE,Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.11)
    "iPhone",//Does the iPhone also switch to wap? Regardless of it, distinguish it first. Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; zh-cn) Apple WebKit/532.9 (KHTML like Gecko) Mobile/8B117
    "iPad",//ua, Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; zh-cn) AppleWebKit/531.21.10 (KHTML like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10
    "Android",//Does Android also switch to wap? Mozilla/5.0 (Linux; U; Android 2.1-update 1; zh-cn; XT800 Build/TITA_M2_16.22.7) Apple WebKit/530.17 (KHTML like Gecko) Version/4.0 Mobile Safari/530.17
    "BlackBerry",//BlackBerry8310/2.7.0.106-4.5.0.182
    "UCWEB",//Is ucweb only for wap pages? Nokia 5800 Xpress Music/ucweb 7.5.0.66/50/999
    "ucweb",//The lowercase UCWEB seems to be uc's proxy server Mozilla/6.0 (compatible; MSIE 6.0;). Opera ucweb-squid
    "BREW",//Strange ua s, such as REW-Applet/0x20068888 (BREW/3.1.5.20; DeviceId: 40105; Lang: zhcn) ucweb-squid
    "J2ME",//Strange ua, only four letters of J2ME
    "YULONG",//YULONG-CoolpadN68/10.14 IPANEL/2.0 CTC/1.0
    "YuLong",//Or Yulong?
    "COOLPAD",//YL-COOLPADS100/08.10.S100 POLARIS/2.9 CTC/1.0
    "TIANYU",//TIANYU-KTOUCH/V209/MIDP2.0/CLDC1.1/Screen-240X320
    "TY-",//Tianyu, TY-F6229/701116_6215_V0230 JUPITOR/2.2 CTC/1.0
    "K-Touch",//It's also Tianyu K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223 Release/30.07.2008 Browser/WAP2.0
    "Haier",//Haier Mobile, Haier-HG-M217_CMCC/3.0 Release/12.1.2007 Browser/WAP2.0
    "DOPOD",//Dopod mobile phone
    "Lenovo",// Lenovo-P650WG/S100 LMP/LML Release/2010.02.22 Profile/MIDP2.0 Configuration/CLDC1.1
    "LENOVO",// Lenovo mobile phones, such as: LENOVO-P780/176A
    "HUAQIN",//Hua Qin mobile phone
    "AIGO-",//Patriots have also been out of mobile phones, AIGO-800C/2.04 TMSS-BROWSER/1.0.0 CTC/1.0
    "CTC/1.0",//Ambiguous meaning
    "CTC/2.0",//Ambiguous meaning
    "CMCC",//Mobile customized mobile phone, K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223 Release/30.07.2008 Browser/WAP2.0
    "DAXIAN",//DAXIAN X180 UP.Browser/6.2.3.2(GUI) MMP/2.0
    "MOT-",//Motorola, MOT-MOTOROKRE6/1.0 LinuxOS/2.4.20 Release/8.4.2006 Browser/Opera 8.00 Profile/MIDP2.0 Configuration/CLDC1.1 Software/R533_G_11.10.54R
    "SonyEricsson",// Sony Ericsson P990i/R100 Mozilla/4.0 (compatible; MSIE 6.0; Symbian OS; 405) Opera 8.65 [zh-CN]
    "GIONEE",//Gionee
    "HTC",//HTC mobile phone
    "ZTE",//ZTE-A211/P109A2V1.0.0/WAP2.0 Profile
    "HUAWEI",//Huawei Mobile,
    "webOS",//palm cell phone, Mozilla/5.0 (webOS/1.4.5; U; zh-CN) Apple WebKit/532.2 (KHTML like Gecko) Version/1.0 Safari/532.2 Pre/1.0
    "GoBrowser",//3g GoBrowser.User-Agent=Nokia5230/GoBrowser/2.0.290 Safari
    "IEMobile",//Windows CE Mobile Phone has its own browser.
    "WAP2.0"//wap 2.0-enabled
    };

/**
     * Judging whether it is pc terminal
     * @param request
     * @return
     */
    public static boolean isPcDevice(Request request){
    	   boolean b = false;
           boolean pcFlag = false;
           boolean mobileFlag = false;
           String via = request.getHeader("Via");
           String userAgent = request.getHeader("user-agent");
           //String xMsisdn = request.getHeader("X-MSISDN");
/*           Map<String, String[]> headers = request.headers();
           play.Logger.info("xM = "+headers);*/
    	 for (int i = 0; userAgent!=null && !userAgent.trim().equals("") && i < pcHeaders.length; i++) {
             if(userAgent.contains(pcHeaders[i])){
                 pcFlag = true;
                 break;
             }
         }
    	 for (int i = 0; via!=null && !via.trim().equals("") && i < mobileGateWayHeaders.length; i++) {
             if(via.contains(mobileGateWayHeaders[i])){
                 mobileFlag = true;
                 break;
             }
         }
         for (int i = 0;!mobileFlag && userAgent!=null && !userAgent.trim().equals("") && i < mobileUserAgents.length; i++) {
             if(userAgent.contains(mobileUserAgents[i])){
                 mobileFlag = true;
                 break;
             }
         }
         
         if(mobileFlag==false && pcFlag==true){
             b=true;
         }
		return b;
    	
    	
    	
    }

  

At this point, the classification of visits is basically completed, and the original article ranking function can be achieved by combining the statistics of visits, and the statistical details can be made into pie chart, which says that the power is suddenly cut off, maybe it's going to be a holiday and excitement.

ps: Here are just some of the core parts, but the detailed implementation is not explained. In the whole process of my implementation, there is a statistical accuracy problem, that is, the same as the external network under the unified local area network, which leads to the existence of statistical access under the same local area network only one person, looking forward to God's guidance.

Posted by wih on Mon, 25 Mar 2019 19:54:29 -0700