Browse Source

GA report for page path

wfansh 5 tháng trước cách đây
mục cha
commit
764512e46d

+ 34 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/util/NumberUtil.java

@@ -0,0 +1,34 @@
+package org.jeecg.modules.adweb.common.util;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.text.NumberFormat;
+
+/**
+ * @author wfansh
+ */
+public class NumberUtil {
+
+    private static final NumberFormat PERCENTAGE_FORMAT = NumberFormat.getPercentInstance();
+
+    static {
+        PERCENTAGE_FORMAT.setRoundingMode(RoundingMode.HALF_UP);
+        // No grouping with ',' delimiter.
+        PERCENTAGE_FORMAT.setGroupingUsed(false);
+    }
+
+    public static BigDecimal formatDecimal(double number, int fractionDigits) {
+        BigDecimal decimal = new BigDecimal(number);
+        return decimal.setScale(fractionDigits, RoundingMode.HALF_UP);
+    }
+
+    /**
+     * {@link cn.hutool.core.util.NumberUtil#formatPercent(double, int)} 没有设置minimumFractionDigits
+     */
+    public static String formatPercentage(double number, int fractionDigits) {
+        PERCENTAGE_FORMAT.setMinimumFractionDigits(fractionDigits);
+        PERCENTAGE_FORMAT.setMaximumFractionDigits(fractionDigits);
+
+        return PERCENTAGE_FORMAT.format(number);
+    }
+}

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

@@ -0,0 +1,81 @@
+package org.jeecg.modules.adweb.dmp.controller;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.commons.lang.StringUtils;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.modules.adweb.common.util.DateUtil;
+import org.jeecg.modules.adweb.dmp.service.IGAPagePathReportService;
+import org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO;
+import org.jeecg.modules.adweb.site.entity.AdwebSite;
+import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * GA网站流量
+ *
+ * @author wfansh
+ */
+@Tag(name = "Google Analytics网站流量")
+@RestController
+@RequestMapping("/ga-data")
+@Slf4j
+public class GADataController {
+
+    @Autowired private IAdwebSiteService adwebSiteService;
+
+    @Autowired private IGAPagePathReportService gaPagePathReportService;
+
+    //    /** 网站流量分析统计 */
+    //    public Result<?> getSiteTraffic(
+    //            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> map = DateUtil.getDateRangeByType(dateType);
+    //            start = map.get("start");
+    //            end = map.get("end");
+    //        } else {
+    //            if (end != null) {
+    //                end = DateUtil.addDays(end, 1);
+    //            }
+    //        }
+    //    }
+
+    @GetMapping("/page-path/stats")
+    public Result<List<PagePathStatsVO>> getPagePathStats(
+            Integer siteId,
+            String dateType,
+            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
+            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
+        AdwebSite adwebSite = adwebSiteService.getById(siteId);
+        if (Objects.isNull(adwebSite)) {
+            return Result.error("站点未找到" + siteId);
+        }
+
+        // 1. 计算时间区间
+        if (StringUtils.isNotBlank(dateType)) {
+            Map<String, Date> dateRange = DateUtil.getDateRangeByType(dateType);
+            start = dateRange.get("start");
+            end = dateRange.get("end");
+        }
+
+        // 2. 查询并返回
+        return Result.ok(
+                gaPagePathReportService.getPagePathStats(
+                        adwebSite.getCode(), adwebSite.getDomain(), start, end, 10));
+    }
+}

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

