神策 开源埋点 后端接收前端sdk采集数据
神策 开源埋点 后端接收前端sdk采集数据,uniapp sdk 和 web js sdk 说明
神策数据示例:
注意:
前后端调试中发现
前端使用 uniapp sdk 时 向后端上报数据 用的get方法
前端使用 web js sdk 时 向后端上报数据 用的post test/plain;charset=UTF-8方法
// 前端上传的数据示例:data=eyJkaXN0aW5jdF9pZCI6Im9JQXRPNmZpT1V6R0hJSVk2VlV0OXRSZU9OMFlfNCIsImxpYiI6eyIkbGliIjoianMiLCIkbGliX21ldGhvZCI6ImNvZGUiLCIkbGliX3ZlcnNpb24iOiIxLjE4LjIwIn0sInByb3BlcnRpZXMiOnsiJHRpbWV6b25lX29mZnNldCI6LTQ4MCwiJHNjcmVlbl9oZWlnaHQiOjg0NCwiJHNjcmVlbl93aWR0aCI6MzkwLCIkbGliIjoianMiLCIkbGliX3ZlcnNpb24iOiIxLjE4LjIwIiwiJGxhdGVzdF90cmFmZmljX3NvdXJjZV90eXBlIjoi55u05o6l5rWB6YePIiwiJGxhdGVzdF9zZWFyY2hfa2V5d29yZCI6IuacquWPluWIsOWAvF%2Fnm7TmjqXmiZPlvIAiLCIkbGF0ZXN0X3JlZmVycmVyIjoiIiwiJGxhdGVzdF9sYW5kaW5nX3BhZ2UiOiJodHRwczovL3Rlc3QuaGVhbHRoLnNpbm9jcmVkaXQueGluL21lZGljYWwtdW5pYXBwLXdlaXhpbi10ZXN0LyMvcGFnZXMvYXBwb2ludG1lbnRkb2N0b3IvUmVnaXN0ZXJlZEhvc3BpdGFsP3NvdXJjZT13eCIsIl9zYV9jaGFubmVsX2xhbmRpbmdfdXJsIjoiIiwiJGlzX2NoYW5uZWxfY2FsbGJhY2tfZXZlbnQiOnRydWUsInVhIjoiTW96aWxsYS81LjAgKGlQaG9uZTsgQ1BVIGlQaG9uZSBPUyAxNV82IGxpa2UgTWFjIE9TIFgpIEFwcGxlV2ViS2l0LzYwNS4xLjE1IChLSFRNTCwgbGlrZSBHZWNrbykgTW9iaWxlLzE1RTE0OCBNaWNyb01lc3Nlbmdlci84LjAuMjMoMHgxODAwMTcyZikgTmV0VHlwZS80RyBMYW5ndWFnZS96aF9DTiIsInVybCI6Im1lZGljYWwvbWVkaWNhbGluZm9ybWF0aW9uL2dldEluZm9ybWF0aW9uQXJ0aWNsZUxpc3QvMTAiLCJjb2RlIjowLCJtc2ciOiJzdWNjZXNzIiwiJGlzX2ZpcnN0X2RheSI6ZmFsc2UsIiRsYXRlc3RfcmVmZXJyZXJfaG9zdCI6IiIsIiR1cmwiOiJodHRwczovL3Rlc3QuaGVhbHRoLnNpbm9jcmVkaXQueGluL21lZGljYWwtdW5pYXBwLXdlaXhpbi10ZXN0LyMvcGFnZXMvYXBwb2ludG1lbnRkb2N0b3IvUmVnaXN0ZXJlZEhvc3BpdGFsP3NvdXJjZT13eCIsIiR0aXRsZSI6IumAieaLqeWMu%2BmZoiJ9LCJsb2dpbl9pZCI6Im9JQXRPNmZpT1V6R0hJSVk2VlV0OXRSZU9OMFlfNCIsImFub255bW91c19pZCI6Im9JQXRPNmZpT1V6R0hJSVk2VlV0OXRSZU9OMFkiLCJ0eXBlIjoidHJhY2siLCJldmVudCI6Imh0dHAiLCJ0aW1lIjoxNjYwNzMwMzk0NjIxLCJfdHJhY2tfaWQiOjU4OTM0NjIxLCJfZmx1c2hfdGltZSI6MTY2MDczMDM5NDYyMX0%3D&ext=crc%3D-479460134
controller
@GetMapping(value = "log")
@ApiOperation("接受数据埋点日志")
public void logGet(HttpServletRequest request, String data) throws Exception {
System.out.println("get data = " + data);
}
@PostMapping(value = "log", consumes = "text/plain;charset=UTF-8")
@ApiOperation("接受数据埋点日志")
public void logPost(HttpServletRequest request) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
String buffer = null;
StringBuffer str = new StringBuffer();
while ((buffer = br.readLine()) != null) {
str.append(buffer);
}
String data = str.toString();
if (StrUtil.isNotBlank(data)) {
String[] dataArray = data.split("&ext=");
String d = dataArray[0].replaceAll("data=", "");
System.out.println("post data = " + d);
}
}
数据处理
需将接收到的 data= 数据进行 URLDecoder解码 在进行 base64解码
String urlDecode = URLDecoder.decode(data, "UTF-8");
String decodeStr = Base64.decodeStr(urlDecode);
JSONObject jsonObject = JSON.parseObject(decodeStr);
System.out.println("接受数据埋点日志 data = " + jsonObject.toJSONString());
ip 和 user_agent
前端反馈:uniapp sdk 和 web js sdk 没有默认上报 ip 和 user_agent
解决 :可前端设置 神策 sdk的公众属性 进行传输 或者 后端 使用 httpServletRequest 解析
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)