|
@@ -1,5 +1,7 @@
|
|
|
package org.jeecg.modules.adweb.dmp.service.google;
|
|
|
|
|
|
+import static org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportConstant.*;
|
|
|
+
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
@@ -24,9 +26,11 @@ import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportType;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.CustomReportData;
|
|
|
import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.GAReportDataDTO;
|
|
|
import org.jeecg.modules.adweb.dmp.entity.GACountryReport;
|
|
|
+import org.jeecg.modules.adweb.dmp.entity.GAPagePathReport;
|
|
|
import org.jeecg.modules.adweb.dmp.entity.GASourceMediumReport;
|
|
|
import org.jeecg.modules.adweb.dmp.entity.GoogleGTM;
|
|
|
import org.jeecg.modules.adweb.dmp.service.IGACountryReportService;
|
|
|
+import org.jeecg.modules.adweb.dmp.service.IGAPagePathReportService;
|
|
|
import org.jeecg.modules.adweb.dmp.service.IGASourceMediumReportService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -34,6 +38,7 @@ import org.springframework.core.ParameterizedTypeReference;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
@@ -59,6 +64,8 @@ public class GAReportService {
|
|
|
|
|
|
@Autowired private IGASourceMediumReportService gaSourceMediumReportService;
|
|
|
|
|
|
+ @Autowired private IGAPagePathReportService gaPagePathReportService;
|
|
|
+
|
|
|
@Autowired private CommonMapper commonMapper;
|
|
|
|
|
|
private RestTemplate restTemplate;
|
|
@@ -68,6 +75,23 @@ public class GAReportService {
|
|
|
this.restTemplate = RestTemplateUtil.getRestTemplate(60, 60, dataBridgeApiToken);
|
|
|
}
|
|
|
|
|
|
+ /** 拉取并同步Google Analytics报表 */
|
|
|
+ public void syncGAReport() {
|
|
|
+ // TODO
|
|
|
+ List<GoogleGTM> googleGTMS = Collections.emptyList();
|
|
|
+
|
|
|
+ // 每个帐号同步三张报表
|
|
|
+ for (GoogleGTM googleGTM : googleGTMS) {
|
|
|
+ try {
|
|
|
+ this.syncGACountryReport(googleGTM);
|
|
|
+ this.syncGASourceMediumReport(googleGTM);
|
|
|
+ this.syncGAPagePathReport(googleGTM);
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ log.warn("同步GA报表异常, siteId = {], error = {}", googleGTM.getSiteId(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 拉取并同步Google Analytics - Country报表
|
|
|
*
|
|
@@ -193,6 +217,67 @@ public class GAReportService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 拉取并同步Google Analytics - Page Path报表
|
|
|
+ *
|
|
|
+ * @param googleGTM
|
|
|
+ */
|
|
|
+ private void syncGAPagePathReport(GoogleGTM googleGTM) {
|
|
|
+ // 1. 报表时间区间
|
|
|
+ Date startDate = this.getReportStartDate("dmp_ga_page_path_report", googleGTM.getSiteId());
|
|
|
+ Date endDate = new Date();
|
|
|
+
|
|
|
+ // 2. 构建GA报表请求参数
|
|
|
+ // 使用ADWEB_CUSTOM_REPORT,不是ADWEB_PAGE_PATH_VIEW - 需要date作为dimension
|
|
|
+ GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
|
|
|
+ gaReportRequest.setPropertyResourceName(
|
|
|
+ GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
|
|
|
+ gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
|
|
|
+ gaReportRequest.setStartDate(DateUtils.date2Str(startDate, DateUtils.date_sdf.get()));
|
|
|
+ gaReportRequest.setEndDate(DateUtils.date2Str(endDate, DateUtils.date_sdf.get()));
|
|
|
+ gaReportRequest.setMetrics(
|
|
|
+ List.of(
|
|
|
+ ReportConstant.METRIC_ENGAGEMENT_RATE,
|
|
|
+ ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
|
|
|
+ ReportConstant.METRIC_USER_ENGAGEMENT_DURATION));
|
|
|
+ gaReportRequest.setDimensions(
|
|
|
+ List.of(ReportConstant.DIMENSION_DATE, ReportConstant.DIMENSION_PAGE_PATH));
|
|
|
+ gaReportRequest.setOrderByType(OrderByType.DIMENSIONS);
|
|
|
+ gaReportRequest.setOrderBy(ReportConstant.DIMENSION_DATE);
|
|
|
+ List<CustomReportData> reportDataList =
|
|
|
+ runGAReport(gaReportRequest, CustomReportData.class);
|
|
|
+
|
|
|
+ // 2. 转化为DB entity
|
|
|
+ List<GAPagePathReport> pagePathReport = Lists.newArrayList();
|
|
|
+ for (CustomReportData reportData : reportDataList) {
|
|
|
+ GAPagePathReport reportRow = new GAPagePathReport();
|
|
|
+ reportRow.setSiteId(googleGTM.getSiteId());
|
|
|
+ reportRow.setDate(
|
|
|
+ DateUtils.str2Date(
|
|
|
+ reportData.get(ReportConstant.DIMENSION_DATE),
|
|
|
+ DateUtils.date_sdf.get()));
|
|
|
+ reportRow.setPagePath(reportData.get(DIMENSION_PAGE_PATH));
|
|
|
+ reportRow.setEngagementRate(
|
|
|
+ Double.parseDouble(reportData.get(ReportConstant.METRIC_ENGAGEMENT_RATE)));
|
|
|
+ reportRow.setPageViews(
|
|
|
+ Integer.parseInt(reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS)));
|
|
|
+ reportRow.setAvgTimeOnPage(
|
|
|
+ Double.parseDouble(
|
|
|
+ reportData.get(ReportConstant.METRIC_USER_ENGAGEMENT_DURATION)));
|
|
|
+
|
|
|
+ pagePathReport.add(reportRow);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 更新数据库 - 删除旧数据,插入新数据
|
|
|
+ QueryWrapper<GAPagePathReport> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("site_id", googleGTM.getSiteId());
|
|
|
+ queryWrapper.ge("date", startDate);
|
|
|
+ queryWrapper.le("date", endDate);
|
|
|
+ gaPagePathReportService.remove(queryWrapper);
|
|
|
+
|
|
|
+ gaPagePathReportService.saveBatch(pagePathReport, pagePathReport.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 请求Google Analytics报表,http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
|
|
|
*
|
|
|
* @param gaReportRequest
|