Skip to content

Commit 39af4cc

Browse files
authored
Adds new AddRemarketingAction code example (#235)
1 parent 9e7db1b commit 39af4cc

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.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.v2.common.TagSnippet;
22+
import com.google.ads.googleads.v2.errors.GoogleAdsError;
23+
import com.google.ads.googleads.v2.errors.GoogleAdsException;
24+
import com.google.ads.googleads.v2.resources.RemarketingAction;
25+
import com.google.ads.googleads.v2.services.GoogleAdsRow;
26+
import com.google.ads.googleads.v2.services.GoogleAdsServiceClient;
27+
import com.google.ads.googleads.v2.services.GoogleAdsServiceClient.SearchPagedResponse;
28+
import com.google.ads.googleads.v2.services.MutateRemarketingActionsResponse;
29+
import com.google.ads.googleads.v2.services.RemarketingActionOperation;
30+
import com.google.ads.googleads.v2.services.RemarketingActionServiceClient;
31+
import com.google.protobuf.StringValue;
32+
import java.io.FileNotFoundException;
33+
import java.io.IOException;
34+
import java.util.Collections;
35+
36+
/** Adds a new remarketing action to the customer and then retrieves its associated tag snippets. */
37+
public class AddRemarketingAction {
38+
39+
private static class AddRemarketingActionParams extends CodeSampleParams {
40+
41+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
42+
private Long customerId;
43+
}
44+
45+
public static void main(String[] args) {
46+
AddRemarketingActionParams params = new AddRemarketingActionParams();
47+
if (!params.parseArguments(args)) {
48+
49+
// Either pass the required parameters for this example on the command line, or insert them
50+
// into the code here. See the parameter class definition above for descriptions.
51+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
52+
}
53+
54+
GoogleAdsClient googleAdsClient;
55+
try {
56+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
57+
} catch (FileNotFoundException fnfe) {
58+
System.err.printf(
59+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
60+
return;
61+
} catch (IOException ioe) {
62+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
63+
return;
64+
}
65+
66+
try {
67+
new AddRemarketingAction().runExample(googleAdsClient, params.customerId);
68+
} catch (GoogleAdsException gae) {
69+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
70+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
71+
// collection of GoogleAdsErrors that indicate the underlying causes of the
72+
// GoogleAdsException.
73+
System.err.printf(
74+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
75+
gae.getRequestId());
76+
int i = 0;
77+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
78+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
79+
}
80+
}
81+
}
82+
83+
/**
84+
* Runs the example.
85+
*
86+
* @param googleAdsClient the Google Ads API client.
87+
* @param customerId the client customer ID.
88+
* @throws GoogleAdsException if an API request failed with one or more service errors.
89+
*/
90+
private void runExample(GoogleAdsClient googleAdsClient, long customerId) {
91+
92+
// Creates a remarketing action with the specified name.
93+
RemarketingAction remarketingAction =
94+
RemarketingAction.newBuilder()
95+
.setName(StringValue.of("Remarketing action #" + System.currentTimeMillis()))
96+
.build();
97+
98+
// Creates a remarketing action operation.
99+
RemarketingActionOperation operation =
100+
RemarketingActionOperation.newBuilder().setCreate(remarketingAction).build();
101+
102+
// Issues a mutate request to add the remarketing action and prints out some information.
103+
String remarketingActionResourceName;
104+
try (RemarketingActionServiceClient conversionActionServiceClient =
105+
googleAdsClient.getLatestVersion().createRemarketingActionServiceClient()) {
106+
MutateRemarketingActionsResponse response =
107+
conversionActionServiceClient.mutateRemarketingActions(
108+
Long.toString(customerId), Collections.singletonList(operation));
109+
remarketingActionResourceName = response.getResults(0).getResourceName();
110+
System.out.printf(
111+
"Added remarketing action with resource name '%s'.%n", remarketingActionResourceName);
112+
}
113+
114+
// Creates a query that retrieves the previously created remarketing action with its generated
115+
// tag snippets.
116+
String query =
117+
String.format(
118+
"SELECT remarketing_action.id,"
119+
+ " remarketing_action.name,"
120+
+ " remarketing_action.tag_snippets "
121+
+ "FROM remarketing_action "
122+
+ "WHERE remarketing_action.resource_name = '%s'",
123+
remarketingActionResourceName);
124+
try (GoogleAdsServiceClient googleAdsServiceClient =
125+
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
126+
// Issues a search request.
127+
SearchPagedResponse searchPagedResponse =
128+
googleAdsServiceClient.search(Long.toString(customerId), query);
129+
130+
// There is only one row because we limited the search using the resource name, which is
131+
// unique.
132+
GoogleAdsRow googleAdsRow = searchPagedResponse.iterateAll().iterator().next();
133+
134+
// Prints some attributes of the remarketing action. The ID and tag snippets are generated by
135+
// the API.
136+
RemarketingAction newRemarketingAction = googleAdsRow.getRemarketingAction();
137+
System.out.printf(
138+
"Remarketing action has ID %d and name '%s'.%n%n",
139+
newRemarketingAction.getId().getValue(), newRemarketingAction.getName().getValue());
140+
System.out.println("It has the following generated tag snippets:");
141+
for (TagSnippet tagSnippet : newRemarketingAction.getTagSnippetsList()) {
142+
System.out.printf(
143+
"Tag snippet with code type '%s' and code page format '%s' has the following global"
144+
+ " site tag:%n%s%n",
145+
tagSnippet.getType(),
146+
tagSnippet.getPageFormat(),
147+
tagSnippet.getGlobalSiteTag().getValue());
148+
System.out.printf(
149+
"and the following event snippet:%n%s%n%n", tagSnippet.getEventSnippet().getValue());
150+
}
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)