|
@@ -8,6 +8,8 @@ import com.google.ads.googleads.v18.services.SearchGoogleAdsRequest;
|
|
|
import com.google.api.core.ApiFuture;
|
|
|
import com.wechi.adweb.bridge.exception.DataException;
|
|
|
import com.wechi.adweb.bridge.google.ads.dto.CustomerStatsDTO;
|
|
|
+import com.wechi.adweb.bridge.google.ads.dto.MetricsDTO;
|
|
|
+import com.wechi.adweb.bridge.google.ads.dto.ReportRequestDTO;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
@@ -15,7 +17,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.stream.Collectors;
|
|
|
import java.util.stream.StreamSupport;
|
|
|
|
|
|
|
|
@@ -24,7 +29,6 @@ import java.util.stream.StreamSupport;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class GoogleAdsService {
|
|
|
-
|
|
|
@Autowired private GoogleAdsClient googleAdsClient;
|
|
|
|
|
|
public CustomerStatsDTO getCustomerStats(String customerId) throws DataException {
|
|
@@ -87,4 +91,38 @@ public class GoogleAdsService {
|
|
|
throw new DataException(e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public Map<String, MetricsDTO> getDailyStats(ReportRequestDTO reportRequest)
|
|
|
+ throws DataException {
|
|
|
+ try (GoogleAdsServiceClient googleAdsServiceClient =
|
|
|
+ googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
|
|
|
+ String query =
|
|
|
+ String.format(
|
|
|
+ "SELECT customer.descriptive_name, customer.currency_code, "
|
|
|
+ + "segments.date, "
|
|
|
+ + "metrics.impressions, metrics.clicks, metrics.ctr, metrics.average_cpc, "
|
|
|
+ + "metrics.average_cpm, metrics.conversions, metrics.cost_micros "
|
|
|
+ + "FROM customer WHERE %s "
|
|
|
+ + "ORDER BY segments.date LIMIT %d",
|
|
|
+ reportRequest.toDateClause(), reportRequest.getLimit());
|
|
|
+
|
|
|
+ SearchGoogleAdsRequest request =
|
|
|
+ SearchGoogleAdsRequest.newBuilder()
|
|
|
+ .setCustomerId(reportRequest.getCustomerId())
|
|
|
+ .setQuery(query)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ SearchPagedResponse response = googleAdsServiceClient.search(request);
|
|
|
+ return StreamSupport.stream(response.iterateAll().spliterator(), false)
|
|
|
+ .collect(
|
|
|
+ Collectors.toMap(
|
|
|
+ googleAdsRow -> googleAdsRow.getSegments().getDate(),
|
|
|
+ googleAdsRow ->
|
|
|
+ MetricsDTO.fromMetrics(googleAdsRow.getMetrics()),
|
|
|
+
|
|
|
+ MetricsDTO::merge,
|
|
|
+
|
|
|
+ LinkedHashMap::new));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|