|
| 1 | +// Copyright 2018 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.advancedoperations; |
| 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.lib.GoogleAdsException; |
| 22 | +import com.google.ads.googleads.v0.common.KeywordInfo; |
| 23 | +import com.google.ads.googleads.v0.enums.KeywordMatchTypeEnum.KeywordMatchType; |
| 24 | +import com.google.ads.googleads.v0.enums.SharedSetTypeEnum.SharedSetType; |
| 25 | +import com.google.ads.googleads.v0.errors.GoogleAdsError; |
| 26 | +import com.google.ads.googleads.v0.resources.CampaignName; |
| 27 | +import com.google.ads.googleads.v0.resources.CampaignSharedSet; |
| 28 | +import com.google.ads.googleads.v0.resources.SharedCriterion; |
| 29 | +import com.google.ads.googleads.v0.resources.SharedSet; |
| 30 | +import com.google.ads.googleads.v0.services.CampaignSharedSetOperation; |
| 31 | +import com.google.ads.googleads.v0.services.CampaignSharedSetServiceClient; |
| 32 | +import com.google.ads.googleads.v0.services.MutateCampaignSharedSetsResponse; |
| 33 | +import com.google.ads.googleads.v0.services.MutateSharedCriteriaResponse; |
| 34 | +import com.google.ads.googleads.v0.services.MutateSharedCriterionResult; |
| 35 | +import com.google.ads.googleads.v0.services.MutateSharedSetsResponse; |
| 36 | +import com.google.ads.googleads.v0.services.SharedCriterionOperation; |
| 37 | +import com.google.ads.googleads.v0.services.SharedCriterionServiceClient; |
| 38 | +import com.google.ads.googleads.v0.services.SharedSetOperation; |
| 39 | +import com.google.ads.googleads.v0.services.SharedSetServiceClient; |
| 40 | +import com.google.common.collect.ImmutableList; |
| 41 | +import com.google.protobuf.StringValue; |
| 42 | +import java.io.FileNotFoundException; |
| 43 | +import java.io.IOException; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +/** |
| 49 | + * This example creates a shared list of negative broad match keywords. It then attaches them to a |
| 50 | + * campaign. |
| 51 | + */ |
| 52 | +public class CreateAndAttachSharedKeywordSet { |
| 53 | + |
| 54 | + private static class CreateAndAttachSharedKeywordSetParams extends CodeSampleParams { |
| 55 | + |
| 56 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 57 | + private Long customerId; |
| 58 | + |
| 59 | + @Parameter(names = ArgumentNames.CAMPAIGN_ID, required = true) |
| 60 | + private Long campaignId; |
| 61 | + } |
| 62 | + |
| 63 | + public static void main(String[] args) throws IOException { |
| 64 | + CreateAndAttachSharedKeywordSetParams params = new CreateAndAttachSharedKeywordSetParams(); |
| 65 | + if (!params.parseArguments(args)) { |
| 66 | + |
| 67 | + // Either pass the required parameters for this example on the command line, or insert them |
| 68 | + // into the code here. See the parameter class definition above for descriptions. |
| 69 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 70 | + params.campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE"); |
| 71 | + } |
| 72 | + |
| 73 | + GoogleAdsClient googleAdsClient; |
| 74 | + try { |
| 75 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 76 | + } catch (FileNotFoundException fnfe) { |
| 77 | + System.err.printf( |
| 78 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 79 | + return; |
| 80 | + } catch (IOException ioe) { |
| 81 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + new CreateAndAttachSharedKeywordSet() |
| 87 | + .runExample(googleAdsClient, params.customerId, params.campaignId); |
| 88 | + } catch (GoogleAdsException gae) { |
| 89 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 90 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 91 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 92 | + // GoogleAdsException. |
| 93 | + System.err.printf( |
| 94 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 95 | + gae.getRequestId()); |
| 96 | + int i = 0; |
| 97 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 98 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Runs the example. |
| 105 | + * |
| 106 | + * @param googleAdsClient the Google Ads API client. |
| 107 | + * @param customerId the client customer ID. |
| 108 | + * @param campaignId the campaign ID. |
| 109 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 110 | + */ |
| 111 | + private void runExample(GoogleAdsClient googleAdsClient, long customerId, long campaignId) { |
| 112 | + |
| 113 | + // Keywords to create a shared set of. |
| 114 | + List<String> keywords = Arrays.asList("mars cruise", "mars hotels"); |
| 115 | + |
| 116 | + // Create shared negative keyword set. |
| 117 | + SharedSet sharedSet = |
| 118 | + SharedSet.newBuilder() |
| 119 | + .setName(StringValue.of("API Negative keyword list - " + System.currentTimeMillis())) |
| 120 | + .setType(SharedSetType.NEGATIVE_KEYWORDS) |
| 121 | + .build(); |
| 122 | + |
| 123 | + SharedSetOperation operation = SharedSetOperation.newBuilder().setCreate(sharedSet).build(); |
| 124 | + |
| 125 | + String sharedSetResourceName; |
| 126 | + try (SharedSetServiceClient sharedSetServiceClient = |
| 127 | + googleAdsClient.getSharedSetServiceClient()) { |
| 128 | + MutateSharedSetsResponse response = |
| 129 | + sharedSetServiceClient.mutateSharedSets( |
| 130 | + Long.toString(customerId), ImmutableList.of(operation)); |
| 131 | + sharedSetResourceName = response.getResults(0).getResourceName(); |
| 132 | + System.out.printf("Created shared set %s%n", sharedSetResourceName); |
| 133 | + } |
| 134 | + |
| 135 | + List<SharedCriterionOperation> sharedCriterionOperations = new ArrayList<>(); |
| 136 | + for (String keyword : keywords) { |
| 137 | + SharedCriterion sharedCriterion = |
| 138 | + SharedCriterion.newBuilder() |
| 139 | + .setKeyword( |
| 140 | + KeywordInfo.newBuilder() |
| 141 | + .setText(StringValue.of(keyword)) |
| 142 | + .setMatchType(KeywordMatchType.BROAD) |
| 143 | + .build()) |
| 144 | + .setSharedSet(StringValue.of(sharedSetResourceName)) |
| 145 | + .build(); |
| 146 | + |
| 147 | + SharedCriterionOperation sharedCriterionOperation = |
| 148 | + SharedCriterionOperation.newBuilder().setCreate(sharedCriterion).build(); |
| 149 | + sharedCriterionOperations.add(sharedCriterionOperation); |
| 150 | + } |
| 151 | + |
| 152 | + try (SharedCriterionServiceClient sharedCriterionServiceClient = |
| 153 | + googleAdsClient.getSharedCriterionServiceClient()) { |
| 154 | + MutateSharedCriteriaResponse response = |
| 155 | + sharedCriterionServiceClient.mutateSharedCriteria( |
| 156 | + Long.toString(customerId), sharedCriterionOperations); |
| 157 | + System.out.printf("Added %d shared criteria:%n", response.getResultsCount()); |
| 158 | + for (MutateSharedCriterionResult result : response.getResultsList()) { |
| 159 | + System.out.printf("\t%s%n", result.getResourceName()); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + String campaignResourceName = |
| 164 | + CampaignName.format(Long.toString(customerId), Long.toString(campaignId)); |
| 165 | + CampaignSharedSet campaignSharedSet = |
| 166 | + CampaignSharedSet.newBuilder() |
| 167 | + .setCampaign(StringValue.of(campaignResourceName)) |
| 168 | + .setSharedSet(StringValue.of(sharedSetResourceName)) |
| 169 | + .build(); |
| 170 | + |
| 171 | + CampaignSharedSetOperation campaignSharedSetOperation = |
| 172 | + CampaignSharedSetOperation.newBuilder().setCreate(campaignSharedSet).build(); |
| 173 | + |
| 174 | + try (CampaignSharedSetServiceClient campaignSharedSetServiceClient = |
| 175 | + googleAdsClient.getCampaignSharedSetServiceClient()) { |
| 176 | + MutateCampaignSharedSetsResponse response = |
| 177 | + campaignSharedSetServiceClient.mutateCampaignSharedSets( |
| 178 | + Long.toString(customerId), ImmutableList.of(campaignSharedSetOperation)); |
| 179 | + String campaignSharedSetResourceName = response.getResults(0).getResourceName(); |
| 180 | + System.out.printf("Created campaign shared set %s%n", campaignSharedSetResourceName); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments