Browse Source

GA page path report

wfansh 5 months ago
parent
commit
7f0cfe649e

+ 67 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/google/GAReportService.java

@@ -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;
@@ -59,6 +63,8 @@ public class GAReportService {
 
     @Autowired private IGASourceMediumReportService gaSourceMediumReportService;
 
+    @Autowired private IGAPagePathReportService gaPagePathReportService;
+
     @Autowired private CommonMapper commonMapper;
 
     private RestTemplate restTemplate;
@@ -193,6 +199,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