Ver Fonte

Campaign stats

wfansh há 3 meses atrás
pai
commit
37370c2421

+ 26 - 0
src/main/java/com/wechi/adweb/bridge/google/ads/controller/GoogleAdsController.java

@@ -6,6 +6,7 @@ import com.wechi.adweb.bridge.common.OpenAPIRequest;
 import com.wechi.adweb.bridge.common.OpenAPIResponse;
 import com.wechi.adweb.bridge.exception.BadRequestException;
 import com.wechi.adweb.bridge.exception.DataException;
+import com.wechi.adweb.bridge.google.ads.dto.CampaignStatsDTO;
 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;
@@ -20,6 +21,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -80,6 +82,30 @@ public class GoogleAdsController extends BaseController {
                 .build();
     }
 
+    @PostMapping("/campaignStats")
+    @ResponseBody
+    public OpenAPIResponse<List<CampaignStatsDTO>> getCampaignStats(
+            @RequestBody OpenAPIRequest<ReportRequestDTO> apiRequest)
+            throws BadRequestException, DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** getCampaignStats() ****** apiRequest = {}", JsonUtils.toJson(apiRequest));
+        ReportRequestDTO reportRequest = apiRequest.getData();
+
+        // 1. Validates the request parameters.
+        if (!validateReportRequest(reportRequest)) {
+            throw new BadRequestException(apiRequest);
+        }
+
+        // 2. Executes the API request.
+        List<CampaignStatsDTO> campaignStats = googleAdsService.getCampaignStats(reportRequest);
+        log.info(
+                "****** getCampaignStats() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<List<CampaignStatsDTO>>builder()
+                .status(APIStatus.SUCCESS)
+                .data(campaignStats)
+                .build();
+    }
+
     @PostMapping("/keywordStats")
     @ResponseBody
     public OpenAPIResponse<Map<String, MetricsDTO>> getKeywordStats(

+ 47 - 0
src/main/java/com/wechi/adweb/bridge/google/ads/dto/CampaignStatsDTO.java

@@ -0,0 +1,47 @@
+package com.wechi.adweb.bridge.google.ads.dto;
+
+import com.google.ads.googleads.v18.common.Metrics;
+import com.google.ads.googleads.v18.resources.Campaign;
+import com.google.ads.googleads.v18.resources.CampaignBudget;
+import com.google.ads.googleads.v18.services.GoogleAdsRow;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class CampaignStatsDTO {
+
+    private String resourceName;
+    private String name;
+
+    private String status;
+    private String primaryStatus;
+    private String advertisingChannelType;
+    private String advertisingChannelSubType;
+    private String biddingStrategyType;
+
+    private long budgetMicros;
+
+    private MetricsDTO metrics;
+
+    public static CampaignStatsDTO fromGoogleAdsRow(GoogleAdsRow googleAdsRow) {
+        Campaign campaign = googleAdsRow.getCampaign();
+        CampaignBudget campaignBudget = googleAdsRow.getCampaignBudget();
+        Metrics metrics = googleAdsRow.getMetrics();
+        return CampaignStatsDTO.builder()
+                .resourceName(campaign.getResourceName())
+                .name(campaign.getName())
+                .status(campaign.getStatus().name())
+                .primaryStatus(campaign.getPrimaryStatus().name())
+                .advertisingChannelType(campaign.getAdvertisingChannelType().name())
+                .advertisingChannelSubType(campaign.getAdvertisingChannelSubType().name())
+                .biddingStrategyType(campaign.getBiddingStrategyType().name())
+                .budgetMicros(campaignBudget.getAmountMicros())
+                .metrics(MetricsDTO.fromMetrics(metrics))
+                .build();
+    }
+}

+ 30 - 0
src/main/java/com/wechi/adweb/bridge/google/ads/service/GoogleAdsService.java

@@ -9,6 +9,7 @@ import com.google.api.core.ApiFuture;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.ads.common.CountryCode;
 import com.wechi.adweb.bridge.google.ads.common.PlacementUtils;
+import com.wechi.adweb.bridge.google.ads.dto.CampaignStatsDTO;
 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;
@@ -20,6 +21,7 @@ import org.springframework.stereotype.Service;
 
 import java.time.LocalDate;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
@@ -128,6 +130,34 @@ public class GoogleAdsService {
         }
     }
 
+    public List<CampaignStatsDTO> getCampaignStats(ReportRequestDTO reportRequest)
+            throws DataException {
+        try (GoogleAdsServiceClient googleAdsServiceClient =
+                googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
+            String query =
+                    String.format(
+                            "SELECT campaign.name, campaign.status, campaign.primary_status, campaign.bidding_strategy_type, "
+                                    + "campaign.advertising_channel_type, campaign.advertising_channel_sub_type, campaign_budget.amount_micros, "
+                                    + "metrics.impressions, metrics.clicks, metrics.ctr, metrics.average_cpc, "
+                                    + "metrics.average_cpm, metrics.conversions, metrics.cost_micros "
+                                    + "FROM campaign "
+                                    + "WHERE campaign.status != 'REMOVED' AND %s "
+                                    + "ORDER BY metrics.impressions DESC, metrics.clicks DESC 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)
+                    .map(CampaignStatsDTO::fromGoogleAdsRow)
+                    .toList();
+        }
+    }
+
     public Map<String, MetricsDTO> getKeywordStats(ReportRequestDTO reportRequest)
             throws DataException {
         try (GoogleAdsServiceClient googleAdsServiceClient =