use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface in project googleads-java-lib by googleads.
the class AddDynamicSearchAdsCampaign method addWebPageCriteria.
/**
* Adds a web page criteria to target Dynamic Search Ads.
*/
private static void addWebPageCriteria(AdWordsServicesInterface adWordsServices, AdWordsSession session, AdGroup adGroup) throws ApiException, RemoteException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Create a webpage criterion for special offers.
WebpageParameter param = new WebpageParameter();
param.setCriterionName("Special offers");
WebpageCondition urlCondition = new WebpageCondition();
urlCondition.setOperand(WebpageConditionOperand.URL);
urlCondition.setArgument("/specialoffers");
WebpageCondition titleCondition = new WebpageCondition();
titleCondition.setOperand(WebpageConditionOperand.PAGE_TITLE);
titleCondition.setArgument("Special Offer");
param.setConditions(new WebpageCondition[] { urlCondition, titleCondition });
Webpage webpage = new Webpage();
webpage.setParameter(param);
// Create biddable ad group criterion.
BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
biddableAdGroupCriterion.setAdGroupId(adGroup.getId());
biddableAdGroupCriterion.setCriterion(webpage);
biddableAdGroupCriterion.setUserStatus(UserStatus.PAUSED);
// Optional: set a custom bid.
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
CpcBid bid = new CpcBid();
bid.setBid(new Money());
bid.getBid().setMicroAmount(10000000L);
biddingStrategyConfiguration.setBids(new Bids[] { bid });
biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
// Create operations.
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
operation.setOperator(Operator.ADD);
operation.setOperand(biddableAdGroupCriterion);
// Create the criterion.
AdGroupCriterion newAdGroupCriterion = adGroupCriterionService.mutate(new AdGroupCriterionOperation[] { operation }).getValue(0);
System.out.printf("Webpage criterion with ID %d was added to ad group ID %d.%n", newAdGroupCriterion.getCriterion().getId(), newAdGroupCriterion.getAdGroupId());
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface in project googleads-java-lib by googleads.
the class AddShoppingDynamicRemarketingCampaign method attachUserList.
/**
* Attach a user list to an ad group. The user list provides positive targeting and feed
* information to drive the dynamic content of the ad.
*
* <p>Note: User lists must be attached at the ad group level for positive targeting in Shopping
* dynamic remarketing campaigns.
*
* @param adGroup the ad group which will have the user list attached.
* @param userListId the user list to use for targeting and dynamic content.
*/
private static void attachUserList(AdWordsServicesInterface services, AdWordsSession session, AdGroup adGroup, long userListId) throws RemoteException {
AdGroupCriterionServiceInterface adGroupCriterionService = services.get(session, AdGroupCriterionServiceInterface.class);
CriterionUserList userList = new CriterionUserList();
userList.setUserListId(userListId);
BiddableAdGroupCriterion adGroupCriterion = new BiddableAdGroupCriterion();
adGroupCriterion.setCriterion(userList);
adGroupCriterion.setAdGroupId(adGroup.getId());
AdGroupCriterionOperation op = new AdGroupCriterionOperation();
op.setOperand(adGroupCriterion);
op.setOperator(Operator.ADD);
adGroupCriterionService.mutate(new AdGroupCriterionOperation[] { op });
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface in project googleads-java-lib by googleads.
the class HandlePartialFailures method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group.
* @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, Long adGroupId) throws RemoteException {
// Enable partial failure.
session.setPartialFailure(true);
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
List<AdGroupCriterionOperation> operations = new ArrayList<>();
// Create keywords.
String[] keywords = new String[] { "mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise" };
for (String keywordText : keywords) {
// Create keyword
Keyword keyword = new Keyword();
keyword.setText(keywordText);
keyword.setMatchType(KeywordMatchType.BROAD);
// Create biddable ad group criterion.
BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
keywordBiddableAdGroupCriterion.setCriterion(keyword);
// Create operation.
AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation();
keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
keywordAdGroupCriterionOperation.setOperator(Operator.ADD);
operations.add(keywordAdGroupCriterionOperation);
}
// Add ad group criteria.
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.toArray(new AdGroupCriterionOperation[] {}));
// Display results.
Arrays.stream(result.getValue()).filter(adGroupCriterionResult -> adGroupCriterionResult.getCriterion() != null).forEach(adGroupCriterionResult -> System.out.printf("Ad group criterion with ad group ID %d, and criterion ID %d, " + "and keyword '%s' was added.%n", adGroupCriterionResult.getAdGroupId(), adGroupCriterionResult.getCriterion().getId(), ((Keyword) adGroupCriterionResult.getCriterion()).getText()));
for (ApiError apiError : result.getPartialFailureErrors()) {
// Get the index of the failed operation from the error's field path elements.
FieldPathElement[] fieldPathElements = apiError.getFieldPathElements();
FieldPathElement firstFieldPathElement = null;
if (fieldPathElements != null && fieldPathElements.length > 0) {
firstFieldPathElement = fieldPathElements[0];
}
if (firstFieldPathElement != null && "operations".equals(firstFieldPathElement.getField()) && firstFieldPathElement.getIndex() != null) {
int operationIndex = firstFieldPathElement.getIndex();
AdGroupCriterion adGroupCriterion = operations.get(operationIndex).getOperand();
System.out.printf("Ad group criterion with ad group ID %d and keyword '%s' " + "triggered a failure for the following reason: %s.%n", adGroupCriterion.getAdGroupId(), ((Keyword) adGroupCriterion.getCriterion()).getText(), apiError.getErrorString());
} else {
System.out.printf("A failure has occurred for the following reason: %s%n", apiError.getErrorString());
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface in project googleads-java-lib by googleads.
the class GetKeywords method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group to use to find keywords.
* @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, Long adGroupId) throws RemoteException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupCriterionField.Id, AdGroupCriterionField.CriteriaType, AdGroupCriterionField.KeywordMatchType, AdGroupCriterionField.KeywordText).orderAscBy(AdGroupCriterionField.KeywordText).offset(offset).limit(PAGE_SIZE).in(AdGroupCriterionField.AdGroupId, adGroupId.toString()).in(AdGroupCriterionField.CriteriaType, "KEYWORD").build();
while (morePages) {
// Get all ad group criteria.
AdGroupCriterionPage page = adGroupCriterionService.get(selector);
// Display ad group criteria.
if (page.getEntries() != null && page.getEntries().length > 0) {
// Display results.
Arrays.stream(page.getEntries()).map(adGroupCriterionResult -> (Keyword) adGroupCriterionResult.getCriterion()).forEach(keyword -> System.out.printf("Keyword with text '%s', match type '%s', criteria type '%s'," + " and ID %d was found.%n", keyword.getText(), keyword.getMatchType(), keyword.getType(), keyword.getId()));
} else {
System.out.println("No ad group criteria were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface in project googleads-java-lib by googleads.
the class ProductPartitionTreeImpl method createAdGroupTree.
/**
* Returns a new instance of this class by retrieving the product partitions of the
* specified ad group. All parameters are required.
*/
static ProductPartitionTreeImpl createAdGroupTree(AdWordsServicesInterface services, AdWordsSession session, Long adGroupId) throws ApiException, RemoteException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface criterionService = services.get(session, AdGroupCriterionServiceInterface.class);
SelectorBuilder selectorBuilder = new SelectorBuilder().fields(REQUIRED_SELECTOR_FIELD_ENUMS.toArray(new AdGroupCriterionField[REQUIRED_SELECTOR_FIELD_ENUMS.size()])).equals(AdGroupCriterionField.AdGroupId, adGroupId.toString()).equals(AdGroupCriterionField.CriteriaType, "PRODUCT_PARTITION").in(AdGroupCriterionField.Status, UserStatus.ENABLED.getValue(), UserStatus.PAUSED.getValue()).limit(PAGE_SIZE);
AdGroupCriterionPage adGroupCriterionPage;
// A multimap from each product partition ID to its direct children.
ListMultimap<Long, AdGroupCriterion> parentIdMap = LinkedListMultimap.create();
int offset = 0;
do {
// Get the next page of results.
adGroupCriterionPage = criterionService.get(selectorBuilder.build());
if (adGroupCriterionPage != null && adGroupCriterionPage.getEntries() != null) {
for (AdGroupCriterion adGroupCriterion : adGroupCriterionPage.getEntries()) {
ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
parentIdMap.put(partition.getParentCriterionId(), adGroupCriterion);
}
offset += adGroupCriterionPage.getEntries().length;
selectorBuilder.increaseOffsetBy(PAGE_SIZE);
}
} while (offset < adGroupCriterionPage.getTotalNumEntries());
// Construct the ProductPartitionTree from the parentIdMap.
if (!parentIdMap.containsKey(null)) {
Preconditions.checkState(parentIdMap.isEmpty(), "No root criterion found in the tree but the tree is not empty");
return createEmptyAdGroupTree(adGroupId, getAdGroupBiddingStrategyConfiguration(services, session, adGroupId));
}
return createNonEmptyAdGroupTree(adGroupId, parentIdMap);
}
Aggregations