Parcourir la source

Source medium report

wfansh il y a 5 mois
Parent
commit
0a61a4c707

+ 20 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/controller/GADataController.java

@@ -9,8 +9,10 @@ import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.adweb.common.util.DateUtil;
 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.jeecg.modules.adweb.dmp.vo.report.CountryStatsVO;
 import org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO;
+import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
 import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.format.annotation.DateTimeFormat;
@@ -34,7 +36,7 @@ import java.util.Map;
 public class GADataController {
 
     @Autowired private IAdwebSiteService adwebSiteService;
-
+    @Autowired private IGASourceMediumReportService gaSourceMediumReportService;
     @Autowired private IGACountryReportService gaCountryReportService;
     @Autowired private IGAPagePathReportService gaPagePathReportService;
 
@@ -56,6 +58,23 @@ public class GADataController {
     //        }
     //    }
 
+    @GetMapping("/source-medium/stats")
+    public Result<List<SourceMediumStatsVO>> getSourceMediumStats(
+            String siteCode,
+            String dateType,
+            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
+            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
+        // 1. 计算时间区间
+        if (StringUtils.isNotBlank(dateType)) {
+            Map<String, Date> dateRange = DateUtil.getDateRangeByType(dateType);
+            start = dateRange.get("start");
+            end = dateRange.get("end");
+        }
+
+        // 2. 查询并返回
+        return Result.ok(gaSourceMediumReportService.getSourceMediumStats(siteCode, start, end));
+    }
+
     @GetMapping("/country/stats")
     public Result<List<CountryStatsVO>> getCountryStats(
             String siteCode,

+ 6 - 5
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/mapper/GASourceMediumReportMapper.java

@@ -1,16 +1,17 @@
 package org.jeecg.modules.adweb.dmp.mapper;
 
-
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 
 import org.jeecg.modules.adweb.dmp.entity.GASourceMediumReport;
+import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
+
+import java.util.Date;
+import java.util.List;
 
 /**
- * @Description: dmp_ga_source_medium_report
- * @Author: jeecg-boot
- * @Date:   2024-10-11
- * @Version: V1.0
+ * @Description: dmp_ga_source_medium_report @Author: jeecg-boot @Date: 2024-10-11 @Version: V1.0
  */
 public interface GASourceMediumReportMapper extends BaseMapper<GASourceMediumReport> {
 
+    List<SourceMediumStatsVO> getSourceMediumStats(String siteCode, Date start, Date end);
 }

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

@@ -2,4 +2,31 @@
 <!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.GASourceMediumReportMapper">
 
+    <select id="getSourceMediumStats" resultType="org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO">
+        SELECT
+        type,
+        SUM(total_users) total_users,
+        SUM(new_users) new_users,
+        SUM(sessions) sessions,
+        ROUND(SUM(page_views_per_session * sessions)) page_views,
+        ROUND(SUM(avg_session_duration * sessions) / SUM(sessions), 2) avg_session_duration,
+        ROUND(SUM(page_views_per_session * sessions) / SUM(sessions), 2) page_views_per_session
+        FROM
+        dmp_ga_source_medium_report
+        WHERE
+        site_code = #{siteCode}
+        AND (type not like '%adwebcloud%' and type not like '%advich%') AND type != ''
+        <if test="start != null">
+            AND date >= #{start}
+        </if>
+        <if test="end != null">
+            AND date &lt; DATE_ADD(#{end}, INTERVAL 1 DAY)
+        </if>
+        GROUP BY
+        type
+        ORDER BY
+        total_users DESC,
+        avg_session_duration DESC
+    </select>
+
 </mapper>

+ 6 - 4
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/IGASourceMediumReportService.java

@@ -3,13 +3,15 @@ package org.jeecg.modules.adweb.dmp.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 
 import org.jeecg.modules.adweb.dmp.entity.GASourceMediumReport;
+import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
+
+import java.util.Date;
+import java.util.List;
 
 /**
- * @Description: dmp_ga_source_medium_report
- * @Author: jeecg-boot
- * @Date:   2024-10-11
- * @Version: V1.0
+ * @Description: dmp_ga_source_medium_report @Author: jeecg-boot @Date: 2024-10-11 @Version: V1.0
  */
 public interface IGASourceMediumReportService extends IService<GASourceMediumReport> {
 
+    List<SourceMediumStatsVO> getSourceMediumStats(String siteCode, Date start, Date end);
 }

+ 42 - 6
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GASourceMediumReportServiceImpl.java

@@ -1,20 +1,56 @@
 package org.jeecg.modules.adweb.dmp.service.impl;
 
-
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
+import org.apache.commons.collections4.CollectionUtils;
+import org.jeecg.modules.adweb.common.util.NumberUtil;
 import org.jeecg.modules.adweb.dmp.entity.GASourceMediumReport;
 import org.jeecg.modules.adweb.dmp.mapper.GASourceMediumReportMapper;
 import org.jeecg.modules.adweb.dmp.service.IGASourceMediumReportService;
+import org.jeecg.modules.adweb.dmp.vo.report.SourceMediumStatsVO;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
 /**
- * @Description: dmp_ga_source_medium_report
- * @Author: jeecg-boot
- * @Date:   2024-10-11
- * @Version: V1.0
+ * @Description: dmp_ga_source_medium_report @Author: jeecg-boot @Date: 2024-10-11 @Version: V1.0
  */
 @Service
-public class GASourceMediumReportServiceImpl extends ServiceImpl<GASourceMediumReportMapper, GASourceMediumReport> implements IGASourceMediumReportService {
+public class GASourceMediumReportServiceImpl
+        extends ServiceImpl<GASourceMediumReportMapper, GASourceMediumReport>
+        implements IGASourceMediumReportService {
+    @Autowired private GASourceMediumReportMapper gaSourceMediumReportMapper;
+
+    @Override
+    public List<SourceMediumStatsVO> getSourceMediumStats(String siteCode, Date start, Date end) {
+        List<SourceMediumStatsVO> sourceMediumStatsVOs =
+                gaSourceMediumReportMapper.getSourceMediumStats(siteCode, start, end);
+        if (CollectionUtils.isEmpty(sourceMediumStatsVOs)) {
+            return Collections.EMPTY_LIST;
+        }
+
+        // 1. 时间区间内所有国家totalUsers总数
+        int totalUsersSum =
+                sourceMediumStatsVOs.stream().mapToInt(SourceMediumStatsVO::getTotalUsers).sum();
+
+        // 2. VO数据填充
+        for (SourceMediumStatsVO sourceMediumStatsVO : sourceMediumStatsVOs) {
+            int totalUsers = sourceMediumStatsVO.getTotalUsers();
+            sourceMediumStatsVO.setTotalUsersProportion(
+                    NumberUtil.formatPercentage(
+                            totalUsersSum == 0 ? 0 : (double) totalUsers / totalUsersSum, 2));
+
+            sourceMediumStatsVO.setNewUsersRatio(
+                    NumberUtil.formatPercentage(
+                            totalUsers == 0
+                                    ? 0
+                                    : (double) sourceMediumStatsVO.getNewUsers() / totalUsers,
+                            2));
+        }
 
+        return sourceMediumStatsVOs;
+    }
 }

+ 35 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/vo/report/SourceMediumStatsVO.java

@@ -0,0 +1,35 @@
+package org.jeecg.modules.adweb.dmp.vo.report;
+
+import lombok.Data;
+
+import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.SourceMediaViewData;
+import org.jeecg.modules.adweb.dmp.entity.GASourceMediumReport;
+
+/**
+ * GA流量来源媒介统计数据VO,see {@link SourceMediaViewData} and {@link GASourceMediumReport}
+ *
+ * @author wfansh
+ */
+@Data
+public class SourceMediumStatsVO {
+
+    private String type;
+
+    private int totalUsers;
+
+    private int newUsers;
+
+    private int sessions;
+
+    private int pageViews;
+
+    private double avgSessionDuration;
+
+    private double pageViewsPerSession;
+
+    // totalUsers / SUM (totalUsers)
+    private String totalUsersProportion;
+
+    // newUsers / totalUsers
+    private String newUsersRatio;
+}