소스 검색

Path pageh view report

wfansh 6 달 전
부모
커밋
69624c66f7

+ 2 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java

@@ -12,6 +12,7 @@ import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.ReportType;
 import com.wechi.adweb.bridge.google.analytics.dto.report.row.CountryChartData;
 import com.wechi.adweb.bridge.google.analytics.dto.report.row.GAReportDataDTO;
+import com.wechi.adweb.bridge.google.analytics.dto.report.row.PagePathViewData;
 import com.wechi.adweb.bridge.google.analytics.dto.report.row.SourceMediaViewData;
 
 import jakarta.annotation.PostConstruct;
@@ -161,6 +162,7 @@ public class GADataService {
             case ADWEB_COUNTRY_CHART -> CountryChartData.fromReportRows(reportRows, reportRequest);
             case ADWEB_SOURCE_MEDIUM_VIEW ->
                     SourceMediaViewData.fromReportRows(reportRows, reportRequest);
+            case ADWEB_PATH_PATH_VIEW -> PagePathViewData.fromReportRows(reportRows, reportRequest);
             default -> Collections.emptyList();
         };
     }

+ 4 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/ReportConstant.java

@@ -11,8 +11,12 @@ public class ReportConstant {
     public static final String METRIC_BOUNCE_RATE = "bounceRate";
     public static final String METRIC_AVG_SESSION_DURATION = "averageSessionDuration";
     public static final String METRIC_SCREEN_PAGE_VIEWS_PER_SESSION = "screenPageViewsPerSession";
+    public static final String METRIC_ENGAGEMENT_RATE = "engagementRate";
+    public static final String METRIC_SCREEN_PAGE_VIEWS = "screenPageViews";
+    public static final String METRIC_USER_ENGAGEMENT_DURATION = "userEngagementDuration";
 
     public static final String DIMENSION_COUNTRY = "country";
     public static final String DIMENSION_DATE = "date";
     public static final String DIMENSION_SESSION_SOURCE_MEDIUM = "sessionSourceMedium";
+    public static final String DIMENSION_PAGE_PATH = "pagePath";
 }

+ 11 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/ReportType.java

@@ -1,5 +1,6 @@
 package com.wechi.adweb.bridge.google.analytics.dto.report;
 
+import static com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO.OrderByType.*;
 import static com.wechi.adweb.bridge.google.analytics.dto.report.ReportConstant.*;
 
 import lombok.Getter;
@@ -20,7 +21,16 @@ public enum ReportType {
                     METRIC_BOUNCE_RATE,
                     METRIC_AVG_SESSION_DURATION,
                     METRIC_SCREEN_PAGE_VIEWS_PER_SESSION),
