Browse Source

Run GA report

wfansh 6 tháng trước cách đây
mục cha
commit
8ec1d9fb67

+ 53 - 9
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java

@@ -1,13 +1,11 @@
 package com.wechi.adweb.bridge.google.analytics;
 
-import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
-import com.google.analytics.data.v1beta.BetaAnalyticsDataSettings;
-import com.google.analytics.data.v1beta.Dimension;
-import com.google.analytics.data.v1beta.Metric;
+import static com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO.*;
+
+import com.google.analytics.data.v1beta.*;
 import com.google.api.gax.core.FixedCredentialsProvider;
 import com.google.auth.oauth2.GoogleCredentials;
 import com.google.common.base.Splitter;
-import com.google.common.base.Strings;
 import com.google.gson.Gson;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO;
@@ -17,8 +15,11 @@ import jakarta.annotation.PostConstruct;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 
+import org.apache.commons.lang3.StringUtils;
+
 import java.io.IOException;
 import java.util.List;
+import java.util.Objects;
 
 @Slf4j
 @Data
@@ -48,22 +49,65 @@ public class GADataService {
             // 1. Dimensions.
             List<Dimension> dimensions =
                     Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
-                            .splitToStream(Strings.nullToEmpty(reportRequest.getDimensions()))
+                            .splitToStream(StringUtils.defaultString(reportRequest.getDimensions()))
+                            .filter(StringUtils::isNotEmpty)
                             .map(dimension -> Dimension.newBuilder().setName(dimension).build())
                             .toList();
 
             // 2. Metrics.
             List<Metric> metrics =
                     Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
-                            .splitToStream(Strings.nullToEmpty(reportRequest.getMetrics()))
+                            .splitToStream(StringUtils.defaultString(reportRequest.getMetrics()))
+                            .filter(StringUtils::isNotEmpty)
                             .map(metric -> Metric.newBuilder().setName(metric).build())
                             .toList();
 
+            // 3. Date range.
+            DateRange dateRange =
+                    DateRange.newBuilder()
+                            .setStartDate(reportRequest.getStartDate())
+                            .setEndDate(reportRequest.getEndDate())
+                            .build();
+
+            // 4. Order by.
+            OrderBy.Builder orderByBuilder = OrderBy.newBuilder();
+
+            String orderByName = reportRequest.getOrderBy();
+            OrderByType orderByType = reportRequest.getOrderByType();
+            if (StringUtils.isNotEmpty(orderByName) && Objects.nonNull(orderByType)) {
+                switch (orderByType) {
+                    case DIMENSIONS ->
+                            orderByBuilder.setDimension(
+                                    OrderBy.DimensionOrderBy.newBuilder()
+                                            .setDimensionName(orderByName));
+                    case METRICS ->
+                            orderByBuilder.setMetric(
+                                    OrderBy.MetricOrderBy.newBuilder().setMetricName(orderByName));
+                }
+
+                // Desc.
+                orderByBuilder.setDesc(reportRequest.isOrderByDesc());
+            }
+
+            RunReportRequest 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();
+
+            RunReportResponse response = analyticsDataClient.runReport(request);
+
+            // TODO: Parses the response.
+            return null;
         } catch (IOException e) {
             log.error(e.getMessage());
             throw new DataException(e);
         }
-
-        return null;
     }
 }

+ 9 - 2
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GAReportRequestDTO.java

@@ -19,9 +19,16 @@ public class GAReportRequestDTO {
 
     private String dimensions;
 
-    private String sorts;
+    private String orderBy;
 
-    private String sortType;
+    private OrderByType orderByType;
+
+    private boolean orderByDesc;
 
     private String type;
+
+    public enum OrderByType {
+        METRICS,
+        DIMENSIONS
+    }
 }

+ 22 - 3
src/test/java/com/wechi/adweb/bridge/DataBridgeApplicationTests.java

@@ -1,13 +1,32 @@
 package com.wechi.adweb.bridge;
 
+import com.google.analytics.data.v1beta.Dimension;
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import com.wechi.adweb.bridge.google.analytics.GAAdminService;
+
+import com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.util.List;
+import java.util.Optional;
+
 @SpringBootTest
 class DataBridgeApplicationTests {
 
-	@Test
-	void contextLoads() {
-	}
+    @Autowired GAAdminService gaAdminService;
+
+    @Test
+    void contextLoads() throws Exception {
 
+//        List<Dimension> dimensions =
+//                Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
+//                        .splitToStream(Strings.nullToEmpty("aaa,bbb"))
+//                        .map(dimension -> Dimension.newBuilder().setName(dimension).build())
+//                        .toList();
+//
+//        System.out.println(dimensions);
+    }
 }