-
Notifications
You must be signed in to change notification settings - Fork 361
/
MigrateToExtensionSettings.java
executable file
·594 lines (518 loc) · 24.9 KB
/
MigrateToExtensionSettings.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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
//
// http://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 adwords.axis.v201809.migration;
import static com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME;
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201809.cm.ApiError;
import com.google.api.ads.adwords.axis.v201809.cm.ApiException;
import com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSetting;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingOperation;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedOperation;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedPage;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand;
import com.google.api.ads.adwords.axis.v201809.cm.ExtensionFeedItem;
import com.google.api.ads.adwords.axis.v201809.cm.ExtensionSetting;
import com.google.api.ads.adwords.axis.v201809.cm.ExtensionSettingPlatform;
import com.google.api.ads.adwords.axis.v201809.cm.Feed;
import com.google.api.ads.adwords.axis.v201809.cm.FeedItem;
import com.google.api.ads.adwords.axis.v201809.cm.FeedItemAttributeValue;
import com.google.api.ads.adwords.axis.v201809.cm.FeedItemOperation;
import com.google.api.ads.adwords.axis.v201809.cm.FeedItemPage;
import com.google.api.ads.adwords.axis.v201809.cm.FeedItemServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.FeedMapping;
import com.google.api.ads.adwords.axis.v201809.cm.FeedMappingPage;
import com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.FeedPage;
import com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.FeedType;
import com.google.api.ads.adwords.axis.v201809.cm.Function;
import com.google.api.ads.adwords.axis.v201809.cm.FunctionArgumentOperand;
import com.google.api.ads.adwords.axis.v201809.cm.FunctionOperand;
import com.google.api.ads.adwords.axis.v201809.cm.FunctionOperator;
import com.google.api.ads.adwords.axis.v201809.cm.Operator;
import com.google.api.ads.adwords.axis.v201809.cm.RequestContextOperand;
import com.google.api.ads.adwords.axis.v201809.cm.RequestContextOperandContextType;
import com.google.api.ads.adwords.axis.v201809.cm.SitelinkFeedItem;
import com.google.api.ads.adwords.axis.v201809.cm.UrlList;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.common.lib.conf.ConfigurationLoadException;
import com.google.api.ads.common.lib.exception.OAuthException;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.client.auth.oauth2.Credential;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* This code example migrates your feed based sitelinks at campaign level to use extension settings.
* To learn more about extensionsettings, see
* https://developers.google.com/adwords/api/docs/guides/extension-settings. To learn more about
* migrating Feed based extensions to extension settings, see
* https://developers.google.com/adwords/api/docs/guides/migrate-to-extension-settings.
*
* <p>This code example doesn't migrate scheduling or feed item-level campaign, ad group, keyword,
* or geo targeting settings.
*/
public class MigrateToExtensionSettings {
private static final int PAGE_SIZE = 100;
// See the Placeholder reference page for a list of all the placeholder types and fields.
// https://developers.google.com/adwords/api/docs/appendix/placeholders
private static final int PLACEHOLDER_SITELINKS = 1;
// See the Placeholder reference page for a list of all the placeholder types and fields.
// https://developers.google.com/adwords/api/docs/appendix/placeholders
private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1;
private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2;
private static final int PLACEHOLDER_FIELD_LINE_2_TEXT = 3;
private static final int PLACEHOLDER_FIELD_LINE_3_TEXT = 4;
private static final int PLACEHOLDER_FIELD_FINAL_URLS = 5;
private static final int PLACEHOLDER_FIELD_FINAL_MOBILE_URLS = 6;
private static final int PLACEHOLDER_FIELD_TRACKING_URL_TEMPLATE = 7;
public static void main(String[] args) {
AdWordsSession session;
try {
// Generate a refreshable OAuth2 credential.
Credential oAuth2Credential =
new OfflineCredentials.Builder()
.forApi(Api.ADWORDS)
.fromFile()
.build()
.generateCredential();
// Construct an AdWordsSession.
session =
new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
} catch (ConfigurationLoadException cle) {
System.err.printf(
"Failed to load configuration from the %s file. Exception: %s%n",
DEFAULT_CONFIGURATION_FILENAME, cle);
return;
} catch (ValidationException ve) {
System.err.printf(
"Invalid configuration in the %s file. Exception: %s%n",
DEFAULT_CONFIGURATION_FILENAME, ve);
return;
} catch (OAuthException oe) {
System.err.printf(
"Failed to create OAuth credentials. Check OAuth settings in the %s file. "
+ "Exception: %s%n",
DEFAULT_CONFIGURATION_FILENAME, oe);
return;
}
AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
try {
runExample(adWordsServices, session);
} catch (ApiException apiException) {
// ApiException is the base class for most exceptions thrown by an API request. Instances
// of this exception have a message and a collection of ApiErrors that indicate the
// type and underlying cause of the exception. Every exception object in the adwords.axis
// packages will return a meaningful value from toString
//
// ApiException extends RemoteException, so this catch block must appear before the
// catch block for RemoteException.
System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
if (apiException.getErrors() != null) {
int i = 0;
for (ApiError apiError : apiException.getErrors()) {
System.err.printf(" Error %d: %s%n", i++, apiError);
}
}
} catch (RemoteException re) {
System.err.printf(
"Request failed unexpectedly due to RemoteException: %s%n", re);
}
}
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session)
throws RemoteException {
// Get all of the feeds for the session's account.
List<Feed> feeds = getFeeds(adWordsServices, session);
for (Feed feed : feeds) {
// Retrieve all the sitelinks from the current feed.
Map<Long, SiteLinkFromFeed> feedItems = getSiteLinksFromFeed(adWordsServices, session, feed);
// Get all the instances where a sitelink from this feed has been added to a campaign.
List<CampaignFeed> campaignFeeds =
getCampaignFeeds(adWordsServices, session, feed, PLACEHOLDER_SITELINKS);
Set<Long> allFeedItemsToDelete = Sets.newHashSet();
for (CampaignFeed campaignFeed : campaignFeeds) {
// Retrieve the sitelinks that have been associated with this campaign.
Set<Long> feedItemIds = getFeedItemIdsForCampaign(campaignFeed);
ExtensionSettingPlatform platformRestrictions =
getPlatformRestictionsForCampaign(campaignFeed);
if (feedItemIds.isEmpty()) {
System.out.printf("Migration skipped for campaign feed with campaign ID %d "
+ "and feed ID %d because no mapped feed item IDs were found in the "
+ "campaign feed's matching function.%n", campaignFeed.getCampaignId(),
campaignFeed.getFeedId());
} else {
// Delete the campaign feed that associates the sitelinks from the feed to the campaign.
deleteCampaignFeed(adWordsServices, session, campaignFeed);
// Create extension settings instead of sitelinks.
createExtensionSetting(adWordsServices,
session,
feedItems,
campaignFeed,
feedItemIds,
platformRestrictions);
// Mark the sitelinks from the feed for deletion.
allFeedItemsToDelete.addAll(feedItemIds);
}
}
// Delete all the sitelinks from the feed.
deleteOldFeedItems(adWordsServices, session, allFeedItemsToDelete, feed);
}
}
/**
* Gets the site links from a feed.
*
* @return a map of feed item ID to SiteLinkFromFeed
*/
private static Map<Long, SiteLinkFromFeed> getSiteLinksFromFeed(
AdWordsServicesInterface adWordsServices, AdWordsSession session, Feed feed)
throws RemoteException {
// Retrieve the feed's attribute mapping.
Multimap<Long, Integer> feedMappings =
getFeedMapping(adWordsServices, session, feed, PLACEHOLDER_SITELINKS);
Map<Long, SiteLinkFromFeed> feedItems = Maps.newHashMap();
for (FeedItem feedItem : getFeedItems(adWordsServices, session, feed)) {
SiteLinkFromFeed siteLinkFromFeed = new SiteLinkFromFeed();
for (FeedItemAttributeValue attributeValue : feedItem.getAttributeValues()) {
// Skip this attribute if it hasn't been mapped to a field.
if (!feedMappings.containsKey(attributeValue.getFeedAttributeId())) {
continue;
}
for (Integer fieldId : feedMappings.get(attributeValue.getFeedAttributeId())) {
switch (fieldId) {
case PLACEHOLDER_FIELD_SITELINK_LINK_TEXT:
siteLinkFromFeed.text = attributeValue.getStringValue();
break;
case PLACEHOLDER_FIELD_SITELINK_URL:
siteLinkFromFeed.url = attributeValue.getStringValue();
break;
case PLACEHOLDER_FIELD_FINAL_URLS:
siteLinkFromFeed.finalUrls = attributeValue.getStringValues();
break;
case PLACEHOLDER_FIELD_FINAL_MOBILE_URLS:
siteLinkFromFeed.finalMobileUrls = attributeValue.getStringValues();
break;
case PLACEHOLDER_FIELD_TRACKING_URL_TEMPLATE:
siteLinkFromFeed.trackingUrlTemplate = attributeValue.getStringValue();
break;
case PLACEHOLDER_FIELD_LINE_2_TEXT:
siteLinkFromFeed.line2 = attributeValue.getStringValue();
break;
case PLACEHOLDER_FIELD_LINE_3_TEXT:
siteLinkFromFeed.line3 = attributeValue.getStringValue();
break;
default:
// Ignore attributes that do not map to a predefined placeholder field.
break;
}
}
}
feedItems.put(feedItem.getFeedItemId(), siteLinkFromFeed);
}
return feedItems;
}
/**
* Gets the feed mapping for a feed.
*
* @return a multimap from feed attribute ID to the set of field IDs mapped to the attribute
*/
private static Multimap<Long, Integer> getFeedMapping(AdWordsServicesInterface adWordsServices,
AdWordsSession session, Feed feed, long placeholderType) throws RemoteException {
// Get the FeedMappingService.
FeedMappingServiceInterface feedMappingService =
adWordsServices.get(session, FeedMappingServiceInterface.class);
String query = String.format(
"SELECT FeedMappingId, AttributeFieldMappings WHERE FeedId = %d and PlaceholderType = %d "
+ "AND Status = 'ENABLED'", feed.getId(), placeholderType);
Multimap<Long, Integer> attributeMappings = HashMultimap.create();
int offset = 0;
FeedMappingPage feedMappingPage;
do {
String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
feedMappingPage = feedMappingService.query(pageQuery);
if (feedMappingPage.getEntries() != null) {
// Normally, a feed attribute is mapped only to one field. However, you may map it to more
// than one field if needed.
for (FeedMapping feedMapping : feedMappingPage.getEntries()) {
for (AttributeFieldMapping attributeMapping : feedMapping.getAttributeFieldMappings()) {
attributeMappings.put(attributeMapping.getFeedAttributeId(),
attributeMapping.getFieldId());
}
}
}
offset += PAGE_SIZE;
} while (offset < feedMappingPage.getTotalNumEntries());
return attributeMappings;
}
/** Returns a list of all enabled feeds. */
private static List<Feed> getFeeds(
AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException {
FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
String query = "SELECT Id, Name, Attributes WHERE Origin = 'USER' AND FeedStatus = 'ENABLED'";
List<Feed> feeds = new ArrayList<>();
int offset = 0;
FeedPage feedPage;
do {
String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
feedPage = feedService.query(pageQuery);
if (feedPage.getEntries() != null) {
feeds.addAll(Arrays.asList(feedPage.getEntries()));
}
offset += PAGE_SIZE;
} while (offset < feedPage.getTotalNumEntries());
return feeds;
}
/**
* Returns the feed items for a feed.
*/
private static List<FeedItem> getFeedItems(AdWordsServicesInterface adWordsServices,
AdWordsSession session, Feed feed) throws RemoteException {
// Get the FeedItemService.
FeedItemServiceInterface feedItemService =
adWordsServices.get(session, FeedItemServiceInterface.class);
String query = String.format(
"SELECT FeedItemId, AttributeValues WHERE Status = 'ENABLED' AND FeedId = %d",
feed.getId());
List<FeedItem> feedItems = new ArrayList<>();
int offset = 0;
FeedItemPage feedItemPage;
do {
String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
feedItemPage = feedItemService.query(pageQuery);
if (feedItemPage.getEntries() != null) {
feedItems.addAll(Arrays.asList(feedItemPage.getEntries()));
}
offset += PAGE_SIZE;
} while (offset < feedItemPage.getTotalNumEntries());
return feedItems;
}
/** Deletes the old feed items for which extension settings have been created. */
private static void deleteOldFeedItems(
AdWordsServicesInterface adWordsServices,
AdWordsSession session,
Set<Long> feedItemIds,
Feed feed)
throws RemoteException {
// Get the FeedItemService.
FeedItemServiceInterface feedItemService =
adWordsServices.get(session, FeedItemServiceInterface.class);
if (feedItemIds.isEmpty()) {
return;
}
List<FeedItemOperation> operations = new ArrayList<>();
for (Long feedItemId : feedItemIds) {
FeedItemOperation operation = new FeedItemOperation();
FeedItem feedItem = new FeedItem();
feedItem.setFeedId(feed.getId());
feedItem.setFeedItemId(feedItemId);
operation.setOperand(feedItem);
operation.setOperator(Operator.REMOVE);
operations.add(operation);
}
feedItemService.mutate(operations.toArray(new FeedItemOperation[operations.size()]));
}
/**
* Creates the extension setting for a list of feed items.
*
* @param adWordsServices the AdWordsServices
* @param session the AdWordsSession
* @param feedItems the list of all feed items
* @param campaignFeed the original campaign feed
* @param feedItemIds IDs of the feed items for which extension settings should be created
* @param platformRestrictions the platform restrictions for the new campaign extension setting
*/
private static void createExtensionSetting(AdWordsServicesInterface adWordsServices,
AdWordsSession session,
Map<Long, SiteLinkFromFeed> feedItems,
CampaignFeed campaignFeed,
Set<Long> feedItemIds,
ExtensionSettingPlatform platformRestrictions) throws RemoteException {
// Get the CampaignExtensionSettingService.
CampaignExtensionSettingServiceInterface campaignExtensionSettingService =
adWordsServices.get(session, CampaignExtensionSettingServiceInterface.class);
CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting();
campaignExtensionSetting.setCampaignId(campaignFeed.getCampaignId());
campaignExtensionSetting.setExtensionType(FeedType.SITELINK);
ExtensionSetting extensionSetting = new ExtensionSetting();
List<ExtensionFeedItem> extensionFeedItems = new ArrayList<>();
for (Long feedItemId : feedItemIds) {
SiteLinkFromFeed siteLinkFromFeed = feedItems.get(feedItemId);
SitelinkFeedItem siteLinkFeedItem = new SitelinkFeedItem();
siteLinkFeedItem.setSitelinkText(siteLinkFromFeed.text);
if (siteLinkFromFeed.finalUrls != null && siteLinkFromFeed.finalUrls.length > 0) {
siteLinkFeedItem.setSitelinkFinalUrls(new UrlList(siteLinkFromFeed.finalUrls));
if (siteLinkFromFeed.finalMobileUrls != null
&& siteLinkFromFeed.finalMobileUrls.length > 0) {
siteLinkFeedItem.setSitelinkFinalMobileUrls(
new UrlList(siteLinkFromFeed.finalMobileUrls));
}
siteLinkFeedItem.setSitelinkTrackingUrlTemplate(siteLinkFromFeed.trackingUrlTemplate);
} else {
siteLinkFeedItem.setSitelinkUrl(siteLinkFromFeed.url);
}
siteLinkFeedItem.setSitelinkLine2(siteLinkFromFeed.line2);
siteLinkFeedItem.setSitelinkLine3(siteLinkFromFeed.line3);
extensionFeedItems.add(siteLinkFeedItem);
}
extensionSetting.setExtensions(
extensionFeedItems.toArray(new ExtensionFeedItem[extensionFeedItems.size()]));
extensionSetting.setPlatformRestrictions(platformRestrictions);
campaignExtensionSetting.setExtensionSetting(extensionSetting);
CampaignExtensionSettingOperation operation = new CampaignExtensionSettingOperation();
operation.setOperand(campaignExtensionSetting);
operation.setOperator(Operator.ADD);
campaignExtensionSettingService.mutate(new CampaignExtensionSettingOperation[] {operation});
}
/**
* Deletes a campaign feed.
*/
private static CampaignFeed deleteCampaignFeed(AdWordsServicesInterface adWordsServices,
AdWordsSession session, CampaignFeed campaignFeed) throws RemoteException {
// Get the CampaignFeedService.
CampaignFeedServiceInterface campaignFeedService =
adWordsServices.get(session, CampaignFeedServiceInterface.class);
CampaignFeedOperation operation = new CampaignFeedOperation();
operation.setOperand(campaignFeed);
operation.setOperator(Operator.REMOVE);
return campaignFeedService.mutate(new CampaignFeedOperation[] {operation}).getValue(0);
}
/**
* Gets the platform restrictions for sitelinks in a campaign.
*/
private static ExtensionSettingPlatform getPlatformRestictionsForCampaign(
CampaignFeed campaignFeed) {
String platformRestrictions = ExtensionSettingPlatform.NONE.getValue();
if (FunctionOperator.AND.equals(campaignFeed.getMatchingFunction().getOperator())) {
for (FunctionArgumentOperand argument : campaignFeed.getMatchingFunction().getLhsOperand()) {
if (argument instanceof FunctionOperand) {
FunctionOperand operand = (FunctionOperand) argument;
if (FunctionOperator.EQUALS.equals(operand.getValue().getOperator())
&& (operand.getValue().getLhsOperand(0) instanceof RequestContextOperand)) {
RequestContextOperand requestContextOperand =
(RequestContextOperand) operand.getValue().getLhsOperand(0);
if (RequestContextOperandContextType.DEVICE_PLATFORM.equals(
requestContextOperand.getContextType())) {
platformRestrictions =
((ConstantOperand) operand.getValue().getRhsOperand(0)).getStringValue();
}
}
}
}
}
return ExtensionSettingPlatform.fromString(platformRestrictions.toUpperCase());
}
/**
* Returns the list of feed item IDs that are used by a campaign through a given campaign feed.
*/
private static Set<Long> getFeedItemIdsForCampaign(CampaignFeed campaignFeed)
throws RemoteException {
Set<Long> feedItemIds = Sets.newHashSet();
FunctionOperator functionOperator = campaignFeed.getMatchingFunction().getOperator();
if (FunctionOperator.IN.equals(functionOperator)) {
// Check if matchingFunction is of the form IN(FEED_ITEM_ID,{xxx,xxx}).
// Extract feed items if applicable.
feedItemIds.addAll(getFeedItemIdsFromArgument(campaignFeed.getMatchingFunction()));
} else if (FunctionOperator.AND.equals(functionOperator)) {
// Check if matchingFunction is of the form IN(FEED_ITEM_ID,{xxx,xxx}).
// Extract feed items if applicable.
Arrays.stream(campaignFeed.getMatchingFunction().getLhsOperand())
.filter(FunctionOperand.class::isInstance)
.map(argument -> (FunctionOperand) argument)
.filter(operand -> FunctionOperator.IN.equals(operand.getValue().getOperator()))
.forEach(operand -> feedItemIds.addAll(getFeedItemIdsFromArgument(operand.getValue())));
} else {
// There are no other matching functions involving feed item IDs.
}
return feedItemIds;
}
/**
* Gets the set of feed item IDs from the function if it is of the form:
* <code>IN(FEED_ITEM_ID,{xxx,xxx})</code>. Otherwise, returns an empty set.
*/
private static Set<Long> getFeedItemIdsFromArgument(Function function) {
if (function.getLhsOperand().length == 1
&& function.getLhsOperand(0) instanceof RequestContextOperand) {
RequestContextOperand requestContextOperand =
(RequestContextOperand) function.getLhsOperand(0);
if (RequestContextOperandContextType.FEED_ITEM_ID.equals(
requestContextOperand.getContextType())
&& FunctionOperator.IN.equals(function.getOperator())) {
return Arrays.stream(function.getRhsOperand())
.filter(ConstantOperand.class::isInstance)
.map(argument -> ((ConstantOperand) argument).getLongValue())
.collect(Collectors.toSet());
}
}
return new HashSet<>();
}
/**
* Returns the campaign feeds that use a particular feed for a particular placeholder type.
*/
private static List<CampaignFeed> getCampaignFeeds(AdWordsServicesInterface adWordsServices,
AdWordsSession session, Feed feed, int placeholderType) throws RemoteException {
// Get the CampaignFeedService.
CampaignFeedServiceInterface campaignFeedService =
adWordsServices.get(session, CampaignFeedServiceInterface.class);
String query = String.format(
"SELECT CampaignId, MatchingFunction, PlaceholderTypes WHERE Status = 'ENABLED' "
+ "AND FeedId = %d AND PlaceholderTypes CONTAINS_ANY [%d]", feed.getId(), placeholderType);
List<CampaignFeed> campaignFeeds = new ArrayList<>();
int offset = 0;
CampaignFeedPage campaignFeedPage;
do {
String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
campaignFeedPage = campaignFeedService.query(pageQuery);
if (campaignFeedPage.getEntries() != null) {
campaignFeeds.addAll(Arrays.asList(campaignFeedPage.getEntries()));
}
offset += PAGE_SIZE;
} while (offset < campaignFeedPage.getTotalNumEntries());
return campaignFeeds;
}
/**
* A sitelink object read from a feed item.
*/
private static class SiteLinkFromFeed {
private String text;
private String url;
private String[] finalUrls;
private String[] finalMobileUrls;
private String trackingUrlTemplate;
private String line2;
private String line3;
}
}