-            List.of(DIMENSION_SESSION_SOURCE_MEDIUM));
+            List.of(DIMENSION_SESSION_SOURCE_MEDIUM)),
+    ADWEB_PATH_PATH_VIEW(
+            List.of(
+                    METRIC_ENGAGEMENT_RATE,
+                    METRIC_SCREEN_PAGE_VIEWS,
+                    METRIC_USER_ENGAGEMENT_DURATION),
+            List.of(DIMENSION_PAGE_PATH),
+            METRIC_SCREEN_PAGE_VIEWS,
+            METRICS,
+            true);
 
     private ReportType(List<String> defaultMetrics, List<String> defaultDimensions) {
         this.defaultMetrics = defaultMetrics;

+ 64 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/PagePathViewData.java

@@ -0,0 +1,64 @@
+package com.wechi.adweb.bridge.google.analytics.dto.report.row;
+
+import static com.wechi.adweb.bridge.google.analytics.dto.report.ReportConstant.*;
+
+import com.google.analytics.data.v1beta.Row;
+import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
+import com.wechi.adweb.bridge.util.NumberUtils;
+
+import lombok.Builder;
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class PagePathViewData implements GAReportDataDTO {
+
+    private String pagePath;
+
+    private String engagementRate;
+
+    private int pageViews;
+
+    private BigDecimal avgTimeOnPage;
+
+    public static List<PagePathViewData> fromReportRows(
+            List<Row> reportRows, GAReportRequestDTO reportRequest) {
+        int indexPagePath = reportRequest.getDimensions().indexOf(DIMENSION_PAGE_PATH);
+        int indexEngagementRate = reportRequest.getMetrics().indexOf(METRIC_ENGAGEMENT_RATE);
+        int indexPageViews = reportRequest.getMetrics().indexOf(METRIC_SCREEN_PAGE_VIEWS);
+        int indexUserEngagementDuration =
+                reportRequest.getMetrics().indexOf(METRIC_USER_ENGAGEMENT_DURATION);
+
+        return reportRows.stream()
+                .map(
+                        row ->
+                                PagePathViewData.builder()
+                                        .pagePath(row.getDimensionValues(indexPagePath).getValue())
+                                        .engagementRate(
+                                                NumberUtils.formatPercentage(
+                                                        Double.parseDouble(
+                                                                row.getMetricValues(
+                                                                                indexEngagementRate)
+                                                                        .getValue()),
+                                                        2))
+                                        .pageViews(
+                                                Integer.parseInt(
+                                                        row.getMetricValues(indexPageViews)
+                                                                .getValue()))
+                                        .avgTimeOnPage(
+                                                NumberUtils.formatDecimal(
+                                                        Double.parseDouble(
+                                                                row.getMetricValues(
+                                                                                indexUserEngagementDuration)
+                                                                        .getValue()),
+                                                        2))
+                                        .build())
+                .toList();
+    }
+}

+ 3 - 2
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/SourceMediaViewData.java

@@ -9,6 +9,7 @@ import com.wechi.adweb.bridge.util.NumberUtils;
 import lombok.Builder;
 import lombok.Data;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 /**
@@ -27,9 +28,9 @@ public class SourceMediaViewData implements GAReportDataDTO {
 
     private String bounceRate;
 
-    private String avgSessionDuration;
+    private BigDecimal avgSessionDuration;
 
-    private String pageViewsPerSession;
+    private BigDecimal pageViewsPerSession;
 
     public static List<SourceMediaViewData> fromReportRows(
             List<Row> reportRows, GAReportRequestDTO reportRequest) {

+ 5 - 11
src/main/java/com/wechi/adweb/bridge/util/NumberUtils.java

@@ -1,5 +1,6 @@
 package com.wechi.adweb.bridge.util;
 
+import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.NumberFormat;
 
@@ -8,24 +9,17 @@ import java.text.NumberFormat;
  */
 public class NumberUtils {
 
-    private static final NumberFormat DECIMAL_FORMAT = NumberFormat.getNumberInstance();
-
     private static final NumberFormat PERCENTAGE_FORMAT = NumberFormat.getPercentInstance();
 
     static {
-        DECIMAL_FORMAT.setRoundingMode(RoundingMode.HALF_UP);
-        // No grouping with ',' delimiter.
-        DECIMAL_FORMAT.setGroupingUsed(false);
-
         PERCENTAGE_FORMAT.setRoundingMode(RoundingMode.HALF_UP);
+        // No grouping with ',' delimiter.
         PERCENTAGE_FORMAT.setGroupingUsed(false);
     }
 
-    public static String formatDecimal(double number, int fractionDigits) {
-        DECIMAL_FORMAT.setMinimumFractionDigits(fractionDigits);
-        DECIMAL_FORMAT.setMaximumFractionDigits(fractionDigits);
-
-        return DECIMAL_FORMAT.format(number);
+    public static BigDecimal formatDecimal(double number, int fractionDigits) {
+        BigDecimal decimal = new BigDecimal(number);
+        return decimal.setScale(fractionDigits, RoundingMode.HALF_UP);
     }
 
     public static String formatPercentage(double number, int fractionDigits) {