Browse Source

GA country report

wfansh 5 months ago
parent
commit
ed8aec70c5

+ 56 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/entity/GACountryReport.java

@@ -0,0 +1,56 @@
+package org.jeecg.modules.adweb.dmp.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+import org.jeecgframework.poi.excel.annotation.Excel;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @Description: dmp_ga_country_report
+ * @Author: jeecg-boot
+ * @Date:   2024-10-11
+ * @Version: V1.0
+ */
+@Data
+@TableName("dmp_ga_country_report")
+@Accessors(chain = true)
+@EqualsAndHashCode(callSuper = false)
+@Schema(description="dmp_ga_country_report")
+public class GACountryReport implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+	/**id*/
+	@TableId(type = IdType.AUTO)
+    @Schema(description = "id")
+    private Integer id;
+	/**站点id*/
+	@Excel(name = "站点id", width = 15)
+    @Schema(description = "站点id")
+    private Integer siteId;
+	/**统计时间*/
+	@Excel(name = "统计时间", width = 15, format = "yyyy-MM-dd")
+	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
+    @DateTimeFormat(pattern="yyyy-MM-dd")
+    @Schema(description = "统计时间")
+    private Date date;
+	/**国家*/
+	@Excel(name = "国家", width = 15)
+    @Schema(description = "国家")
+    private String country;
+	/**访问量*/
+	@Excel(name = "访问量", width = 15)
+    @Schema(description = "访问量")
+    private Integer totalUsers;
+}

+ 1 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/entity/GoogleGTM.java

