소스 검색

GA report

wfansh 6 달 전
부모
커밋
5a023b9f8f

+ 10 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/GAAdminService.java

@@ -21,6 +21,7 @@ import org.springframework.stereotype.Service;
 import java.io.IOException;
 import java.util.Collections;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author wfansh
@@ -112,6 +113,15 @@ public class GAAdminService {
                 gaProperties.add(gaProperty);
             }
 
+            // Distinct by property name.
+            gaProperties =
+                    gaProperties.stream()
+                            .collect(Collectors.groupingBy(GAPropertyDTO::getName))
+                            .values()
+                            .stream()
+                            .map(values -> values.get(0))
+                            .toList();
+
             log.info("listGAProperties for {} : {}", accountName, gson.toJson(gaProperties));
             return gaProperties;
         } catch (IOException e) {

+ 14 - 13
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java

@@ -12,18 +12,18 @@ import com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO;
 
 import jakarta.annotation.PostConstruct;
 
-import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
 
 import java.io.IOException;
 import java.util.List;
 import java.util.Objects;
 
 @Slf4j
-@Data
+@Service
 public class GADataService {
 
     @Value("${spring.application.name}")
@@ -48,7 +48,7 @@ public class GADataService {
                         .build();
     }
 
-    List<Object> runReport(GAReportRequestDTO reportRequest) throws DataException {
+    public List<Object> runReport(GAReportRequestDTO reportRequest) throws DataException {
         try (BetaAnalyticsDataClient analyticsDataClient =
                 BetaAnalyticsDataClient.create(dataSettings)) {
             // 1. Dimensions.
@@ -75,38 +75,39 @@ public class GADataService {
                             .build();
 
             // 4. Order by.
-            OrderBy.Builder orderByBuilder = OrderBy.newBuilder();
-
+            OrderBy.Builder orderBy = null;
             String orderByName = reportRequest.getOrderBy();
             OrderByType orderByType = reportRequest.getOrderByType();
             if (StringUtils.isNotEmpty(orderByName) && Objects.nonNull(orderByType)) {
+                orderBy = OrderBy.newBuilder();
                 switch (orderByType) {
                     case DIMENSIONS ->
-                            orderByBuilder.setDimension(
+                            orderBy.setDimension(
                                     OrderBy.DimensionOrderBy.newBuilder()
                                             .setDimensionName(orderByName));
                     case METRICS ->
-                            orderByBuilder.setMetric(
+                            orderBy.setMetric(
                                     OrderBy.MetricOrderBy.newBuilder().setMetricName(orderByName));
                 }
 
                 // Desc.
-                orderByBuilder.setDesc(reportRequest.isOrderByDesc());
+                orderBy.setDesc(reportRequest.isOrderByDesc());
             }
 
-            RunReportRequest request =
+            RunReportRequest.Builder request =
                     RunReportRequest.newBuilder()
                             .setProperty(reportRequest.getPropertyName())
                             .addAllDimensions(dimensions)
                             .addAllMetrics(metrics)
                             .addDateRanges(dateRange)
-                            .addOrderBys(orderByBuilder)
                             // The API returns a maximum of 250,000 rows per request. See
                             // https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
-                            .setLimit(250_000L)
-                            .build();
+                            .setLimit(250_000L);
+            if (Objects.nonNull(orderBy)) {
+                request.addOrderBys(orderBy);
+            }
 
-            RunReportResponse response = analyticsDataClient.runReport(request);
+            RunReportResponse response = analyticsDataClient.runReport(request.build());
 
             // TODO: Parses the response.
             return null;