-
Notifications
You must be signed in to change notification settings - Fork 176
/
UpdateFeedItemAttributeValue.java
279 lines (253 loc) · 11.7 KB
/
UpdateFeedItemAttributeValue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.ads.googleads.examples.remarketing;
import com.beust.jcommander.Parameter;
import com.google.ads.googleads.examples.utils.ArgumentNames;
import com.google.ads.googleads.examples.utils.CodeSampleParams;
import com.google.ads.googleads.lib.GoogleAdsClient;
import com.google.ads.googleads.lib.utils.FieldMasks;
import com.google.ads.googleads.v3.enums.FlightPlaceholderFieldEnum.FlightPlaceholderField;
import com.google.ads.googleads.v3.errors.GoogleAdsError;
import com.google.ads.googleads.v3.errors.GoogleAdsException;
import com.google.ads.googleads.v3.resources.FeedAttribute;
import com.google.ads.googleads.v3.resources.FeedItem;
import com.google.ads.googleads.v3.resources.FeedItemAttributeValue;
import com.google.ads.googleads.v3.services.FeedItemOperation;
import com.google.ads.googleads.v3.services.FeedItemServiceClient;
import com.google.ads.googleads.v3.services.GoogleAdsServiceClient;
import com.google.ads.googleads.v3.services.GoogleAdsServiceClient.SearchPagedResponse;
import com.google.ads.googleads.v3.services.MutateFeedItemResult;
import com.google.ads.googleads.v3.services.MutateFeedItemsResponse;
import com.google.ads.googleads.v3.services.SearchGoogleAdsRequest;
import com.google.ads.googleads.v3.utils.ResourceNames;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.Int64Value;
import com.google.protobuf.StringValue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
/**
* Updates a FeedItemAttributeValue in a flights feed. To create a flights feed,
* run the AddFlightsFeed example. This example is specific to feeds of type DYNAMIC_FLIGHT.
* The attribute you are updating must be present on the feed. This example is
* specifically for updating the StringValue of an attribute.
*/
public class UpdateFeedItemAttributeValue {
private static final int PAGE_SIZE = 1_000;
private static class UpdateFeedItemAttributeValueParams extends CodeSampleParams {
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
private long customerId;
@Parameter(names = ArgumentNames.FEED_ID, required = true)
private long feedId;
@Parameter(names = ArgumentNames.FEED_ITEM_ID, required = true)
private long feedItemId;
@Parameter(names = ArgumentNames.FLIGHT_PLACEHOLDER_FIELD, required = true)
private String flightPlaceholderField;
@Parameter(names = ArgumentNames.ATTRIBUTE_VALUE, required = true)
private String attributeValue;
}
public static void main(String[] args) {
UpdateFeedItemAttributeValueParams params = new UpdateFeedItemAttributeValueParams();
if (!params.parseArguments(args)) {
// Either pass the required parameters for this example on the command line, or insert them
// into the code here. See the parameter class definition above for descriptions.
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
params.feedId = Long.parseLong("INSERT_FEED_ID_HERE");
params.feedItemId = Long.parseLong("INSERT_FEED_ITEM_ID_HERE");
params.flightPlaceholderField = "INSERT_FLIGHT_PLACEHOLDER_FIELD_HERE";
params.attributeValue = "INSERT_ATTRIBUTE_VALUE_HERE";
}
GoogleAdsClient googleAdsClient;
try {
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
} catch (FileNotFoundException fnfe) {
System.err.printf(
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
return;
} catch (IOException ioe) {
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
return;
}
try {
new UpdateFeedItemAttributeValue().runExample(
googleAdsClient,
params.customerId,
params.feedId,
params.feedItemId,
params.flightPlaceholderField,
params.attributeValue);
} catch (GoogleAdsException gae) {
// GoogleAdsException is the base class for most exceptions thrown by an API request.
// Instances of this exception have a message and a GoogleAdsFailure that contains a
// collection of GoogleAdsErrors that indicate the underlying causes of the
// GoogleAdsException.
System.err.printf(
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
gae.getRequestId());
int i = 0;
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
}
}
}
/**
* Runs the example.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param feedId ID of the feed containing the feed item to be updated.
* @param feedItemId ID of the feed item to be updated.
* @param flightPlaceholderField the placeholder type for the attribute to be removed.
* @param attributeValue String value with which to update the FeedAttributeValue.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
private void runExample(
GoogleAdsClient googleAdsClient,
long customerId,
long feedId,
long feedItemId,
String flightPlaceholderField,
String attributeValue) {
updateFeedItem(
googleAdsClient, customerId, feedId, feedItemId, flightPlaceholderField, attributeValue);
}
/**
* Updates attribute value of the feed item. In order to update a FeedItemAttributeValue you must
* update the FeedItem.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param feedId ID of the feed containing the feed item to be updated.
* @param feedItemId ID of the feed item to be updated.
* @param flightPlaceholderField the placeholder type for the attribute to be removed.
* @param attributeValue String value with which to update the FeedAttributeValue.
*/
private void updateFeedItem(
GoogleAdsClient googleAdsClient,
long customerId,
long feedId,
long feedItemId,
String flightPlaceholderField,
String attributeValue) {
// Gets the feed resource name.
String feedResourceName = ResourceNames.feed(customerId, feedId);
// Gets a map of the placeholder values and feed attributes.
Map<FlightPlaceholderField, FeedAttribute> feedAttributes =
AddFlightsFeed.getFeed(googleAdsClient, customerId, feedResourceName);
// Gets the ID of the attribute to update. This is needed to specify which
// FeedItemAttributeValue will be updated in the given FeedItem.
long attributeId = feedAttributes
.get(FlightPlaceholderField.valueOf(flightPlaceholderField.toUpperCase()))
.getId()
.getValue();
// Gets the feed item resource name.
String feedItemResourceName =
ResourceNames.feedItem(customerId, feedId, feedItemId);
// Retrieves the feed item and its associated attributes based on its resource name.
FeedItem feedItem = getFeedItem(googleAdsClient, customerId, feedItemResourceName);
// Creates the updated FeedItemAttributeValue.
FeedItemAttributeValue feedItemAttributeValue =
FeedItemAttributeValue.newBuilder()
.setFeedAttributeId(Int64Value.of(attributeId))
.setStringValue(StringValue.of(attributeValue))
.build();
// Creates a new FeedItem from the existing FeedItem. Any FeedItemAttributeValues that are
// not included in the updated FeedItem will be removed from the FeedItem, which is why you
// must create the FeedItem from the existing FeedItem and set the field(s) that will be
// updated.
feedItem =
feedItem.toBuilder()
// Sets the attribute value of the FeedItem given its index relative to other attributes
// in the FeedItem.
.setAttributeValues(
// Gets the index of the attribute value that will be updated.
getAttributeIndex(feedItem, feedItemAttributeValue), feedItemAttributeValue)
.build();
// Creates the operation.
FeedItemOperation operation =
FeedItemOperation.newBuilder()
.setUpdate(feedItem)
.setUpdateMask(FieldMasks.allSetFieldsOf(feedItem))
.build();
// Creates the feed item service client.
try (FeedItemServiceClient feedItemServiceClient =
googleAdsClient.getLatestVersion().createFeedItemServiceClient()) {
// Updates the feed item.
MutateFeedItemsResponse response =
feedItemServiceClient.mutateFeedItems(
Long.toString(customerId), ImmutableList.of(operation));
for (MutateFeedItemResult result : response.getResultsList()) {
System.out.printf("Updated feed item with resource name '%s'.%n", result.getResourceName());
}
}
}
/**
* Retrieves a feed item and its attribute values given a resource name.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param feedItemResourceName feed item resource name.
* @return FeedItem with the given resource name.
*/
private FeedItem getFeedItem(
GoogleAdsClient googleAdsClient, long customerId, String feedItemResourceName) {
// Constructs the query.
String query =
"SELECT feed_item.attribute_values FROM feed_item WHERE feed_item.resource_name "
+ "= '"
+ feedItemResourceName
+ "'";
// Constructs the request.
SearchGoogleAdsRequest request =
SearchGoogleAdsRequest.newBuilder()
.setCustomerId(String.valueOf(customerId))
.setPageSize(PAGE_SIZE)
.setQuery(query)
.build();
// Issues the search request.
try (GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
SearchPagedResponse searchPagedResponse = googleAdsServiceClient.search(request);
// Returns the feed item attribute values.
return searchPagedResponse.getPage().getResponse().getResults(0).getFeedItem();
}
}
/**
* Gets the ID of the attribute. This is needed to specify which
* FeedItemAttributeValue will be updated in the given FeedItem.
*
* @param feedItem the FeedItem that will be updated.
* @param newFeedItemAttributeValue the new FeedItemAttributeValue that will be updated.
* @return int the index of the attribute.
*/
private int getAttributeIndex(
FeedItem feedItem, FeedItemAttributeValue newFeedItemAttributeValue) {
Integer attributeIndex = null;
// Loops through attribute values to find the index of the FeedItemAttributeValue to update
for (FeedItemAttributeValue feedItemAttributeValue : feedItem.getAttributeValuesList()) {
attributeIndex = (attributeIndex != null) ? attributeIndex + 1 : 0;
// Checks if the current feedItemAttributeValue is the one we are updating
if (feedItemAttributeValue.getFeedAttributeId().getValue()
== newFeedItemAttributeValue.getFeedAttributeId().getValue()) {
break;
}
}
// Throws an exception if the attribute value is not found.
if (attributeIndex == null) {
new IllegalArgumentException(
"No matching feed attribute for feed item attribute value: " + newFeedItemAttributeValue);
}
return attributeIndex;
}
}