|
| 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.remarketing; |
| 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.CustomAudienceMemberTypeEnum.CustomAudienceMemberType; |
| 22 | +import com.google.ads.googleads.v6.enums.CustomAudienceStatusEnum.CustomAudienceStatus; |
| 23 | +import com.google.ads.googleads.v6.enums.CustomAudienceTypeEnum.CustomAudienceType; |
| 24 | +import com.google.ads.googleads.v6.errors.GoogleAdsError; |
| 25 | +import com.google.ads.googleads.v6.errors.GoogleAdsException; |
| 26 | +import com.google.ads.googleads.v6.resources.CustomAudience; |
| 27 | +import com.google.ads.googleads.v6.resources.CustomAudienceMember; |
| 28 | +import com.google.ads.googleads.v6.services.CustomAudienceOperation; |
| 29 | +import com.google.ads.googleads.v6.services.CustomAudienceServiceClient; |
| 30 | +import com.google.ads.googleads.v6.services.MutateCustomAudiencesResponse; |
| 31 | +import com.google.common.collect.ImmutableList; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.IOException; |
| 34 | + |
| 35 | +/** |
| 36 | + * Illustrates adding a custom audience. Custom audiences help you reach your ideal audience by |
| 37 | + * entering relevant keywords, URLs and apps. For more information about custom audiences, see: |
| 38 | + * https://support.google.com/google-ads/answer/9805516. |
| 39 | + */ |
| 40 | +public class AddCustomAudience { |
| 41 | + |
| 42 | + private static class AddCustomAudienceParams extends CodeSampleParams { |
| 43 | + |
| 44 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 45 | + private Long customerId; |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + AddCustomAudienceParams params = new AddCustomAudienceParams(); |
| 50 | + if (!params.parseArguments(args)) { |
| 51 | + |
| 52 | + // Either pass the required parameters for this example on the command line, or insert them |
| 53 | + // into the code here. See the parameter class definition above for descriptions. |
| 54 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 55 | + } |
| 56 | + |
| 57 | + GoogleAdsClient googleAdsClient = null; |
| 58 | + try { |
| 59 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 60 | + } catch (FileNotFoundException fnfe) { |
| 61 | + System.err.printf( |
| 62 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 63 | + System.exit(1); |
| 64 | + } catch (IOException ioe) { |
| 65 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 66 | + System.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + new AddCustomAudience().runExample(googleAdsClient, params.customerId); |
| 71 | + } catch (GoogleAdsException gae) { |
| 72 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 73 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 74 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 75 | + // GoogleAdsException. |
| 76 | + System.err.printf( |
| 77 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 78 | + gae.getRequestId()); |
| 79 | + int i = 0; |
| 80 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 81 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 82 | + } |
| 83 | + System.exit(1); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** Runs the example. */ |
| 88 | + private void runExample(GoogleAdsClient googleAdsClient, long customerId) { |
| 89 | + // Creates a CustomAudience object to represent the new audience. |
| 90 | + CustomAudience customAudience = |
| 91 | + CustomAudience.newBuilder() |
| 92 | + .setName("Example CustomAudience #" + System.currentTimeMillis()) |
| 93 | + .setDescription("Custom audiences who have searched specific terms on Google Search") |
| 94 | + // Matches customers by what they searched on Google Search. |
| 95 | + // Note: "INTEREST" OR "PURCHASE_INTENT" is not allowed for the type field |
| 96 | + // of newly created custom audience. Use "AUTO" instead of these 2 options |
| 97 | + // when creating a new custom audience. |
| 98 | + .setType(CustomAudienceType.SEARCH) |
| 99 | + .setStatus(CustomAudienceStatus.ENABLED) |
| 100 | + |
| 101 | + // Lists the members that this custom audience is composed of. Customers that meet any |
| 102 | + // of the membership conditions will be reached. |
| 103 | + |
| 104 | + // Adds Keywords or keyword phrases, which describe the customers' interests or search |
| 105 | + // terms. |
| 106 | + .addMembers(createCustomAudienceMember(CustomAudienceMemberType.KEYWORD, "mars cruise")) |
| 107 | + .addMembers( |
| 108 | + createCustomAudienceMember(CustomAudienceMemberType.KEYWORD, "jupiter cruise")) |
| 109 | + |
| 110 | + // Adds website URLs that your customers might visit. |
| 111 | + .addMembers( |
| 112 | + createCustomAudienceMember( |
| 113 | + CustomAudienceMemberType.URL, "http://www.example.com/locations/mars")) |
| 114 | + .addMembers( |
| 115 | + createCustomAudienceMember( |
| 116 | + CustomAudienceMemberType.URL, "http://www.example.com/locations/jupiter")) |
| 117 | + |
| 118 | + // Adds package names of Android apps which customers might install. |
| 119 | + .addMembers( |
| 120 | + createCustomAudienceMember( |
| 121 | + CustomAudienceMemberType.APP, "com.google.android.apps.adwords")) |
| 122 | + .build(); |
| 123 | + |
| 124 | + // Creates an operation to add the CustomAudience. |
| 125 | + CustomAudienceOperation operation = |
| 126 | + CustomAudienceOperation.newBuilder().setCreate(customAudience).build(); |
| 127 | + |
| 128 | + // Creates an API client and send the mutate request. |
| 129 | + try (CustomAudienceServiceClient serviceClient = |
| 130 | + googleAdsClient.getLatestVersion().createCustomAudienceServiceClient()) { |
| 131 | + // Issues the mutate request. |
| 132 | + MutateCustomAudiencesResponse response = |
| 133 | + serviceClient.mutateCustomAudiences( |
| 134 | + String.valueOf(customerId), ImmutableList.of(operation)); |
| 135 | + |
| 136 | + // Prints some information about the result. |
| 137 | + System.out.printf( |
| 138 | + "New custom audience added with resource name: '%s'.\n", |
| 139 | + response.getResults(0).getResourceName()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Constructs a {@link CustomAudienceMember} from a {@link CustomAudienceMemberType} and value for |
| 145 | + * the member type. |
| 146 | + */ |
| 147 | + private static CustomAudienceMember createCustomAudienceMember( |
| 148 | + CustomAudienceMemberType memberType, String value) { |
| 149 | + CustomAudienceMember.Builder builder = |
| 150 | + CustomAudienceMember.newBuilder().setMemberType(memberType); |
| 151 | + if (memberType == CustomAudienceMemberType.KEYWORD) { |
| 152 | + builder.setKeyword(value); |
| 153 | + } else if (memberType == CustomAudienceMemberType.URL) { |
| 154 | + builder.setUrl(value); |
| 155 | + } else if (memberType == CustomAudienceMemberType.APP) { |
| 156 | + builder.setApp(value); |
| 157 | + } |
| 158 | + return builder.build(); |
| 159 | + } |
| 160 | +} |
0 commit comments