Skip to content

Commit a6c500d

Browse files
authored
Added UpdateSitelink example (#257)
1 parent 3c6934d commit a6c500d

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.v3.common.SitelinkFeedItem;
23+
import com.google.ads.googleads.v3.errors.GoogleAdsError;
24+
import com.google.ads.googleads.v3.errors.GoogleAdsException;
25+
import com.google.ads.googleads.v3.resources.ExtensionFeedItem;
26+
import com.google.ads.googleads.v3.services.ExtensionFeedItemOperation;
27+
import com.google.ads.googleads.v3.services.ExtensionFeedItemServiceClient;
28+
import com.google.ads.googleads.v3.services.MutateExtensionFeedItemResult;
29+
import com.google.ads.googleads.v3.services.MutateExtensionFeedItemsResponse;
30+
import com.google.ads.googleads.v3.utils.ResourceNames;
31+
import com.google.common.collect.ImmutableList;
32+
import com.google.protobuf.StringValue;
33+
import java.io.FileNotFoundException;
34+
import java.io.IOException;
35+
36+
/**
37+
* Updates a sitelink extension feed item {@code SitelinkFeedItem} with the specified link text
38+
* and URL.
39+
*/
40+
public class UpdateSitelink {
41+
42+
private static class UpdateSitelinkParams extends CodeSampleParams {
43+
44+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
45+
private Long customerId;
46+
47+
@Parameter(names = ArgumentNames.FEED_ITEM_ID, required = true)
48+
private Long feedItemId;
49+
50+
@Parameter(names = ArgumentNames.SITELINK_TEXT, required = true)
51+
private String sitelinkText;
52+
}
53+
54+
public static void main(String[] args) {
55+
UpdateSitelinkParams params = new UpdateSitelinkParams();
56+
if (!params.parseArguments(args)) {
57+
58+
// Either pass the required parameters for this example on the command line, or insert them
59+
// into the code here. See the parameter class definition above for descriptions.
60+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
61+
params.feedItemId = Long.parseLong("INSERT_FEED_ITEM_ID_HERE");
62+
params.sitelinkText = "INSERT_SITELINK_TEXT_HERE";
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 UpdateSitelink()
79+
.runExample(googleAdsClient, params.customerId, params.feedItemId, params.sitelinkText);
80+
} catch (GoogleAdsException gae) {
81+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
82+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
83+
// collection of GoogleAdsErrors that indicate the underlying causes of the
84+
// GoogleAdsException.
85+
System.err.printf(
86+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
87+
gae.getRequestId());
88+
int i = 0;
89+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
90+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
91+
}
92+
}
93+
}
94+
95+
/**
96+
* Runs the example.
97+
*
98+
* @param googleAdsClient the Google Ads API client.
99+
* @param customerId the client customer ID.
100+
* @param feedItemId the ID of the feed item to update.
101+
* @param sitelinkText the new sitelink text to update to.
102+
* @throws GoogleAdsException if an API request failed with one or more service errors.
103+
*/
104+
private void runExample(
105+
GoogleAdsClient googleAdsClient, long customerId, long feedItemId, String sitelinkText) {
106+
try (ExtensionFeedItemServiceClient extensionFeedItemServiceClient =
107+
googleAdsClient.getLatestVersion().createExtensionFeedItemServiceClient()) {
108+
// Creates an extension feed item using the specified feed item ID and sitelink text.
109+
ExtensionFeedItem extensionFeedItem = ExtensionFeedItem.newBuilder()
110+
.setResourceName(ResourceNames.extensionFeedItem(customerId, feedItemId))
111+
.setSitelinkFeedItem(
112+
SitelinkFeedItem.newBuilder().setLinkText(StringValue.of(sitelinkText)).build())
113+
.build();
114+
// Constructs an operation that will update the extension feed item, using the FieldMasks
115+
// utility to derive the update mask. This mask tells the Google Ads API which attributes of
116+
// the extension feed item you want to change.
117+
ExtensionFeedItemOperation operation =
118+
ExtensionFeedItemOperation.newBuilder()
119+
.setUpdate(extensionFeedItem)
120+
.setUpdateMask(FieldMasks.allSetFieldsOf(extensionFeedItem))
121+
.build();
122+
// Sends the operation in a mutate request.
123+
MutateExtensionFeedItemsResponse response =
124+
extensionFeedItemServiceClient.mutateExtensionFeedItems(
125+
Long.toString(customerId), ImmutableList.of(operation));
126+
// Prints the resource name of each updated object.
127+
for (MutateExtensionFeedItemResult mutateExtensionFeedItemResult : response
128+
.getResultsList()) {
129+
System.out.printf(
130+
"Updated extension feed item with the resource name: '%s'.%n",
131+
mutateExtensionFeedItemResult.getResourceName());
132+
}
133+
}
134+
}
135+
}

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
@@ -57,4 +57,5 @@ public final class ArgumentNames {
5757
public static final String MANAGER_ID = "--managerId";
5858
public static final String CALLOUT_TEXT = "--calloutText";
5959
public static final String LANGUAGE_CODE = "--languageCode";
60+
public static final String SITELINK_TEXT = "--sitelinkText";
6061
}

0 commit comments

Comments
 (0)