|
@@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.jeecg.modules.adweb.common.util.AdwebRedisUtil;
|
|
|
import org.jeecg.modules.adweb.common.util.DateUtil;
|
|
|
+import org.jeecg.modules.adweb.common.util.NumberUtil;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.google.analytics.GAPropertyDTO;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.GAReportRequestDTO;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.OrderByType;
|
|
@@ -18,6 +19,7 @@ import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.CustomReport
|
|
|
import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
|
|
|
import org.jeecg.modules.adweb.dmp.service.google.GAReportService;
|
|
|
import org.jeecg.modules.adweb.dmp.vo.report.DeviceStatsVO;
|
|
|
+import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
|
|
|
import org.jeecg.modules.redis.CacheConfig;
|
|
|
import org.jeecg.modules.redis.TTLCacheManager;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -35,98 +37,103 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
public class DMPReportService {
|
|
|
|
|
|
+ @Autowired private IGoogleGTMService googleGTMService;
|
|
|
@Autowired private GAReportService gaReportService;
|
|
|
+ @Autowired private AdwebRedisUtil redisUtil;
|
|
|
|
|
|
- @Autowired private IGoogleGTMService googleGTMService;
|
|
|
+ /** 拉取Google Analytics - Session Source Medium报表 */
|
|
|
+ @Cacheable(
|
|
|
+ cacheManager = CacheConfig.TTL_CACHE_MANAGER,
|
|
|
+ cacheNames =
|
|
|
+ "dmp:getSourceMediumStats"
|
|
|
+ + TTLCacheManager.TTL_SPLITTER
|
|
|
+ + 60 * 10) // Redis TTL为10分钟
|
|
|
+ public List<SourceMediumStatsVO> getSourceMediumStats(String siteCode, Date start, Date end) {
|
|
|
+ GoogleGTM googleGTM = this.getGTMAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleGTM.getGaPropertyId())) {
|
|
|
+ log.info("Google Analytics帐号未配置, siteCode = {}", siteCode);
|
|
|
+ return Collections.EMPTY_LIST;
|
|
|
+ }
|
|
|
|
|
|
- @Autowired private AdwebRedisUtil redisUtil;
|
|
|
+ // 1. 构建GA报表请求参数
|
|
|
+ // 使用ReportType.ADWEB_CUSTOM_REPORT
|
|
|
+ Pair<String, String> dateRange = this.getDateRange(start, end);
|
|
|
+
|
|
|
+ GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
|
|
|
+ gaReportRequest.setPropertyResourceName(
|
|
|
+ GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
|
|
|
+ gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
|
|
|
+ gaReportRequest.setStartDate(dateRange.getLeft());
|
|
|
+ gaReportRequest.setEndDate(dateRange.getRight());
|
|
|
+ gaReportRequest.setMetrics(
|
|
|
+ List.of(
|
|
|
+ ReportConstant.METRIC_TOTAL_USERS,
|
|
|
+ ReportConstant.METRIC_NEW_USERS,
|
|
|
+ ReportConstant.METRIC_SESSIONS,
|
|
|
+ ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
|
|
|
+ ReportConstant.METRIC_AVG_SESSION_DURATION,
|
|
|
+ ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION));
|
|
|
+ gaReportRequest.setDimensions(List.of(ReportConstant.DIMENSION_SESSION_SOURCE_MEDIUM));
|
|
|
+ gaReportRequest.setOrderByType(OrderByType.METRICS);
|
|
|
+ gaReportRequest.setOrderBy(ReportConstant.METRIC_TOTAL_USERS);
|
|
|
+ gaReportRequest.setOrderByDesc(true);
|
|
|
|
|
|
- //
|
|
|
- // public List<SourceMediumStatsVO> getSourceMediumStats(String siteCode, Date start, Date
|
|
|
- // end) {
|
|
|
- // GoogleGTM googleGTM = this.getGTMAccount(siteCode);
|
|
|
- // if (StringUtils.isBlank(googleGTM.getGaPropertyId())) {
|
|
|
- // log.info("Google Analytics帐号未配置, siteCode = {}", siteCode);
|
|
|
- // return Collections.EMPTY_LIST;
|
|
|
- // }
|
|
|
- //
|
|
|
- // // 2. 构建GA报表请求参数
|
|
|
- // // 使用ReportType.ADWEB_CUSTOM_REPORT,不是ADWEB_SESSION_SOURCE_MEDIUM_VIEW -
|
|
|
- // 需要date作为dimension
|
|
|
- // GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
|
|
|
- // gaReportRequest.setPropertyResourceName(
|
|
|
- // GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
|
|
|
- // gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
|
|
|
- // gaReportRequest.setStartDate(
|
|
|
- // DateUtil.formatDate(
|
|
|
- // Optional.ofNullable(start)
|
|
|
- // // 如果start为空,设置为无限早时间
|
|
|
- // .orElse(DateUtil.parseDate("2016-01-01",
|
|
|
- // DateUtil.DATE_FORMAT)),
|
|
|
- // DateUtil.DATE_FORMAT));
|
|
|
- // gaReportRequest.setEndDate(
|
|
|
- // DateUtil.formatDate(
|
|
|
- // Optional.ofNullable(end).orElse(new Date()), DateUtil.DATE_FORMAT));
|
|
|
- // gaReportRequest.setMetrics(
|
|
|
- // List.of(
|
|
|
- // ReportConstant.METRIC_TOTAL_USERS,
|
|
|
- // ReportConstant.METRIC_NEW_USERS,
|
|
|
- // ReportConstant.METRIC_SESSIONS,
|
|
|
- // ReportConstant.METRIC_BOUNCE_RATE,
|
|
|
- // ReportConstant.METRIC_AVG_SESSION_DURATION,
|
|
|
- // ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION));
|
|
|
- //
|
|
|
- // gaReportRequest.setDimensions(List.of(ReportConstant.DIMENSION_SESSION_SOURCE_MEDIUM));
|
|
|
- // List<CustomReportData> reportDataList =
|
|
|
- // gaReportService.runGAReport(gaReportRequest, CustomReportData.class);
|
|
|
- //
|
|
|
- // // 2. 转化为DB entity
|
|
|
- // List<SourceMediumStatsVO> sourceMediumReport = Lists.newArrayList();
|
|
|
- // for (CustomReportData reportData : reportDataList) {
|
|
|
- // SourceMediumStatsVO sourceMediumStatsVO = new SourceMediumStatsVO();
|
|
|
- //
|
|
|
- // sourceMediumStatsVO.setType(
|
|
|
- // reportData.get(ReportConstant.DIMENSION_SESSION_SOURCE_MEDIUM));
|
|
|
- //
|
|
|
- // sourceMediumStatsVO.setTotalUsers(
|
|
|
- // Integer.parseInt(reportData.get(ReportConstant.METRIC_TOTAL_USERS)));
|
|
|
- // sourceMediumStatsVO.setNewUsers(
|
|
|
- // Integer.parseInt(reportData.get(ReportConstant.METRIC_NEW_USERS)));
|
|
|
- // sourceMediumStatsVO.setSessions(
|
|
|
- // Integer.parseInt(reportData.get(ReportConstant.METRIC_SESSIONS)));
|
|
|
- //
|
|
|
- // sourceMediumStatsVO.setAvgSessionDuration(
|
|
|
- //
|
|
|
- // Double.parseDouble(reportData.get(ReportConstant.METRIC_AVG_SESSION_DURATION)));
|
|
|
- // sourceMediumStatsVO.setPageViewsPerSession(
|
|
|
- // Double.parseDouble(
|
|
|
- //
|
|
|
- // reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION)));
|
|
|
- //
|
|
|
- // sourceMediumReport.add(sourceMediumStatsVO);
|
|
|
- // }
|
|
|
- //
|
|
|
- // // 1. 时间区间内所有国家totalUsers总数
|
|
|
- // int totalUsersSum =
|
|
|
- //
|
|
|
- // sourceMediumReport.stream().mapToInt(SourceMediumStatsVO::getTotalUsers).sum();
|
|
|
- //
|
|
|
- // // 2. VO数据填充
|
|
|
- // for (SourceMediumStatsVO sourceMediumStatsVO : sourceMediumReport) {
|
|
|
- // int totalUsers = sourceMediumStatsVO.getTotalUsers();
|
|
|
- // sourceMediumStatsVO.setTotalUsersProportion(
|
|
|
- // NumberUtil.formatPercentage(
|
|
|
- // NumberUtil.safeDivide(totalUsers, totalUsersSum), 2));
|
|
|
- //
|
|
|
- // sourceMediumStatsVO.setNewUsersRatio(
|
|
|
- // NumberUtil.formatPercentage(
|
|
|
- // NumberUtil.safeDivide(sourceMediumStatsVO.getNewUsers(),
|
|
|
- // totalUsers),
|
|
|
- // 2));
|
|
|
- // }
|
|
|
- //
|
|
|
- // return sourceMediumReport;
|
|
|
- // }
|
|
|
+ // 2. 请求API接口
|
|
|
+ List<CustomReportData> reportDataList =
|
|
|
+ gaReportService.runGAReport(gaReportRequest, CustomReportData.class);
|
|
|
+
|
|
|
+ // 2.1 转化为VOs
|
|
|
+ List<SourceMediumStatsVO> sourceMediumStatsVOs = Lists.newArrayList();
|
|
|
+ for (CustomReportData reportData : reportDataList) {
|
|
|
+ SourceMediumStatsVO sourceMediumStatsVO = new SourceMediumStatsVO();
|
|
|
+ sourceMediumStatsVO.setType(
|
|
|
+ reportData.get(ReportConstant.DIMENSION_SESSION_SOURCE_MEDIUM));
|
|
|
+ sourceMediumStatsVO.setTotalUsers(
|
|
|
+ Integer.parseInt(reportData.get(ReportConstant.METRIC_TOTAL_USERS)));
|
|
|
+ sourceMediumStatsVO.setNewUsers(
|
|
|
+ Integer.parseInt(reportData.get(ReportConstant.METRIC_NEW_USERS)));
|
|
|
+ sourceMediumStatsVO.setSessions(
|
|
|
+ Integer.parseInt(reportData.get(ReportConstant.METRIC_SESSIONS)));
|
|
|
+ sourceMediumStatsVO.setPageViews(
|
|
|
+ Integer.parseInt(reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS)));
|
|
|
+ sourceMediumStatsVO.setAvgSessionDuration(
|
|
|
+ NumberUtil.formatDecimal(
|
|
|
+ Double.parseDouble(
|
|
|
+ reportData.get(
|
|
|
+ ReportConstant.METRIC_AVG_SESSION_DURATION)),
|
|
|
+ 2)
|
|
|
+ .doubleValue());
|
|
|
+ sourceMediumStatsVO.setPageViewsPerSession(
|
|
|
+ NumberUtil.formatDecimal(
|
|
|
+ Double.parseDouble(
|
|
|
+ reportData.get(
|
|
|
+ ReportConstant
|
|
|
+ .METRIC_SCREEN_PAGE_VIEWS_PER_SESSION)),
|
|
|
+ 2)
|
|
|
+ .doubleValue());
|
|
|
+
|
|
|
+ sourceMediumStatsVOs.add(sourceMediumStatsVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2.2 时间区间内所有媒介totalUsers总数
|
|
|
+ int totalUsersSum =
|
|
|
+ sourceMediumStatsVOs.stream().mapToInt(SourceMediumStatsVO::getTotalUsers).sum();
|
|
|
+
|
|
|
+ // 2.3 VO数据计算填充
|
|
|
+ for (SourceMediumStatsVO sourceMediumStatsVO : sourceMediumStatsVOs) {
|
|
|
+ int totalUsers = sourceMediumStatsVO.getTotalUsers();
|
|
|
+ sourceMediumStatsVO.setTotalUsersProportion(
|
|
|
+ NumberUtil.formatPercentage(
|
|
|
+ NumberUtil.safeDivide(totalUsers, totalUsersSum), 2));
|
|
|
+
|
|
|
+ sourceMediumStatsVO.setNewUsersRatio(
|
|
|
+ NumberUtil.formatPercentage(
|
|
|
+ NumberUtil.safeDivide(sourceMediumStatsVO.getNewUsers(), totalUsers),
|
|
|
+ 2));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sourceMediumStatsVOs;
|
|
|
+ }
|
|
|
|
|
|
/** 拉取Google Analytics - Device报表 */
|
|
|
@Cacheable(
|
|
@@ -164,6 +171,7 @@ public class DMPReportService {
|
|
|
List<CustomReportData> reportDataList =
|
|
|
gaReportService.runGAReport(gaReportRequest, CustomReportData.class);
|
|
|
|
|
|
+ // 3. 转化为VOs
|
|
|
List<DeviceStatsVO> deviceStatsVOs = Lists.newArrayList();
|
|
|
for (CustomReportData reportData : reportDataList) {
|
|
|
DeviceStatsVO deviceStatsVO = new DeviceStatsVO();
|