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