Baidu Statistics provides users with access to API, enabling users to customize UI without entering Baidu Statistics website.There are two account systems in Baidu Statistics: Baidu Business Account and Baidu Account. The former is the early user account of Baidu Statistics. Based on Baidu Business Account, this paper explains how to access statistical API.
- Official API documentation: https://tongji.baidu.com/api/manual/
- White Paper: https://tongji.baidu.com/web/image/Baidu Publishes Web Site Analysis White Paper V3.0.pdf
Common Request Header
Requests from interfaces require that JSON-formatted data be sent to the appropriate API via POST, noting that JSON data is placed in the requested RawBody, rather than a common key pair worth the POST structure.The structure of the request data is divided into two parts, the header part is user information and the body part: the body part is different for different APIs.
String json = "{\"header\": " + "{" + "\"username\": \"xxxxx\", " + "\"password\": \"xxxxx\"," + "\"token\": \"xxxx\"," + "\"account_type\": 1" + "}" + "}"; String html = HttpBasicRequest.post("https://api.baidu.com/json/tongji/v1/ReportService/getSiteList", json, conn -> { conn.addRequestProperty("Content-type", "application/json"); });
The request must specify Content-type as application/json, otherwise it cannot be requested.
In the implementation, we define the header with a map and convert it to a JSON string, as shown below.
/** * Get Request Header * * @return */ private static String getHeader() { Map<String, Object> header = new HashMap<String, Object>() { private static final long serialVersionUID = 1L; { put("username", ConfigService.get("baidu_tongji.api_username")); put("password", ConfigService.get("baidu_tongji.api_password")); put("token", ConfigService.get("baidu_tongji.api_token")); put("account_type", 1); } }; return JsonHelper.stringifyMap(header); }
Then you can pass in different business needs for the body, which is also a Map.
private static final String JSON = "{\"header\": %s, \"body\": %s}"; /** * Request API * * @param body * @return */ private static String getData(Map<String, Object> body) { String req = String.format(JSON, getHeader(), JsonHelper.stringifyMap(body)); String json = HttpBasicRequest.post("https://api.baidu.com/json/tongji/v1/ReportService/getData", req, conn -> { conn.addRequestProperty("Content-type", "application/json"); }); return "json::" + json; }
Then make the request and return the result JSON.
Get a list of sites
There can be more than one site under a Baidu statistical account.Interface https://api.baidu.com/json/tongji/v1/ReportService/getSiteList .The key field is siteId, which does not have to be requested if it is known.
Get report data
This is the core API, and the interface is https://api.baidu.com/json/tongji/v1/ReportService/getData .In this interface, report data is obtained according to different parameters.This is the method field of the body.
Get an overview of the website: pv/uv today/yesterday
For general tips, the generated interface is as follows.
@GET @Path("getTimeTrendRpt") @Produces(MediaType.APPLICATION_JSON) public String getTimeTrendRpt() { Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.DAY_OF_MONTH, -1);// Yesterday String today = formater.format(now), yesterday = formater.format(calendar.getTime()); Map<String, Object> body = new HashMap<String, Object>() { private static final long serialVersionUID = 1L; { put("site_id", ConfigService.get("baidu_tongji.siteId")); put("start_date", yesterday); put("end_date", today); put("method", "overview/getTimeTrendRpt"); put("metrics", "pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time"); } }; return getData(body); }
Get an overview of the site: source site, search terms, entry page, visited page
The generated interface is as follows (section).
The default is yesterday's and today's data.
@GET @Path("getCommonTrackRpt") @Produces(MediaType.APPLICATION_JSON) public String getCommonTrackRpt() { Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.DAY_OF_MONTH, -1);// Yesterday String today = formater.format(now), yesterday = formater.format(calendar.getTime()); Map<String, Object> body = new HashMap<String, Object>() { private static final long serialVersionUID = 1L; { put("site_id", ConfigService.get("baidu_tongji.siteId")); put("start_date", yesterday); put("end_date", today); put("method", "overview/getCommonTrackRpt"); } }; return getData(body); }
Get pv/uv trend map
Obtain PV and UV data in Trend Analysis Report at a daily granularity.
@GET @Path("getTrend") @Produces(MediaType.APPLICATION_JSON) public String getTrend(@QueryParam("start_date") @NotNull String start_date, @QueryParam("end_date") @NotNull String end_date) { Map<String, Object> body = new HashMap<String, Object>() { private static final long serialVersionUID = 1L; { put("site_id", ConfigService.get("baidu_tongji.siteId")); put("start_date", start_date); put("end_date", end_date); put("method", "trend/time/a"); put("metrics", "pv_count,visit_count"); put("max_results", "0"); put("gran", "day"); } }; return getData(body); }
This system is based on the front end of vue.This requires a chart component to display, and I started using it vue2-frappe But for some reason, there may be a style conflict, and adding my style does not show that DOM exists. I don't bother with this component, so I change to another lightweight chart component, a lighter one, a national work, for Laue. https://laue.js.org ,https://github.com/qingwei-li/laue) .
SummaryAt present, not all API s of Baidu Statistics are fully integrated, but three of its most important interfaces are used, which is enough to give users a preliminary display.