Skip to content

Commit 53c19ab

Browse files
authored
Add AddGeoTarget example (#315)
1 parent 24bee41 commit 53c19ab

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.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.lib.utils.FieldMasks;
22+
import com.google.ads.googleads.v4.errors.GoogleAdsError;
23+
import com.google.ads.googleads.v4.errors.GoogleAdsException;
24+
import com.google.ads.googleads.v4.resources.ExtensionFeedItem;
25+
import com.google.ads.googleads.v4.services.ExtensionFeedItemOperation;
26+
import com.google.ads.googleads.v4.services.ExtensionFeedItemServiceClient;
27+
import com.google.ads.googleads.v4.services.MutateExtensionFeedItemsResponse;
28+
import com.google.ads.googleads.v4.utils.ResourceNames;
29+
import com.google.common.collect.ImmutableList;
30+
import com.google.protobuf.StringValue;
31+
import java.io.FileNotFoundException;
32+
import java.io.IOException;
33+
34+
/** Adds a geo target to an extension feed item for targeting. */
35+
public class AddGeoTarget {
36+
37+
// A list of country codes can be referenced here:
38+
// https://developers.google.com/adwords/api/docs/appendix/geotargeting
39+
private static long GEO_TARGET_CONSTANT_ID = 2840L; // US
40+
41+
private static class AddGeoTargetParams extends CodeSampleParams {
42+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
43+
private Long customerId;
44+
45+
@Parameter(names = ArgumentNames.FEED_ITEM_ID, required = true)
46+
private Long feedItemId;
47+
48+
@Parameter(names = ArgumentNames.GEO_TARGET_CONSTANT_ID)
49+
private Long geoTargetConstantId;
50+
}
51+
52+
public static void main(String[] args) {
53+
AddGeoTargetParams params = new AddGeoTargetParams();
54+
if (!params.parseArguments(args)) {
55+
56+
// Either pass the required parameters for this example on the command line, or insert them
57+
// into the code here. See the parameter class definition above for descriptions.
58+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
59+
params.feedItemId = Long.parseLong("INSERT_FEED_ID_HERE");
60+
61+
// Optional: Specify a geoTargetConstantId.
62+
params.geoTargetConstantId = GEO_TARGET_CONSTANT_ID;
63+
}
64+
65+
GoogleAdsClient googleAdsClient;
66+
try {
67+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
68+
} catch (FileNotFoundException fnfe) {
69+
System.err.printf(
70+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
71+
return;
72+
} catch (IOException ioe) {
73+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
74+
return;
75+
}
76+
77+
try {
78+
new AddGeoTarget()
79+
.runExample(
80+
googleAdsClient, params.customerId, params.feedItemId, params.geoTargetConstantId);
81+
} catch (GoogleAdsException gae) {
82+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
83+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
84+
// collection of GoogleAdsErrors that indicate the underlying causes of the
85+
// GoogleAdsException.
86+
System.err.printf(
87+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
88+
gae.getRequestId());
89+
int i = 0;
90+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
91+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
92+
}
93+
}
94+
}
95+
;
96+
97+
/**
98+
* Runs the example.
99+
*
100+
* @param googleAdsClient the Google Ads API client.
101+
* @param customerId the client customer ID.
102+
* @param feedItemId the feed item ID.
103+
* @param geoTargetConstantId the geo target constant ID to add to the extension feed item.
104+
*/
105+
private void runExample(
106+
GoogleAdsClient googleAdsClient, long customerId, long feedItemId, long geoTargetConstantId) {
107+
// Creates an extension feed item using the specified feed item ID and geo target constant
108+
// ID for targeting.
109+
ExtensionFeedItem extensionFeedItem =
110+
ExtensionFeedItem.newBuilder()
111+
.setResourceName(ResourceNames.extensionFeedItem(customerId, feedItemId))
112+
.setTargetedGeoTargetConstant(
113+
StringValue.of(ResourceNames.geoTargetConstant(geoTargetConstantId)))
114+
.build();
115+
116+
// Constructs an operation that will update the extension feed item, using the FieldMasks
117+
// utility to derive the update mask. This mask tells the Google Ads API which attributes of
118+
// the extension feed item you want to change.
119+
ExtensionFeedItemOperation extensionFeedItemOperation =
120+
ExtensionFeedItemOperation.newBuilder()
121+
.setUpdate(extensionFeedItem)
122+
.setUpdateMask(FieldMasks.allSetFieldsOf(extensionFeedItem))
123+
.build();
124+
125+
// Creates the service client.
126+
try (ExtensionFeedItemServiceClient extensionFeedItemServiceClient =
127+
googleAdsClient.getLatestVersion().createExtensionFeedItemServiceClient()) {
128+
// Issues a mutate request to update the extension feed item.
129+
MutateExtensionFeedItemsResponse response =
130+
extensionFeedItemServiceClient.mutateExtensionFeedItems(
131+
Long.toString(customerId), ImmutableList.of(extensionFeedItemOperation));
132+
133+
// Prints the resource name of the updated extension feed item.
134+
System.out.printf(
135+
"Updated feed item with resource name '%s'.%n", response.getResults(0).getResourceName());
136+
}
137+
}
138+
}

google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public final class ArgumentNames {
5151
public static final String FEED_ITEM_IDS = "--feedItemIds";
5252
public static final String FLIGHT_PLACEHOLDER_FIELD = "--flightPlaceholderField";
5353
public static final String GCLID = "--gclid";
54+
public static final String GEO_TARGET_CONSTANT_ID = "--geoTargetConstantId";
5455
public static final String GMB_ACCESS_TOKEN = "--gmbAccessToken";
5556
public static final String GMB_EMAIL_ADDRESS = "--gmbEmailAddress";
5657
public static final String HOTEL_CENTER_ACCOUNT_ID = "--hotelCenterAccountId";

0 commit comments

Comments
 (0)