Browse Source

Merge branch 'device' of wangfan/adweb3-server into master

wangfan 3 months ago
parent
commit
79f8fd1352

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

@@ -1,7 +1,5 @@
 package org.jeecg.modules.adweb.dmp.service.google;
 
-import static org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportConstant.*;
-
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -60,6 +58,8 @@ public class GAReportService {
             AnnotationUtils.findAnnotation(GASourceMediumReport.class, TableName.class).value();
     private static final String TABLE_GA_PAGE_PATH_REPORT =
             AnnotationUtils.findAnnotation(GAPagePathReport.class, TableName.class).value();
+    private static final String TABLE_GA_DEVICE_REPORT =
+            AnnotationUtils.findAnnotation(GADeviceReport.class, TableName.class).value();
 
     private static final String GA_REPORT_API_PATH = "/api/google/ga/report";
 
@@ -75,6 +75,7 @@ public class GAReportService {
     @Autowired private IGACountryReportService gaCountryReportService;
     @Autowired private IGASourceMediumReportService gaSourceMediumReportService;
     @Autowired private IGAPagePathReportService gaPagePathReportService;
+    @Autowired private IGADeviceReportService gaDeviceReportService;
 
     @Autowired private CommonMapper commonMapper;
 
@@ -100,12 +101,13 @@ public class GAReportService {
                         new LambdaQueryWrapper<GoogleGTM>().in(GoogleGTM::getSiteCode, siteCodes));
 
         for (GoogleGTM googleGTM : googleGTMs) {
-            // 每个帐号同步更新张报表
+            // 每个帐号同步更新张报表
             try {
                 this.syncGADailyReport(googleGTM);
                 this.syncGACountryReport(googleGTM);
                 this.syncGASourceMediumReport(googleGTM);
                 this.syncGAPagePathReport(googleGTM);
+                this.syncGADeviceReport(googleGTM);
             } catch (RuntimeException e) {
                 log.warn("同步GA报表异常, siteCode = {], error = {}", googleGTM.getSiteCode(), e);
             }
@@ -163,7 +165,8 @@ public class GAReportService {
                     Double.parseDouble(reportData.get(ReportConstant.METRIC_BOUNCE_RATE)));
             reportRow.setAvgSessionDuration(
                     Double.parseDouble(reportData.get(ReportConstant.METRIC_AVG_SESSION_DURATION)));
-            reportRow.setPageViews(Integer.parseInt(reportData.get(METRIC_SCREEN_PAGE_VIEWS)));
+            reportRow.setPageViews(
+                    Integer.parseInt(reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS)));
             reportRow.setPageViewsPerSession(
                     Double.parseDouble(
                             reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION)));
@@ -339,7 +342,7 @@ public class GAReportService {
                     DateUtils.str2Date(
                             reportData.get(ReportConstant.DIMENSION_DATE),
                             DateUtils.date_sdf.get()));
-            reportRow.setPagePath(reportData.get(DIMENSION_PAGE_PATH));
+            reportRow.setPagePath(reportData.get(ReportConstant.DIMENSION_PAGE_PATH));
             reportRow.setEngagementRate(
                     Double.parseDouble(reportData.get(ReportConstant.METRIC_ENGAGEMENT_RATE)));
             reportRow.setPageViews(
@@ -360,6 +363,75 @@ public class GAReportService {
     }
 
     /**
+     * 拉取并同步Google Analytics - Device报表
+     *
+     * @param googleGTM
+     */
+    private void syncGADeviceReport(GoogleGTM googleGTM) {
+        // 1. 报表时间区间
+        Date startDate = this.getReportStartDate(TABLE_GA_DEVICE_REPORT, googleGTM.getSiteCode());
+        Date endDate = new Date();
+
+        // 2. 构建GA报表请求参数
+        // 使用ADWEB_CUSTOM_REPORT
+        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_TOTAL_USERS,
+                        ReportConstant.METRIC_NEW_USERS,
+                        ReportConstant.METRIC_SESSIONS,
+                        ReportConstant.METRIC_AVG_SESSION_DURATION,
+                        ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
+                        ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION));
+        gaReportRequest.setDimensions(
+                List.of(
+                        ReportConstant.DIMENSION_DATE,
+                        ReportConstant.DIMENSION_PLATFORM_DEVICE_CATEGORY));
+        gaReportRequest.setOrderByType(OrderByType.DIMENSIONS);
+        gaReportRequest.setOrderBy(ReportConstant.DIMENSION_DATE);
+        List<CustomReportData> reportDataList =
+                runGAReport(gaReportRequest, CustomReportData.class);
+
+        // 2. 转化为DB entity
+        List<GADeviceReport> deviceReport = Lists.newArrayList();
+        for (CustomReportData reportData : reportDataList) {
+            GADeviceReport reportRow = new GADeviceReport();
+            reportRow.setSiteCode(googleGTM.getSiteCode());
+            reportRow.setDate(
+                    DateUtils.str2Date(
+                            reportData.get(ReportConstant.DIMENSION_DATE),
+                            DateUtils.date_sdf.get()));
+            reportRow.setDevice(reportData.get(ReportConstant.DIMENSION_PLATFORM_DEVICE_CATEGORY));
+            reportRow.setTotalUsers(
+                    Integer.parseInt(reportData.get(ReportConstant.METRIC_TOTAL_USERS)));
+            reportRow.setNewUsers(
+                    Integer.parseInt(reportData.get(ReportConstant.METRIC_NEW_USERS)));
+            reportRow.setSessions(Integer.parseInt(reportData.get(ReportConstant.METRIC_SESSIONS)));
+            reportRow.setAvgSessionDuration(
+                    Double.parseDouble(reportData.get(ReportConstant.METRIC_AVG_SESSION_DURATION)));
+            reportRow.setPageViews(
+                    Integer.parseInt(reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS)));
+            reportRow.setPageViewsPerSession(
+                    Double.parseDouble(
+                            reportData.get(ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION)));
+
+            deviceReport.add(reportRow);
+        }
+
+        // 3. 更新数据库 - 删除旧数据,插入新数据
+        gaDeviceReportService.remove(
+                this.getRemoveQueryWrapper(
+                        GADeviceReport.class, googleGTM.getSiteCode(), startDate, endDate));
+
+        gaDeviceReportService.saveBatch(deviceReport, deviceReport.size());
+    }
+
+    /**
      * 请求Google Analytics报表,http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
      *
      * @param gaReportRequest