|
@@ -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;
|
|
|
}
|
|
|
}
|