wfansh 6 miesięcy temu
rodzic
commit
c8375b61af

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

@@ -0,0 +1,69 @@
+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 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;
+
+import jakarta.annotation.PostConstruct;
+
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.List;
+
+@Slf4j
+@Data
+public class GADataService {
+
+    private BetaAnalyticsDataSettings dataSettings;
+
+    private Gson gson = new Gson();
+
+    @PostConstruct
+    private void init() throws IOException {
+        GoogleCredentials credentials =
+                GoogleCredentials.fromStream(
+                        this.getClass()
+                                .getClassLoader()
+                                .getResourceAsStream("google/service-account-key.json"));
+
+        this.dataSettings =
+                BetaAnalyticsDataSettings.newBuilder()
+                        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
+                        .build();
+    }
+
+    List<Object> runReport(GAReportRequestDTO reportRequest) throws DataException {
+        try (BetaAnalyticsDataClient analyticsDataClient =
+                BetaAnalyticsDataClient.create(dataSettings)) {
+            // 1. Dimensions.
+            List<Dimension> dimensions =
+                    Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
+                            .splitToStream(Strings.nullToEmpty(reportRequest.getDimensions()))
+                            .map(dimension -> Dimension.newBuilder().setName(dimension).build())
+                            .toList();
+
+            // 2. Metrics.
+            List<Metric> metrics =
+                    Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
+                            .splitToStream(Strings.nullToEmpty(reportRequest.getMetrics()))
+                            .map(metric -> Metric.newBuilder().setName(metric).build())
+                            .toList();
+
+        } catch (IOException e) {
+            log.error(e.getMessage());
+            throw new DataException(e);
+        }
+
+        return null;
+    }
+}

+ 27 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GAReportRequestDTO.java

@@ -0,0 +1,27 @@
+package com.wechi.adweb.bridge.google.analytics.dto;
+
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+public class GAReportRequestDTO {
+    public static final char DEFAULT_SPLITTER = ',';
+
+    private String propertyName;
+
+    private String startDate;
+
+    private String endDate;
+
+    private String metrics;
+
+    private String dimensions;
+
+    private String sorts;
+
+    private String sortType;
+
+    private String type;
+}