/
AddMerchantCenterDynamicRemarketingCampaign.java
411 lines (378 loc) · 18.5 KB
/
AddMerchantCenterDynamicRemarketingCampaign.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// 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 static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime;
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.v8.common.AdImageAsset;
import com.google.ads.googleads.v8.common.AdTextAsset;
import com.google.ads.googleads.v8.common.ImageAsset;
import com.google.ads.googleads.v8.common.ManualCpc;
import com.google.ads.googleads.v8.common.ResponsiveDisplayAdInfo;
import com.google.ads.googleads.v8.common.UserListInfo;
import com.google.ads.googleads.v8.enums.AdGroupStatusEnum.AdGroupStatus;
import com.google.ads.googleads.v8.enums.AdvertisingChannelTypeEnum.AdvertisingChannelType;
import com.google.ads.googleads.v8.enums.AssetTypeEnum.AssetType;
import com.google.ads.googleads.v8.enums.CampaignStatusEnum.CampaignStatus;
import com.google.ads.googleads.v8.enums.DisplayAdFormatSettingEnum.DisplayAdFormatSetting;
import com.google.ads.googleads.v8.errors.GoogleAdsError;
import com.google.ads.googleads.v8.errors.GoogleAdsException;
import com.google.ads.googleads.v8.resources.Ad;
import com.google.ads.googleads.v8.resources.AdGroup;
import com.google.ads.googleads.v8.resources.AdGroupAd;
import com.google.ads.googleads.v8.resources.AdGroupCriterion;
import com.google.ads.googleads.v8.resources.Asset;
import com.google.ads.googleads.v8.resources.Campaign;
import com.google.ads.googleads.v8.resources.Campaign.ShoppingSetting;
import com.google.ads.googleads.v8.services.AdGroupAdOperation;
import com.google.ads.googleads.v8.services.AdGroupAdServiceClient;
import com.google.ads.googleads.v8.services.AdGroupCriterionOperation;
import com.google.ads.googleads.v8.services.AdGroupCriterionServiceClient;
import com.google.ads.googleads.v8.services.AdGroupOperation;
import com.google.ads.googleads.v8.services.AdGroupServiceClient;
import com.google.ads.googleads.v8.services.AssetOperation;
import com.google.ads.googleads.v8.services.AssetServiceClient;
import com.google.ads.googleads.v8.services.CampaignOperation;
import com.google.ads.googleads.v8.services.CampaignServiceClient;
import com.google.ads.googleads.v8.services.MutateAdGroupAdsResponse;
import com.google.ads.googleads.v8.services.MutateAdGroupCriteriaResponse;
import com.google.ads.googleads.v8.services.MutateAdGroupsResponse;
import com.google.ads.googleads.v8.services.MutateAssetsResponse;
import com.google.ads.googleads.v8.services.MutateCampaignsResponse;
import com.google.ads.googleads.v8.utils.ResourceNames;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.protobuf.ByteString;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
/**
* Creates a shopping campaign associated with an existing merchant center account, along with a
* related ad group and dynamic display ad, and targets a user list for remarketing purposes.
*/
public class AddMerchantCenterDynamicRemarketingCampaign {
private static class AddMerchantCenterDynamicRemarketingCampaignParams extends CodeSampleParams {
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
private long customerId;
@Parameter(names = ArgumentNames.MERCHANT_CENTER_ACCOUNT_ID, required = true)
private long merchantCenterAccountId;
@Parameter(names = ArgumentNames.CAMPAIGN_BUDGET_ID, required = true)
private long campaignBudgetId;
@Parameter(names = ArgumentNames.USER_LIST_ID, required = true)
private long userListId;
}
public static void main(String[] args) throws IOException {
AddMerchantCenterDynamicRemarketingCampaignParams params =
new AddMerchantCenterDynamicRemarketingCampaignParams();
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.merchantCenterAccountId = Long.parseLong("INSERT_MERCHANT_CENTER_ACCOUNT_ID_HERE");
params.campaignBudgetId = Long.parseLong("INSERT_CAMPAIGN_BUDGET_ID_HERE");
params.userListId = Long.parseLong("INSERT_USER_LIST_ID_HERE");
}
GoogleAdsClient googleAdsClient = null;
try {
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
} catch (FileNotFoundException fnfe) {
System.err.printf(
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
System.exit(1);
} catch (IOException ioe) {
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
System.exit(1);
}
try {
new AddMerchantCenterDynamicRemarketingCampaign().runExample(googleAdsClient, params);
} 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);
}
System.exit(1);
}
}
/**
* Runs the example.
*
* @param googleAdsClient the Google Ads API client.
* @param params the AddMerchantCenterDynamicRemarketingCampaignParams for the example.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
private void runExample(
GoogleAdsClient googleAdsClient, AddMerchantCenterDynamicRemarketingCampaignParams params)
throws IOException {
// Creates a shopping campaign associated with a given merchant center account.
String campaignResourceName =
createCampaign(
googleAdsClient,
params.customerId,
params.merchantCenterAccountId,
params.campaignBudgetId);
// Creates an ad group for the campaign.
String adGroupResourceName =
createAdGroup(googleAdsClient, params.customerId, campaignResourceName);
// Creates a dynamic display ad in the ad group.
createAd(googleAdsClient, params.customerId, adGroupResourceName);
// Targets a specific user list for remarketing.
attachUserList(googleAdsClient, params.customerId, adGroupResourceName, params.userListId);
}
/**
* Creates a campaign linked to a Merchant Center product feed.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param merchantCenterAccountId the Merchant Center account ID.
* @param campaignBudgetId the campaign budget ID.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
// [START add_merchant_center_dynamic_remarketing_campaign_2]
private String createCampaign(
GoogleAdsClient googleAdsClient,
long customerId,
long merchantCenterAccountId,
long campaignBudgetId) {
String budgetResourceName = ResourceNames.campaignBudget(customerId, campaignBudgetId);
// Creates the campaign.
Campaign campaign =
Campaign.newBuilder()
.setName("Shopping campaign #" + getPrintableDateTime())
// Dynamic remarketing campaigns are only available on the Google Display Network.
.setAdvertisingChannelType(AdvertisingChannelType.DISPLAY)
.setStatus(CampaignStatus.PAUSED)
.setCampaignBudget(budgetResourceName)
.setManualCpc(ManualCpc.newBuilder().build())
// The settings for the shopping campaign.
// This connects the campaign to the merchant center account.
.setShoppingSetting(
ShoppingSetting.newBuilder()
.setCampaignPriority(0)
.setMerchantId(merchantCenterAccountId)
// Display Network campaigns do not support partition by country. The only
// supported value is "ZZ". This signals that products from all countries are
// available in the campaign. The actual products which serve are based on
// the products tagged in the user list entry.
.setSalesCountry("ZZ")
.setEnableLocal(true)
.build())
.build();
// Creates the campaign operation.
CampaignOperation operation = CampaignOperation.newBuilder().setCreate(campaign).build();
// Creates the campaign service client.
try (CampaignServiceClient campaignServiceClient =
googleAdsClient.getLatestVersion().createCampaignServiceClient()) {
// Adds the campaign.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
Long.toString(customerId), ImmutableList.of(operation));
String campaignResourceName = response.getResults(0).getResourceName();
System.out.printf("Created campaign with resource name '%s'.%n", campaignResourceName);
return campaignResourceName;
}
}
// [END add_merchant_center_dynamic_remarketing_campaign_2]
/**
* Creates an ad group for the remarketing campaign.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param campaignResourceName the campaign resource name.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
// [START add_merchant_center_dynamic_remarketing_campaign_1]
private String createAdGroup(
GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) {
// Creates the ad group.
AdGroup adGroup =
AdGroup.newBuilder()
.setName("Dynamic remarketing ad group")
.setCampaign(campaignResourceName)
.setStatus(AdGroupStatus.ENABLED)
.build();
// Creates the ad group operation.
AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build();
// Creates the ad group service client.
try (AdGroupServiceClient adGroupServiceClient =
googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
// Adds the ad group.
MutateAdGroupsResponse response =
adGroupServiceClient.mutateAdGroups(
Long.toString(customerId), ImmutableList.of(operation));
String adGroupResourceName = response.getResults(0).getResourceName();
System.out.printf("Created ad group with resource name '%s'.%n", adGroupResourceName);
return adGroupResourceName;
}
}
// [END add_merchant_center_dynamic_remarketing_campaign_1]
/**
* Creates the responsive display ad.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param adGroupResourceName the campaign resource name.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
// [START add_merchant_center_dynamic_remarketing_campaign]
private void createAd(
GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName)
throws IOException {
String marketingImageUrl = "https://goo.gl/3b9Wfh";
String marketingImageName = "Marketing Image";
String marketingImageResourceName =
uploadAsset(googleAdsClient, customerId, marketingImageUrl, marketingImageName);
String squareMarketingImageName = "Square Marketing Image";
String squareMarketingImageUrl = "https://goo.gl/mtt54n";
String squareMarketingImageResourceName =
uploadAsset(googleAdsClient, customerId, squareMarketingImageUrl, squareMarketingImageName);
// Creates the responsive display ad info object.
ResponsiveDisplayAdInfo responsiveDisplayAdInfo =
ResponsiveDisplayAdInfo.newBuilder()
.addMarketingImages(
AdImageAsset.newBuilder().setAsset(marketingImageResourceName).build())
.addSquareMarketingImages(
AdImageAsset.newBuilder().setAsset(squareMarketingImageResourceName).build())
.addHeadlines(AdTextAsset.newBuilder().setText("Travel").build())
.setLongHeadline(AdTextAsset.newBuilder().setText("Travel the World").build())
.addDescriptions(AdTextAsset.newBuilder().setText("Take to the air!").build())
.setBusinessName("Interplanetary Cruises")
// Optional: Call to action text.
// Valid texts: https://support.google.com/adwords/answer/7005917
.setCallToActionText("Apply Now")
// Optional: Sets the ad colors.
.setMainColor("#0000ff")
.setAccentColor("#ffff00")
// Optional: Sets to false to strictly render the ad using the colors.
.setAllowFlexibleColor(false)
// Optional: Sets the format setting that the ad will be served in.
.setFormatSetting(DisplayAdFormatSetting.NON_NATIVE)
// Optional: Creates a logo image and sets it to the ad.
/*
.addLogoImages(
AdImageAsset.newBuilder()
.setAsset(StringValue.of("INSERT_LOGO_IMAGE_RESOURCE_NAME_HERE"))
.build())
*/
// Optional: Creates a square logo image and sets it to the ad.
/*
.addSquareLogoImages(
AdImageAsset.newBuilder()
.setAsset(StringValue.of("INSERT_SQUARE_LOGO_IMAGE_RESOURCE_NAME_HERE"))
.build())
*/
.build();
// Creates the ad.
Ad ad =
Ad.newBuilder()
.setResponsiveDisplayAd(responsiveDisplayAdInfo)
.addFinalUrls("http://www.example.com/")
.build();
// Creates the ad group ad.
AdGroupAd adGroupAd = AdGroupAd.newBuilder().setAdGroup(adGroupResourceName).setAd(ad).build();
// Creates the ad group ad operation.
AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build();
// Creates the ad group ad service client.
try (AdGroupAdServiceClient adGroupAdServiceClient =
googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) {
// Adds the ad group ad.
MutateAdGroupAdsResponse response =
adGroupAdServiceClient.mutateAdGroupAds(
Long.toString(customerId), ImmutableList.of(operation));
System.out.printf(
"Created ad group ad with resource name '%s'.%n",
response.getResults(0).getResourceName());
}
}
// [END add_merchant_center_dynamic_remarketing_campaign]
/**
* Adds an image to the Google Ads account.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param imageUrl the url of the image.
* @param assetName the name of the asset.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
private String uploadAsset(
GoogleAdsClient googleAdsClient, long customerId, String imageUrl, String assetName)
throws IOException {
byte[] imageData = ByteStreams.toByteArray(new URL(imageUrl).openStream());
// Creates the image asset.
Asset asset =
Asset.newBuilder()
.setName(assetName)
.setType(AssetType.IMAGE)
.setImageAsset(ImageAsset.newBuilder().setData(ByteString.copyFrom(imageData)).build())
.build();
// Creates the asset operation.
AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build();
// Creates the asset service client.
try (AssetServiceClient assetServiceClient =
googleAdsClient.getLatestVersion().createAssetServiceClient()) {
// Adds the image asset.
MutateAssetsResponse response =
assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation));
String imageResourceName = response.getResults(0).getResourceName();
System.out.printf("Created image asset with resource name '%s'.%n", imageResourceName);
return imageResourceName;
}
}
/**
* Targets a user list.
*
* @param googleAdsClient the Google Ads API client.
* @param customerId the client customer ID.
* @param adGroupResourceName the campaign resource name.
* @param userListId ID of the user list.
* @throws GoogleAdsException if an API request failed with one or more service errors.
*/
// [START add_merchant_center_dynamic_remarketing_campaign_3]
private void attachUserList(
GoogleAdsClient googleAdsClient,
long customerId,
String adGroupResourceName,
long userListId) {
String userListResourceName = ResourceNames.userList(customerId, userListId);
// Creates the ad group criterion that targets the user list.
AdGroupCriterion adGroupCriterion =
AdGroupCriterion.newBuilder()
.setAdGroup(adGroupResourceName)
.setUserList(UserListInfo.newBuilder().setUserList(userListResourceName).build())
.build();
// Creates the ad group criterion operation.
AdGroupCriterionOperation operation =
AdGroupCriterionOperation.newBuilder().setCreate(adGroupCriterion).build();
// Creates the ad group criterion service client.
try (AdGroupCriterionServiceClient adGroupCriterionServiceClient =
googleAdsClient.getLatestVersion().createAdGroupCriterionServiceClient()) {
// Adds the ad group criterion.
MutateAdGroupCriteriaResponse response =
adGroupCriterionServiceClient.mutateAdGroupCriteria(
Long.toString(customerId), ImmutableList.of(operation));
System.out.printf(
"Created ad group criterion with resource name '%s'.%n",
response.getResults(0).getResourceName());
}
}
// [END add_merchant_center_dynamic_remarketing_campaign_3]
}