@@ -32,7 +32,7 @@ public class GoogleGTM implements Serializable {
     private static final long serialVersionUID = 1L;
 
 	/**id*/
-	@TableId(type = IdType.ASSIGN_ID)
+	@TableId(type = IdType.AUTO)
     @Schema(description = "id")
     private Integer id;
 	/**siteId*/

+ 16 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/mapper/GACountryReportMapper.java

@@ -0,0 +1,16 @@
+package org.jeecg.modules.adweb.dmp.mapper;
+
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import org.jeecg.modules.adweb.dmp.entity.GACountryReport;
+
+/**
+ * @Description: dmp_ga_country_report
+ * @Author: jeecg-boot
+ * @Date:   2024-10-11
+ * @Version: V1.0
+ */
+public interface GACountryReportMapper extends BaseMapper<GACountryReport> {
+
+}

+ 5 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/mapper/xml/GACountryReportMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="org.jeecg.modules.adweb.dmp.mapper.GACountryReportMapper">
+
+</mapper>

+ 14 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/IGACountryReportService.java

@@ -0,0 +1,14 @@
+package org.jeecg.modules.adweb.dmp.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import org.jeecg.modules.adweb.dmp.entity.GACountryReport;
+
+/**
+ * @Description: dmp_ga_country_report
+ * @Author: jeecg-boot
+ * @Date:   2024-10-11
+ * @Version: V1.0
+ */
+public interface IGACountryReportService extends IService<GACountryReport> {
+
+}

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

@@ -6,13 +6,24 @@ import jakarta.annotation.PostConstruct;
 
 import lombok.extern.slf4j.Slf4j;
 
+import org.apache.commons.compress.utils.Lists;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.reflect.TypeUtils;
+import org.jeecg.common.util.DateUtils;
 import org.jeecg.common.util.FastJsonUtil;
 import org.jeecg.modules.adweb.common.util.RestTemplateUtil;
 import org.jeecg.modules.adweb.dmp.dto.OpenAPIRequest;
 import org.jeecg.modules.adweb.dmp.dto.OpenAPIResponse;
 import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.GAReportRequestDTO;
+import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.OrderByType;
+import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportConstant;
+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.GoogleGTM;
+import org.jeecg.modules.adweb.dmp.service.IGACountryReportService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.ParameterizedTypeReference;
 import org.springframework.stereotype.Service;
@@ -37,6 +48,8 @@ public class GAReportService {
     @Value("${data-bridge.api.token}")
     private String dataBridgeApiToken;
 
+    @Autowired private IGACountryReportService gaCountryReportService;
+
     private RestTemplate restTemplate;
 
     @PostConstruct
@@ -44,13 +57,48 @@ public class GAReportService {
         this.restTemplate = RestTemplateUtil.getRestTemplate(60, 60, dataBridgeApiToken);
     }
 
-    //    /**
-    //     * 拉取Google Analytics国家报表
-    //     *
-    //     * @param googleGTM
-    //     */
-    //    private void queryGARegionReport(GoogleGTM googleGTM) {}
-    //
+    /**
+     * 拉取Google Analytics 国家按日报表
+     *
+     * @param googleGTM
+     */
+    void queryGACountryDailyReport(GoogleGTM googleGTM) {
+        // 1. 构建报表请求参数
+        // 使用ADWEB_CUSTOM_REPORT而不是ADWEB_COUNTRY_CHART,因为需要date作为dimension
+        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
+        gaReportRequest.setPropertyResourceName(
+                toGAPropertyResourceName(googleGTM.getGaPropertyId()));
+        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
+        gaReportRequest.setStartDate("2024-01-01");
+        gaReportRequest.setEndDate("2024-10-11");
+        gaReportRequest.setMetrics(List.of(ReportConstant.METRIC_TOTAL_USERS));
+        gaReportRequest.setDimensions(
+                List.of(ReportConstant.DIMENSION_DATE, ReportConstant.DIMENSION_COUNTRY));
+        gaReportRequest.setOrderByType(OrderByType.DIMENSIONS);
+        gaReportRequest.setOrderBy(ReportConstant.DIMENSION_DATE);
+        List<CustomReportData> reportDataList =
+                runGAReport(gaReportRequest, CustomReportData.class);
+
+        // 2. 转化为DB entity
+        List<GACountryReport> countryReport = Lists.newArrayList();
+        for (CustomReportData reportData : reportDataList) {
+            GACountryReport reportRow = new GACountryReport();
+            reportRow.setSiteId(googleGTM.getSiteId());
+            reportRow.setDate(
+                    DateUtils.str2Date(
+                            reportData.get(ReportConstant.DIMENSION_DATE),
+                            DateUtils.date_sdf.get()));
+            reportRow.setCountry(reportData.get(ReportConstant.DIMENSION_COUNTRY));
+            reportRow.setTotalUsers(
+                    Integer.parseInt(reportData.get(ReportConstant.METRIC_TOTAL_USERS)));
+
+            countryReport.add(reportRow);
+        }
+
+        // 3. 保存数据库
+        // TODO - 重复数据
+        gaCountryReportService.saveOrUpdateBatch(countryReport);
+    }
 
     /**
      * 请求Google Analytics报表,http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
@@ -88,4 +136,8 @@ public class GAReportService {
                 FastJsonUtil.toJSONString(openAPIResponse.getData()));
         return openAPIResponse.getData();
     }
+
+    private final String toGAPropertyResourceName(String gaPropertyId) {
+        return StringUtils.isNumeric(gaPropertyId) ? "properties/" + gaPropertyId : gaPropertyId;
+    }
 }

+ 19 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GACountryReportServiceImpl.java

@@ -0,0 +1,19 @@
+package org.jeecg.modules.adweb.dmp.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+
+import org.jeecg.modules.adweb.dmp.entity.GACountryReport;
+import org.jeecg.modules.adweb.dmp.mapper.GACountryReportMapper;
+import org.jeecg.modules.adweb.dmp.service.IGACountryReportService;
+import org.springframework.stereotype.Service;
+
+/**
+ * @Description: dmp_ga_country_report
+ * @Author: jeecg-boot
+ * @Date:   2024-10-11
+ * @Version: V1.0
+ */
+@Service
+public class GACountryReportServiceImpl extends ServiceImpl<GACountryReportMapper, GACountryReport> implements IGACountryReportService {
+
+}