|
| 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 | +// http://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.extensions; |
| 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.v6.enums.ExtensionTypeEnum.ExtensionType; |
| 22 | +import com.google.ads.googleads.v6.errors.GoogleAdsError; |
| 23 | +import com.google.ads.googleads.v6.errors.GoogleAdsException; |
| 24 | +import com.google.ads.googleads.v6.services.CampaignExtensionSettingOperation; |
| 25 | +import com.google.ads.googleads.v6.services.ExtensionFeedItemOperation; |
| 26 | +import com.google.ads.googleads.v6.services.GoogleAdsRow; |
| 27 | +import com.google.ads.googleads.v6.services.GoogleAdsServiceClient; |
| 28 | +import com.google.ads.googleads.v6.services.MutateGoogleAdsResponse; |
| 29 | +import com.google.ads.googleads.v6.services.MutateOperation; |
| 30 | +import com.google.ads.googleads.v6.services.MutateOperationResponse; |
| 31 | +import com.google.ads.googleads.v6.services.SearchGoogleAdsStreamRequest; |
| 32 | +import com.google.ads.googleads.v6.services.SearchGoogleAdsStreamResponse; |
| 33 | +import com.google.ads.googleads.v6.utils.ResourceNames; |
| 34 | +import com.google.api.gax.rpc.ServerStream; |
| 35 | +import java.io.FileNotFoundException; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.stream.Collectors; |
| 40 | + |
| 41 | +/** |
| 42 | + * Removes the entire sitelink campaign extension setting by removing both the sitelink campaign |
| 43 | + * extension setting itself and its associated sitelink extension feed items. This requires two |
| 44 | + * steps, since removing the campaign extension setting doesn't automatically remove its extension |
| 45 | + * feed items. |
| 46 | + * |
| 47 | + * <p>To make this example work with other types of extensions, find {@link ExtensionType#SITELINK} |
| 48 | + * and replace it with the extension type you wish to remove. |
| 49 | + */ |
| 50 | +public class RemoveEntireSitelinkCampaignExtensionSetting { |
| 51 | + |
| 52 | + private static class RemoveEntireSitelinkCampaignExtensionSettingParams extends CodeSampleParams { |
| 53 | + |
| 54 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 55 | + private long customerId; |
| 56 | + |
| 57 | + @Parameter(names = ArgumentNames.CAMPAIGN_ID, required = true) |
| 58 | + private long campaignId; |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + RemoveEntireSitelinkCampaignExtensionSettingParams params = |
| 63 | + new RemoveEntireSitelinkCampaignExtensionSettingParams(); |
| 64 | + if (!params.parseArguments(args)) { |
| 65 | + |
| 66 | + // Either pass the required parameters for this example on the command line, or insert them |
| 67 | + // into the code here. See the parameter class definition above for descriptions. |
| 68 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 69 | + params.campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE"); |
| 70 | + } |
| 71 | + |
| 72 | + GoogleAdsClient googleAdsClient = null; |
| 73 | + try { |
| 74 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 75 | + } catch (FileNotFoundException fnfe) { |
| 76 | + System.err.printf( |
| 77 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 78 | + System.exit(1); |
| 79 | + } catch (IOException ioe) { |
| 80 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 81 | + System.exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + new RemoveEntireSitelinkCampaignExtensionSetting() |
| 86 | + .runExample(googleAdsClient, params.customerId, params.campaignId); |
| 87 | + } catch (GoogleAdsException gae) { |
| 88 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 89 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 90 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 91 | + // GoogleAdsException. |
| 92 | + System.err.printf( |
| 93 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 94 | + gae.getRequestId()); |
| 95 | + int i = 0; |
| 96 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 97 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 98 | + } |
| 99 | + System.exit(1); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** Runs the example. */ |
| 104 | + // [START RemoveEntireSitelinkCampaignExtensionSetting] |
| 105 | + private void runExample(GoogleAdsClient googleAdsClient, long customerId, long campaignId) { |
| 106 | + |
| 107 | + // Retrieves all sitelink extension feed items for a customer and campaign. |
| 108 | + List<String> extensionFeedItemResourceNames = |
| 109 | + getSitelinkExtensionFeedItems(googleAdsClient, customerId, campaignId); |
| 110 | + |
| 111 | + // Constructs operations to remove the extension feed items. |
| 112 | + List<MutateOperation> feedItemMutateOperations = |
| 113 | + extensionFeedItemResourceNames.stream() |
| 114 | + .map( |
| 115 | + feedItem -> |
| 116 | + MutateOperation.newBuilder() |
| 117 | + .setExtensionFeedItemOperation( |
| 118 | + ExtensionFeedItemOperation.newBuilder().setRemove(feedItem)) |
| 119 | + .build()) |
| 120 | + .collect(Collectors.toList()); |
| 121 | + |
| 122 | + // Creates a list of operations that contains both the campaign extension setting and the feed |
| 123 | + // item operations. |
| 124 | + List<MutateOperation> allOperations = new ArrayList(); |
| 125 | + // Adds an operation to remove the campaign extension setting. |
| 126 | + allOperations.add( |
| 127 | + MutateOperation.newBuilder() |
| 128 | + .setCampaignExtensionSettingOperation( |
| 129 | + CampaignExtensionSettingOperation.newBuilder() |
| 130 | + .setRemove( |
| 131 | + ResourceNames.campaignExtensionSetting( |
| 132 | + customerId, campaignId, ExtensionType.SITELINK)) |
| 133 | + .build()) |
| 134 | + .build()); |
| 135 | + // Adds the operations to remove the feed items. |
| 136 | + allOperations.addAll(feedItemMutateOperations); |
| 137 | + |
| 138 | + // Connects to the API. |
| 139 | + try (GoogleAdsServiceClient client = |
| 140 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 141 | + // Issues the request. |
| 142 | + MutateGoogleAdsResponse response = client.mutate(String.valueOf(customerId), allOperations); |
| 143 | + |
| 144 | + // Prints the result of removing the campaign extension setting. |
| 145 | + // Each mutate operation response is returned in the same order as we passed its |
| 146 | + // corresponding operation. Therefore, the first belongs to the campaign setting operation, |
| 147 | + // and the rest belong to the extension feed item operations. |
| 148 | + System.out.printf( |
| 149 | + "Removed a campaign extension setting with resource name: '%s'.%n", |
| 150 | + response |
| 151 | + .getMutateOperationResponses(0) |
| 152 | + .getCampaignExtensionSettingResult() |
| 153 | + .getResourceName()); |
| 154 | + |
| 155 | + // Prints the result of removing the extension feed items. |
| 156 | + for (MutateOperationResponse mutateResponse : |
| 157 | + response |
| 158 | + .getMutateOperationResponsesList() |
| 159 | + .subList(1, response.getMutateOperationResponsesList().size())) { |
| 160 | + System.out.printf( |
| 161 | + "Removed an extension feed item with resource name: '%s'.%n", |
| 162 | + mutateResponse.getExtensionFeedItemResult().getResourceName()); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + // [END RemoveEntireSitelinkCampaignExtensionSetting] |
| 167 | + |
| 168 | + /** Retrieves all sitelink extension feed items for a customer + campaign combination. */ |
| 169 | + // [START RemoveEntireSitelinkCampaignExtensionSetting_1] |
| 170 | + private List<String> getSitelinkExtensionFeedItems( |
| 171 | + GoogleAdsClient googleAdsClient, long customerId, long campaignId) { |
| 172 | + // Defines a query to retrieve the sitelink extension feed items. |
| 173 | + String query = |
| 174 | + String.format( |
| 175 | + "SELECT campaign_extension_setting.campaign, " |
| 176 | + + " campaign_extension_setting.extension_type, " |
| 177 | + + " campaign_extension_setting.extension_feed_items " |
| 178 | + + "FROM " |
| 179 | + + " campaign_extension_setting " |
| 180 | + + "WHERE " |
| 181 | + + " campaign_extension_setting.campaign = '%s' " |
| 182 | + + " AND campaign_extension_setting.extension_type = SITELINK", |
| 183 | + ResourceNames.campaign(customerId, campaignId)); |
| 184 | + // Connects to the API. |
| 185 | + try (GoogleAdsServiceClient client = |
| 186 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 187 | + // Issues the search request. |
| 188 | + ServerStream<SearchGoogleAdsStreamResponse> response = |
| 189 | + client |
| 190 | + .searchStreamCallable() |
| 191 | + .call( |
| 192 | + SearchGoogleAdsStreamRequest.newBuilder() |
| 193 | + .setCustomerId(String.valueOf(customerId)) |
| 194 | + .setQuery(query) |
| 195 | + .build()); |
| 196 | + |
| 197 | + // Constructs the result (a list of resource names matching the query). |
| 198 | + List<String> result = new ArrayList(); |
| 199 | + for (SearchGoogleAdsStreamResponse page : response) { |
| 200 | + for (GoogleAdsRow row : page.getResultsList()) { |
| 201 | + result.addAll(row.getCampaignExtensionSetting().getExtensionFeedItemsList()); |
| 202 | + } |
| 203 | + } |
| 204 | + if (result.isEmpty()) { |
| 205 | + System.out.println( |
| 206 | + "The specified campaign does not contain a sitelink campaign extension setting."); |
| 207 | + } |
| 208 | + return result; |
| 209 | + } |
| 210 | + } |
| 211 | + // [END RemoveEntireSitelinkCampaignExtensionSetting_1] |
| 212 | +} |
0 commit comments