Skip to content

Commit 3c2249e

Browse files
charmingohbinarywang
authored andcommitted
binarywang#565 小程序增加数据分析相关 API
* 微信开放平台:1. WxOpenInRedisConfigStorage 支持 JedisPool/JedisSentinelPool 等 Pool<Jedis> 的子类;2. WxOpenInRedisConfigStorage 增加 keyPrefix 以支持可配置的前缀; * 微信开放平台:增加小程序代码模板库管理 * 小程序:增加代码管理相关 API * 小程序:增加修改服务器地址、成员管理 API * 小程序:增加数据分析相关 API
1 parent ba93544 commit 3c2249e

19 files changed

+1048
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo;
4+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaSummaryTrend;
5+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait;
6+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution;
7+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitPage;
8+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitTrend;
9+
import me.chanjar.weixin.common.exception.WxErrorException;
10+
11+
import java.util.Date;
12+
import java.util.List;
13+
14+
/**
15+
* 小程序数据分析相关接口
16+
* 文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/analysis.html
17+
*
18+
* @author <a href="https://github.com/charmingoh">Charming</a>
19+
* @since 2018-04-28
20+
*/
21+
public interface WxMaAnalysisService {
22+
String GET_DAILY_SUMMARY_TREND_URL = "https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend";
23+
String GET_DAILY_VISIT_TREND_URL = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend";
24+
String GET_WEEKLY_VISIT_TREND_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend";
25+
String GET_MONTHLY_VISIT_TREND_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend";
26+
String GET_VISIT_DISTRIBUTION_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution";
27+
String GET_DAILY_RETAIN_INFO_URL = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo";
28+
String GET_WEEKLY_RETAIN_INFO_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo";
29+
String GET_MONTHLY_RETAIN_INFO_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo";
30+
String GET_VISIT_PAGE_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage";
31+
String GET_USER_PORTRAIT_URL = "https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait";
32+
33+
/**
34+
* 查询概况趋势
35+
* 温馨提示:小程序接口目前只能查询一天的数据,即 beginDate 和 endDate 一样
36+
*
37+
* @param beginDate 开始日期
38+
* @param endDate 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日
39+
* @return 概况趋势
40+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
41+
*/
42+
List<WxMaSummaryTrend> getDailySummaryTrend(Date beginDate, Date endDate) throws WxErrorException;
43+
44+
/**
45+
* 获取日访问趋势
46+
* 温馨提示:小程序接口目前只能查询一天的数据,即 beginDate 和 endDate 一样
47+
*
48+
* @param beginDate 开始日期
49+
* @param endDate 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日
50+
* @return 日访问趋势
51+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
52+
*/
53+
List<WxMaVisitTrend> getDailyVisitTrend(Date beginDate, Date endDate) throws WxErrorException;
54+
55+
/**
56+
* 获取周访问趋势
57+
* 限定查询一个自然周的数据,时间必须按照自然周的方式输入: 如:20170306(周一), 20170312(周日)
58+
*
59+
* @param beginDate 开始日期,为周一日期
60+
* @param endDate 结束日期,为周日日期,限定查询一周数据
61+
* @return 周访问趋势(每项数据都是一个自然周汇总)
62+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
63+
*/
64+
List<WxMaVisitTrend> getWeeklyVisitTrend(Date beginDate, Date endDate) throws WxErrorException;
65+
66+
/**
67+
* 获取月访问趋势
68+
* 限定查询一个自然月的数据,时间必须按照自然月的方式输入: 如:20170201(月初), 20170228(月末)
69+
*
70+
* @param beginDate 开始日期,为自然月第一天
71+
* @param endDate 结束日期,为自然月最后一天,限定查询一个月数据
72+
* @return 月访问趋势(每项数据都是一个自然月汇总)
73+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
74+
*/
75+
List<WxMaVisitTrend> getMonthlyVisitTrend(Date beginDate, Date endDate) throws WxErrorException;
76+
77+
/**
78+
* 获取访问分布
79+
* (此接口目前只能查询一天的数据,即 beginDate 和 endDate 一样)
80+
*
81+
* @param beginDate 开始日期,为周一日期
82+
* @param endDate 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日
83+
* @return 访问分布
84+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
85+
*/
86+
WxMaVisitDistribution getVisitDistribution(Date beginDate, Date endDate) throws WxErrorException;
87+
88+
/**
89+
* 日留存
90+
* (此接口目前只能查询一天的数据,即 beginDate 和 endDate 一样)
91+
*
92+
* @param beginDate 开始日期,为周一日期
93+
* @param endDate 结束日期,限定查询 1 天数据,endDate 允许设置的最大值为昨日
94+
* @return 日留存
95+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
96+
*/
97+
WxMaRetainInfo getDailyRetainInfo(Date beginDate, Date endDate) throws WxErrorException;
98+
99+
/**
100+
* 周留存
101+
* 限定查询一个自然周的数据,时间必须按照自然周的方式输入: 如:20170306(周一), 20170312(周日)
102+
*
103+
* @param beginDate 开始日期,为周一日期
104+
* @param endDate 结束日期,为周日日期,限定查询一周数据
105+
* @return 周留存
106+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
107+
*/
108+
WxMaRetainInfo getWeeklyRetainInfo(Date beginDate, Date endDate) throws WxErrorException;
109+
110+
/**
111+
* 月留存
112+
* 限定查询一个自然月的数据,时间必须按照自然月的方式输入: 如:20170201(月初), 20170228(月末)
113+
*
114+
* @param beginDate 开始日期,为自然月第一天
115+
* @param endDate 结束日期,为自然月最后一天,限定查询一个月数据
116+
* @return 月留存
117+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
118+
*/
119+
WxMaRetainInfo getMonthlyRetainInfo(Date beginDate, Date endDate) throws WxErrorException;
120+
121+
/**
122+
* 获取访问页面数据
123+
* 温馨提示:此接口目前只能查询一天的数据,即 beginDate 和 endDate 一样
124+
*
125+
* @param beginDate 开始日期
126+
* @param endDate 结束日期,限定查询1天数据,end_date允许设置的最大值为昨日
127+
* @return 访问页面数据
128+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
129+
*/
130+
List<WxMaVisitPage> getVisitPage(Date beginDate, Date endDate) throws WxErrorException;
131+
132+
/**
133+
* 获取小程序新增或活跃用户的画像分布数据
134+
* 时间范围支持昨天、最近7天、最近30天。
135+
* 其中,新增用户数为时间范围内首次访问小程序的去重用户数,
136+
* 活跃用户数为时间范围内访问过小程序的去重用户数。
137+
* 画像属性包括用户年龄、性别、省份、城市、终端类型、机型。
138+
*
139+
* @param beginDate 开始日期
140+
* @param endDate 结束日期,开始日期与结束日期相差的天数限定为0/6/29,分别表示查询最近1/7/30天数据,end_date允许设置的最大值为昨日
141+
* @return 小程序新增或活跃用户的画像分布数据
142+
* @throws WxErrorException 获取失败时抛出,具体错误码请看文档
143+
*/
144+
WxMaUserPortrait getUserPortrait(Date beginDate, Date endDate) throws WxErrorException;
145+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ public interface WxMaService {
135135
*/
136136
WxMaTemplateService getTemplateService();
137137

138+
/**
139+
* 数据分析相关查询服务
140+
*
141+
* @return WxMaAnalysisService
142+
*/
143+
WxMaAnalysisService getAnalysisService();
144+
138145
/**
139146
* 返回代码操作相关的 API
140147
*
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package cn.binarywang.wx.miniapp.api.impl;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaAnalysisService;
4+
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo;
6+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaSummaryTrend;
7+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait;
8+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution;
9+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitPage;
10+
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitTrend;
11+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
12+
import com.google.gson.JsonObject;
13+
import com.google.gson.JsonParser;
14+
import com.google.gson.reflect.TypeToken;
15+
import me.chanjar.weixin.common.exception.WxErrorException;
16+
import org.apache.commons.lang3.time.DateFormatUtils;
17+
18+
import java.lang.reflect.Type;
19+
import java.util.Date;
20+
import java.util.List;
21+
22+
/**
23+
* @author <a href="https://github.com/charmingoh">Charming</a>
24+
* @since 2018-04-28
25+
*/
26+
public class WxMaAnalysisServiceImpl implements WxMaAnalysisService {
27+
private static final JsonParser JSON_PARSER = new JsonParser();
28+
private WxMaService wxMaService;
29+
30+
public WxMaAnalysisServiceImpl(WxMaService wxMaService) {
31+
this.wxMaService = wxMaService;
32+
}
33+
34+
@Override
35+
public List<WxMaSummaryTrend> getDailySummaryTrend(Date beginDate, Date endDate) throws WxErrorException {
36+
return getAnalysisResultAsList(GET_DAILY_SUMMARY_TREND_URL, beginDate, endDate,
37+
new TypeToken<List<WxMaSummaryTrend>>() {
38+
}.getType());
39+
}
40+
41+
@Override
42+
public List<WxMaVisitTrend> getDailyVisitTrend(Date beginDate, Date endDate) throws WxErrorException {
43+
return getAnalysisResultAsList(GET_DAILY_VISIT_TREND_URL, beginDate, endDate,
44+
new TypeToken<List<WxMaVisitTrend>>() {
45+
}.getType());
46+
}
47+
48+
@Override
49+
public List<WxMaVisitTrend> getWeeklyVisitTrend(Date beginDate, Date endDate) throws WxErrorException {
50+
return getAnalysisResultAsList(GET_WEEKLY_VISIT_TREND_URL, beginDate, endDate,
51+
new TypeToken<List<WxMaVisitTrend>>() {
52+
}.getType());
53+
}
54+
55+
@Override
56+
public List<WxMaVisitTrend> getMonthlyVisitTrend(Date beginDate, Date endDate) throws WxErrorException {
57+
return getAnalysisResultAsList(GET_MONTHLY_VISIT_TREND_URL, beginDate, endDate,
58+
new TypeToken<List<WxMaVisitTrend>>() {
59+
}.getType());
60+
}
61+
62+
@Override
63+
public WxMaVisitDistribution getVisitDistribution(Date beginDate, Date endDate) throws WxErrorException {
64+
String responseContent = this.wxMaService.post(GET_VISIT_DISTRIBUTION_URL, toJson(beginDate, endDate));
65+
return WxMaVisitDistribution.fromJson(responseContent);
66+
}
67+
68+
@Override
69+
public WxMaRetainInfo getDailyRetainInfo(Date beginDate, Date endDate) throws WxErrorException {
70+
return getRetainInfo(beginDate, endDate, GET_DAILY_RETAIN_INFO_URL);
71+
}
72+
73+
@Override
74+
public WxMaRetainInfo getWeeklyRetainInfo(Date beginDate, Date endDate) throws WxErrorException {
75+
return getRetainInfo(beginDate, endDate, GET_WEEKLY_RETAIN_INFO_URL);
76+
}
77+
78+
@Override
79+
public WxMaRetainInfo getMonthlyRetainInfo(Date beginDate, Date endDate) throws WxErrorException {
80+
return getRetainInfo(beginDate, endDate, GET_MONTHLY_RETAIN_INFO_URL);
81+
}
82+
83+
@Override
84+
public List<WxMaVisitPage> getVisitPage(Date beginDate, Date endDate) throws WxErrorException {
85+
return getAnalysisResultAsList(GET_VISIT_PAGE_URL, beginDate, endDate,
86+
new TypeToken<List<WxMaVisitPage>>() {
87+
}.getType());
88+
}
89+
90+
@Override
91+
public WxMaUserPortrait getUserPortrait(Date beginDate, Date endDate) throws WxErrorException {
92+
String responseContent = this.wxMaService.post(GET_USER_PORTRAIT_URL, toJson(beginDate, endDate));
93+
return WxMaUserPortrait.fromJson(responseContent);
94+
}
95+
96+
private WxMaRetainInfo getRetainInfo(Date beginDate, Date endDate, String url) throws WxErrorException {
97+
String responseContent = this.wxMaService.post(url, toJson(beginDate, endDate));
98+
return WxMaRetainInfo.fromJson(responseContent);
99+
}
100+
101+
/**
102+
* 获取数据分析结果并返回 List,returnType 类型
103+
*
104+
* @param url 链接
105+
* @param returnType 返回的类型
106+
* @param <T> 返回的类型
107+
* @return List 类型的数据
108+
*/
109+
private <T> List<T> getAnalysisResultAsList(String url, Date beginDate, Date endDate, Type returnType) throws WxErrorException {
110+
String responseContent = this.wxMaService.post(url, toJson(beginDate, endDate));
111+
JsonObject response = JSON_PARSER.parse(responseContent).getAsJsonObject();
112+
boolean hasList = response.has("list");
113+
if (hasList) {
114+
return WxMaGsonBuilder.create().fromJson(response.getAsJsonArray("list"), returnType);
115+
} else {
116+
return null;
117+
}
118+
}
119+
120+
private static String toJson(Date beginDate, Date endDate) {
121+
JsonObject param = new JsonObject();
122+
param.addProperty("begin_date", DateFormatUtils.format(beginDate, "yyyyMMdd"));
123+
param.addProperty("end_date", DateFormatUtils.format(endDate, "yyyyMMdd"));
124+
return param.toString();
125+
}
126+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.binarywang.wx.miniapp.api.impl;
22

3+
import cn.binarywang.wx.miniapp.api.WxMaAnalysisService;
34
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
45
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
56
import cn.binarywang.wx.miniapp.api.WxMaMsgService;
@@ -52,6 +53,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
5253
private WxMaUserService userService = new WxMaUserServiceImpl(this);
5354
private WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this);
5455
private WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this);
56+
private WxMaAnalysisService analysisService = new WxMaAnalysisServiceImpl(this);
5557
private WxMaCodeService codeService = new WxMaCodeServiceImpl(this);
5658
private WxMaSettingService settingService = new WxMaSettingServiceImpl(this);
5759

@@ -294,6 +296,11 @@ public WxMaTemplateService getTemplateService() {
294296
return this.templateService;
295297
}
296298

299+
@Override
300+
public WxMaAnalysisService getAnalysisService() {
301+
return this.analysisService;
302+
}
303+
297304
@Override
298305
public WxMaCodeService getCodeService() {
299306
return this.codeService;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.binarywang.wx.miniapp.bean.analysis;
2+
3+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.Data;
6+
7+
import java.io.Serializable;
8+
import java.util.Map;
9+
10+
/**
11+
* 访问留存
12+
*
13+
* @author <a href="https://github.com/charmingoh">Charming</a>
14+
* @since 2018-04-28 14:41
15+
*/
16+
@Data
17+
public class WxMaRetainInfo implements Serializable {
18+
private static final long serialVersionUID = 8986403173656848413L;
19+
/**
20+
* 日留存:日期,yyyyMMdd 格式,如 20170313
21+
* 周留存:时间,如"20170306-20170312"
22+
* 月留存:时间,如"201702"
23+
*/
24+
@SerializedName(value = "refDate", alternate = "ref_date")
25+
private String refDate;
26+
/**
27+
* 新增用户留存
28+
* - key:
29+
* - 日留存:标识,0开始,0表示当天,1表示1天后,依此类推,key取值分别是:0,1,2,3,4,5,6,7,14,30
30+
* - 周留存:标识,0开始,0表示当周,1表示1周后,依此类推,key 取值分别是:0,1,2,3,4
31+
* - 月留存:标识,0开始,0表示当月,1表示1月后,key取值分别是:0,1
32+
* - value: key对应日期的新增用户数/活跃用户数(key=0时)或留存用户数(k>0时)
33+
*/
34+
private Map<Integer, Integer> visitUvNew;
35+
/**
36+
* 活跃用户留存
37+
*/
38+
private Map<Integer, Integer> visitUv;
39+
40+
public static WxMaRetainInfo fromJson(String json) {
41+
return WxMaGsonBuilder.create().fromJson(json, WxMaRetainInfo.class);
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.binarywang.wx.miniapp.bean.analysis;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* 小程序概况趋势
10+
*
11+
* @author <a href="https://github.com/charmingoh">Charming</a>
12+
* @since 2018-04-28
13+
*/
14+
@Data
15+
public class WxMaSummaryTrend implements Serializable {
16+
private static final long serialVersionUID = 1379688517709317935L;
17+
/**
18+
* 日期,yyyyMMdd 格式,如 20170313
19+
*/
20+
@SerializedName(value = "refDate", alternate = "ref_date")
21+
private String refDate;
22+
/**
23+
* 累计用户数
24+
*/
25+
@SerializedName(value = "visitTotal", alternate = "visit_total")
26+
private Long visitTotal;
27+
/**
28+
* 转发次数
29+
*/
30+
@SerializedName(value = "sharePv", alternate = "share_pv")
31+
private Long sharePv;
32+
/**
33+
* 转发人数
34+
*/
35+
@SerializedName(value = "shareUv", alternate = "share_uv")
36+
private Long shareUv;
37+
}

0 commit comments

Comments
 (0)