@@ -35,7 +35,7 @@ public class GTMController {
 
     @RequestMapping(value = "/add", method = RequestMethod.POST)
     @ResponseBody
-    public Result<Pair<String, String>> addContainer(String siteId) {
+    public Result<Pair<String, String>> addContainer(Integer siteId) {
         AdwebSite adwebSite = adwebSiteService.getById(siteId);
         if (Objects.isNull(adwebSite)) {
             return Result.error("站点未找到" + siteId);

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

@@ -1,16 +1,19 @@
 package org.jeecg.modules.adweb.dmp.mapper;
 
-
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 
 import org.jeecg.modules.adweb.dmp.entity.GAPagePathReport;
+import org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO;
+
+import java.util.Date;
+import java.util.List;
 
 /**
- * @Description: dmp_ga_page_path_report
- * @Author: jeecg-boot
- * @Date:   2024-10-11
- * @Version: V1.0
+ * @Description: dmp_ga_page_path_report @Author: jeecg-boot @Date: 2024-10-11 @Version: V1.0
  */
 public interface GAPagePathReportMapper extends BaseMapper<GAPagePathReport> {
 
+    List<PagePathStatsVO> getPagePathStats(String siteCode, Date start, Date end, int limit);
+
+    int countTotalPVs(String siteCode, Date start, Date end);
 }

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

@@ -2,4 +2,45 @@
 <!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.GAPagePathReportMapper">
 
+    <select id="getPagePathStats" resultType="org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO">
+        SELECT
+        *
+        FROM
+        (
+        SELECT
+        page_path,
+        SUM(page_views) page_views,
+        ROUND(SUM(page_views * avg_time_on_page) / SUM(page_views), 2) avg_time_on_page
+        FROM dmp_ga_page_path_report
+        WHERE
+        site_code = #{siteCode}
+        <if test="start != null">
+            AND date >= #{start}
+        </if>
+        <if test="end != null">
+            AND date &lt; DATE_ADD(#{end}, INTERVAL 1 DAY)
+        </if>
+        GROUP BY page_path
+        ) t
+        ORDER BY
+        page_views DESC,
+        avg_time_on_page DESC
+        LIMIT #{limit}
+    </select>
+
+    <select id="countTotalPVs" resultType="java.lang.Integer">
+        SELECT
+        SUM(page_views) page_views
+        FROM
+        dmp_ga_page_path_report
+        WHERE
+        site_code = #{siteCode}
+        <if test="start != null">
+            AND date >= #{start}
+        </if>
+        <if test="end != null">
+            AND date &lt; DATE_ADD(#{end}, INTERVAL 1 DAY)
+        </if>
+    </select>
+
 </mapper>

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

@@ -3,13 +3,16 @@ package org.jeecg.modules.adweb.dmp.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 
 import org.jeecg.modules.adweb.dmp.entity.GAPagePathReport;
+import org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO;
+
+import java.util.Date;
+import java.util.List;
 
 /**
- * @Description: dmp_ga_page_path_report
- * @Author: jeecg-boot
- * @Date:   2024-10-11
- * @Version: V1.0
+ * @Description: dmp_ga_page_path_report @Author: jeecg-boot @Date: 2024-10-11 @Version: V1.0
  */
 public interface IGAPagePathReportService extends IService<GAPagePathReport> {
 
+    List<PagePathStatsVO> getPagePathStats(
+            String siteCode, String siteUrl, Date start, Date end, int limit);
 }

+ 44 - 5
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/impl/GAPagePathReportServiceImpl.java

@@ -1,19 +1,58 @@
 package org.jeecg.modules.adweb.dmp.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.common.base.Strings;
 
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.jeecg.modules.adweb.common.util.NumberUtil;
 import org.jeecg.modules.adweb.dmp.entity.GAPagePathReport;
 import org.jeecg.modules.adweb.dmp.mapper.GAPagePathReportMapper;
 import org.jeecg.modules.adweb.dmp.service.IGAPagePathReportService;
+import org.jeecg.modules.adweb.dmp.vo.report.PagePathStatsVO;
+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_page_path_report
- * @Author: jeecg-boot
- * @Date:   2024-10-11
- * @Version: V1.0
+ * @Description: dmp_ga_page_path_report @Author: jeecg-boot @Date: 2024-10-11 @Version: V1.0
  */
 @Service
-public class GAPagePathReportServiceImpl extends ServiceImpl<GAPagePathReportMapper, GAPagePathReport> implements IGAPagePathReportService {
+@Slf4j
+public class GAPagePathReportServiceImpl
+        extends ServiceImpl<GAPagePathReportMapper, GAPagePathReport>
+        implements IGAPagePathReportService {
+
+    @Autowired private GAPagePathReportMapper gaPagePathReportMapper;
+
+    public List<PagePathStatsVO> getPagePathStats(
+            String siteCode, String siteUrl, Date start, Date end, int limit) {
+        List<PagePathStatsVO> pagePathStatsVOs =
+                gaPagePathReportMapper.getPagePathStats(siteCode, start, end, limit);
+        if (CollectionUtils.isEmpty(pagePathStatsVOs)) {
+            return Collections.EMPTY_LIST;
+        }
+
+        // 1. 网站URL格式化
+        siteUrl = StringUtils.removeEnd(Strings.nullToEmpty(siteUrl), "/");
+
+        // 2. 时间区间内PV总数
+        int totalPVs = gaPagePathReportMapper.countTotalPVs(siteCode, start, end);
+
+        // 3. VO数据填充
+        for (PagePathStatsVO pagePathStatsVO : pagePathStatsVOs) {
+            pagePathStatsVO.setPagePath(siteUrl + pagePathStatsVO.getPagePath());
+            pagePathStatsVO.setPvProportion(
+                    NumberUtil.formatPercentage(
+                            totalPVs == 0 ? 0 : (double) pagePathStatsVO.getPageViews() / totalPVs,
+                            2));
+        }
 
+        return pagePathStatsVOs;
+    }
 }

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

@@ -0,0 +1,25 @@
+package org.jeecg.modules.adweb.dmp.vo.report;
+
+import lombok.Data;
+
+import org.jeecg.modules.adweb.dmp.dto.google.analytics.report.data.PagePathViewData;
+import org.jeecg.modules.adweb.dmp.entity.GAPagePathReport;
+
+import java.math.BigDecimal;
+
+/**
+ * GA页面访问数据VO,see {@link PagePathViewData} and {@link GAPagePathReport}
+ *
+ * @author wfansh
+ */
+@Data
+public class PagePathStatsVO {
+
+    private String pagePath;
+
+    private int pageViews;
+
+    private BigDecimal avgTimeOnPage;
+
+    private String pvProportion;
+}