|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.ads.googleads.examples.planning; |
| 16 | + |
| 17 | +import com.beust.jcommander.Parameter; |
| 18 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 19 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 21 | +import com.google.ads.googleads.v3.common.BidModifierSimulationPoint; |
| 22 | +import com.google.ads.googleads.v3.errors.GoogleAdsError; |
| 23 | +import com.google.ads.googleads.v3.errors.GoogleAdsException; |
| 24 | +import com.google.ads.googleads.v3.resources.CampaignCriterionSimulation; |
| 25 | +import com.google.ads.googleads.v3.services.GoogleAdsRow; |
| 26 | +import com.google.ads.googleads.v3.services.GoogleAdsServiceClient; |
| 27 | +import com.google.ads.googleads.v3.services.SearchGoogleAdsStreamRequest; |
| 28 | +import com.google.ads.googleads.v3.services.SearchGoogleAdsStreamResponse; |
| 29 | +import com.google.api.gax.rpc.ServerStream; |
| 30 | +import java.io.FileNotFoundException; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +/** |
| 34 | + * Gets all available criterion bid modifier simulations for a given campaign. To get campaigns, run |
| 35 | + * GetCampaigns.java. |
| 36 | + */ |
| 37 | +public class GetCampaignCriterionBidModifierSimulations { |
| 38 | + |
| 39 | + private static class GetCampaignCriterionBidModifierSimulationsParams extends CodeSampleParams { |
| 40 | + |
| 41 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 42 | + private Long customerId; |
| 43 | + |
| 44 | + @Parameter(names = ArgumentNames.CAMPAIGN_ID, required = true) |
| 45 | + private Long campaignId; |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) throws IOException { |
| 49 | + GetCampaignCriterionBidModifierSimulationsParams params = |
| 50 | + new GetCampaignCriterionBidModifierSimulationsParams(); |
| 51 | + if (!params.parseArguments(args)) { |
| 52 | + |
| 53 | + // Either pass the required parameters for this example on the command line, or insert them |
| 54 | + // into the code here. See the parameter class definition above for descriptions. |
| 55 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 56 | + params.campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE"); |
| 57 | + } |
| 58 | + |
| 59 | + GoogleAdsClient googleAdsClient; |
| 60 | + try { |
| 61 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 62 | + } catch (FileNotFoundException fnfe) { |
| 63 | + System.err.printf( |
| 64 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 65 | + return; |
| 66 | + } catch (IOException ioe) { |
| 67 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + new GetCampaignCriterionBidModifierSimulations() |
| 73 | + .runExample(googleAdsClient, params.customerId, params.campaignId); |
| 74 | + } catch (GoogleAdsException gae) { |
| 75 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 76 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 77 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 78 | + // GoogleAdsException. |
| 79 | + System.err.printf( |
| 80 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 81 | + gae.getRequestId()); |
| 82 | + int i = 0; |
| 83 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 84 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Runs the example. |
| 91 | + * |
| 92 | + * @param googleAdsClient the Google Ads API client. |
| 93 | + * @param customerId the client customer ID. |
| 94 | + * @param campaignId the campaign ID to get the criterion bid modifier simulations. |
| 95 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 96 | + */ |
| 97 | + private void runExample(GoogleAdsClient googleAdsClient, long customerId, long campaignId) { |
| 98 | + try (GoogleAdsServiceClient googleAdsServiceClient = |
| 99 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 100 | + // Creates a query that retrieves the criterion bid modifier simulations. |
| 101 | + String query = |
| 102 | + String.format( |
| 103 | + "SELECT campaign_criterion_simulation.criterion_id, " |
| 104 | + + "campaign_criterion_simulation.start_date, " |
| 105 | + + "campaign_criterion_simulation.end_date, " |
| 106 | + + "campaign_criterion_simulation.bid_modifier_point_list.points " |
| 107 | + + "FROM campaign_criterion_simulation " |
| 108 | + + "WHERE campaign_criterion_simulation.type = BID_MODIFIER " |
| 109 | + + "AND campaign_criterion_simulation.campaign_id = %d", |
| 110 | + campaignId); |
| 111 | + // Constructs the SearchGoogleAdsStreamRequest. |
| 112 | + SearchGoogleAdsStreamRequest request = |
| 113 | + SearchGoogleAdsStreamRequest.newBuilder() |
| 114 | + .setCustomerId(Long.toString(customerId)) |
| 115 | + .setQuery(query) |
| 116 | + .build(); |
| 117 | + |
| 118 | + // Creates and issues a search Google Ads stream request. |
| 119 | + ServerStream<SearchGoogleAdsStreamResponse> stream = |
| 120 | + googleAdsServiceClient.searchStreamCallable().call(request); |
| 121 | + |
| 122 | + // Iterates over all rows in all messages and prints the requested field values for |
| 123 | + // the campaign criterion bid modifier simulation in each row. |
| 124 | + for (SearchGoogleAdsStreamResponse response : stream) { |
| 125 | + for (GoogleAdsRow googleAdsRow : response.getResultsList()) { |
| 126 | + CampaignCriterionSimulation simulation = googleAdsRow.getCampaignCriterionSimulation(); |
| 127 | + System.out.printf( |
| 128 | + "Found campaign-level criterion bid modifier simulation for " |
| 129 | + + "criterion with ID %d, start date '%s', end date '%s', and points:%n'", |
| 130 | + simulation.getCriterionId().getValue(), |
| 131 | + simulation.getStartDate().getValue(), |
| 132 | + simulation.getEndDate().getValue()); |
| 133 | + for (BidModifierSimulationPoint point : |
| 134 | + simulation.getBidModifierPointList().getPointsList()) { |
| 135 | + System.out.printf( |
| 136 | + " bid modifier: %.2f => clicks: %d, cost: %d, impressions: %d, " |
| 137 | + + "parent clicks: %d, parent cost: %d, parent impressions: %d, " |
| 138 | + + "parent required budget: %d%n", |
| 139 | + point.getBidModifier().getValue(), |
| 140 | + point.getClicks().getValue(), |
| 141 | + point.getCostMicros().getValue(), |
| 142 | + point.getImpressions().getValue(), |
| 143 | + point.getParentClicks().getValue(), |
| 144 | + point.getParentCostMicros().getValue(), |
| 145 | + point.getParentImpressions().getValue(), |
| 146 | + point.getParentRequiredBudgetMicros().getValue()); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments