use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion 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.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapterTest method testCommonAttributes.
/**
* Asserts that the attributes of {@code adGroupCriterion} match expectations.
*
* @param node the node from which the criterion was built
* @param adGroupCriterion the criterion created by {@link ProductPartitionNodeAdapter}
* @param isForRemove if true, this method will only check the attributes required for a REMOVE
* operation
*/
private void testCommonAttributes(ProductPartitionNode node, AdGroupCriterion adGroupCriterion, boolean isForRemove) {
assertEquals("Ad group ID is incorrect", adGroupId, adGroupCriterion.getAdGroupId());
Criterion criterion = adGroupCriterion.getCriterion();
assertTrue("Criterion should be a ProductPartition", criterion instanceof ProductPartition);
assertEquals("Partition ID is incorrect", node.getProductPartitionId(), criterion.getId());
if (isForRemove) {
assertEquals("Type of AdGroupCriterion for REMOVE should be the base class", AdGroupCriterion.class, adGroupCriterion.getClass());
// The above checks suffice for REMOVE operations.
return;
}
ProductPartition partition = (ProductPartition) criterion;
assertEquals("The caseValue of the partition does not match the dimension of the node", 0, new ProductDimensionComparator().compare(partition.getCaseValue(), node.getDimension()));
if (node.getParent() == null) {
assertNull("Parent ID should be null", partition.getParentCriterionId());
} else {
assertEquals("Parent ID does not match ID of parent node", node.getParent().getProductPartitionId(), partition.getParentCriterionId());
}
if (node.isBiddableUnit()) {
assertTrue("Biddable node should be translated into a BiddableAdGroupCriterion", adGroupCriterion instanceof BiddableAdGroupCriterion);
BiddableAdGroupCriterion biddableCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
BiddingStrategyConfiguration biddingStrategyConfig = biddableCriterion.getBiddingStrategyConfiguration();
if (node.getBid() == null) {
assertArrayEquals(new Bids[0], biddingStrategyConfig.getBids());
} else {
Bids bid = biddingStrategyConfig.getBids(0);
assertTrue("Bid should be a CpcBid", bid instanceof CpcBid);
CpcBid cpcBid = (CpcBid) bid;
assertEquals("Bid amount is incorrect", node.getBid(), cpcBid.getBid().getMicroAmount());
assertEquals("Partition is not a UNIT partition", ProductPartitionType.UNIT, partition.getPartitionType());
}
assertEquals("tracking URL template is incorrect", node.getTrackingUrlTemplate(), biddableCriterion.getTrackingUrlTemplate());
// The adapter should always have a CustomParameters object, even if the node had no params.
// This ensures that the parameters will be cleared (via doReplace=true) if all params were
// removed from the node.
CustomParameters customParameters = biddableCriterion.getUrlCustomParameters();
assertNotNull("Biddable criterion does not have custom parameters", customParameters);
assertEquals("doReplace for custom parameters should always be true", true, customParameters.getDoReplace());
// Convert the BiddableAdGroupCriterion's custom parameters to a map to simplify comparison
// against the node's custom parameter map.
Map<String, String> actualCustomParameters = new HashMap<>();
for (CustomParameter customParameter : customParameters.getParameters()) {
actualCustomParameters.put(customParameter.getKey(), customParameter.getValue());
}
assertEquals("node and criterion do not have the same custom parameters", node.getCustomParameters(), actualCustomParameters);
} else {
assertTrue("Excluded node should be translated into a NegativeAdGroupCriterion", adGroupCriterion instanceof NegativeAdGroupCriterion);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapter method createCriterionForRemove.
/**
* Returns a new AdGroupCriterion configured for a REMOVE operation.
*
* @param node the node whose criterion should be removed
* @param adGroupId the ad group ID of the criterion
*/
static AdGroupCriterion createCriterionForRemove(ProductPartitionNode node, long adGroupId) {
Preconditions.checkNotNull(node, "Null node");
AdGroupCriterion adGroupCriterion = new AdGroupCriterion();
adGroupCriterion.setAdGroupId(adGroupId);
adGroupCriterion.setCriterion(new ProductPartition());
adGroupCriterion.getCriterion().setId(node.getProductPartitionId());
return adGroupCriterion;
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapter method createCriterionForSetBiddableUnit.
/**
* Returns a new AdGroupCriterion configured for a SET operation that will set the criterion's
* bid, tracking template, and custom parameters.
*
* @param node the node whose criterion should be updated
* @param adGroupId the ad group ID of the criterion
* @param biddingConfig the bidding strategy configuration of the criterion
*/
static AdGroupCriterion createCriterionForSetBiddableUnit(ProductPartitionNode node, long adGroupId, BiddingStrategyConfiguration biddingConfig) {
Preconditions.checkNotNull(node, "Null node");
Preconditions.checkNotNull(biddingConfig, "Null bidding configuration");
Preconditions.checkArgument(node.isBiddableUnit(), "Node is not a biddable unit");
BiddableAdGroupCriterion biddableCriterion = new BiddableAdGroupCriterion();
biddableCriterion.setAdGroupId(adGroupId);
ProductPartition partition = new ProductPartition();
partition.setId(node.getProductPartitionId());
if (node.getParent() != null) {
partition.setParentCriterionId(node.getParent().getProductPartitionId());
}
partition.setCaseValue(node.getDimension());
partition.setPartitionType(ProductPartitionType.UNIT);
biddableCriterion.setCriterion(partition);
// Set the bidding attributes on the new ad group criterion.
if (node.getBid() != null) {
Money bidMoney = new Money();
bidMoney.setMicroAmount(node.getBid());
CpcBid cpcBid = new CpcBid();
cpcBid.setBid(bidMoney);
biddingConfig.setBids(new Bids[] { cpcBid });
} else {
biddingConfig.setBids(new Bids[0]);
}
biddableCriterion.setBiddingStrategyConfiguration(biddingConfig);
// Set the upgraded URL attributes on the new ad group criterion.
if (node.getTrackingUrlTemplate() != null) {
biddableCriterion.setTrackingUrlTemplate(node.getTrackingUrlTemplate());
}
biddableCriterion.setUrlCustomParameters(createCustomParameters(node));
return biddableCriterion;
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapter method createCriterionForAdd.
/**
* Returns a new AdGroupCriterion configured for an ADD operation.
*
* @param node the node whose criterion should be added
* @param adGroupId the ad group ID of the criterion
* @param biddingConfig the bidding strategy configuration of the criterion
*/
static AdGroupCriterion createCriterionForAdd(ProductPartitionNode node, long adGroupId, BiddingStrategyConfiguration biddingConfig) {
Preconditions.checkNotNull(node, "Null node");
Preconditions.checkNotNull(biddingConfig, "Null bidding configuration");
AdGroupCriterion adGroupCriterion;
if (node.isExcludedUnit()) {
adGroupCriterion = new NegativeAdGroupCriterion();
} else if (node.isBiddableUnit()) {
BiddableAdGroupCriterion biddableCriterion = new BiddableAdGroupCriterion();
if (node.getBid() != null) {
Money bidMoney = new Money();
bidMoney.setMicroAmount(node.getBid());
CpcBid cpcBid = new CpcBid();
cpcBid.setBid(bidMoney);
cpcBid.setCpcBidSource(BidSource.CRITERION);
biddingConfig.setBids(new Bids[] { cpcBid });
biddableCriterion.setBiddingStrategyConfiguration(biddingConfig);
}
if (node.getTrackingUrlTemplate() != null) {
biddableCriterion.setTrackingUrlTemplate(node.getTrackingUrlTemplate());
}
biddableCriterion.setUrlCustomParameters(createCustomParameters(node));
adGroupCriterion = biddableCriterion;
} else {
adGroupCriterion = new BiddableAdGroupCriterion();
}
adGroupCriterion.setAdGroupId(adGroupId);
ProductPartition partition = new ProductPartition();
partition.setId(node.getProductPartitionId());
if (node.getParent() != null) {
partition.setParentCriterionId(node.getParent().getProductPartitionId());
}
partition.setCaseValue(node.getDimension());
partition.setPartitionType(node.isUnit() ? ProductPartitionType.UNIT : ProductPartitionType.SUBDIVISION);
adGroupCriterion.setCriterion(partition);
return adGroupCriterion;
}
Aggregations