use of com.google.api.ads.adwords.axis.v201809.cm.Bids in project googleads-java-lib by googleads.
the class ProductPartitionTreeTest method testMutateMultiNodeTree.
/**
* Tests mutating an existing tree with multiple nodes.
*/
@Test
public void testMutateMultiNodeTree() {
List<AdGroupCriterion> adGroupCriteria = Lists.newArrayList();
List<CriterionDescriptor> descriptors = Lists.newArrayList();
descriptors.add(new CriterionDescriptor(false, false, null, null, 1L, null));
ProductBrand brandGoogle = ProductDimensions.createBrand("google");
descriptors.add(new CriterionDescriptor(false, false, brandGoogle, null, 2L, 1L));
descriptors.add(new CriterionDescriptor(true, false, ProductDimensions.createOfferId("A"), 1000000L, 3L, 2L));
Long offerBOriginalPartitionId = 4L;
descriptors.add(new CriterionDescriptor(true, true, ProductDimensions.createOfferId("B"), null, offerBOriginalPartitionId, 2L));
Long brandOtherOriginalPartitionId = 5L;
descriptors.add(new CriterionDescriptor(true, true, ProductDimensions.createBrand(null), null, brandOtherOriginalPartitionId, 1L));
ProductBrand brandMotorola = ProductDimensions.createBrand("motorola");
Long brandMotorolaOriginalPartitionId = 6L;
descriptors.add(new CriterionDescriptor(true, true, brandMotorola, null, brandMotorolaOriginalPartitionId, 1L));
descriptors.forEach(descriptor -> adGroupCriteria.add(descriptor.createCriterion()));
Map<Long, Map<Long, CriterionDescriptor>> descriptorMap = buildDescriptorMap(descriptors);
ProductPartitionTree tree = ProductPartitionTree.createAdGroupTree(-1L, biddingStrategyConfig, adGroupCriteria);
assertEquals("ad group ID is incorrect", -1L, tree.getAdGroupId().longValue());
Queue<ProductPartitionNode> nodes = Lists.newLinkedList();
nodes.add(tree.getRoot());
int nodesFound = 0;
while (!nodes.isEmpty()) {
ProductPartitionNode node = nodes.remove();
Long parentId = node.getParent() == null ? null : node.getParent().getProductPartitionId();
CriterionDescriptor expectedDescriptor = descriptorMap.get(parentId).get(node.getProductPartitionId());
CriterionDescriptor actualDescriptor = new CriterionDescriptor(node);
expectedDescriptor.assertDescriptorEquals(actualDescriptor);
// Add children to process.
Iterables.addAll(nodes, node.getChildren());
nodesFound++;
}
assertEquals("Tree does not contain the expected # of nodes", adGroupCriteria.size(), nodesFound);
// Change the bids on leaf nodes.
ProductPartitionNode brandGoogleNode = tree.getRoot().getChild(brandGoogle);
ProductPartitionNode offerANode = brandGoogleNode.getChild(ProductDimensions.createOfferId("A"));
// This should produce 1 SET operation.
offerANode.setBid(offerANode.getBid() * 10);
// This should produce 1 REMOVE operation + 1 ADD operation.
ProductPartitionNode offerBNode = brandGoogleNode.getChild(ProductDimensions.createOfferId("B"));
offerBNode.asBiddableUnit().setBid(5000000L);
// This should produce 1 REMOVE operation + 1 ADD operation.
ProductPartitionNode brandOtherNode = tree.getRoot().getChild(ProductDimensions.createBrand(null));
brandOtherNode = brandOtherNode.asBiddableUnit();
// Add an offer C node. This should produce 1 ADD operation.
ProductPartitionNode offerCNode = brandGoogleNode.addChild(ProductDimensions.createOfferId("C")).asBiddableUnit().setBid(1500000L).putCustomParameter("param1", "value1");
// Remove the brand Motorola node. This should produce 1 REMOVE operation.
tree.getRoot().removeChild(brandMotorola);
// Get the mutate operations generated by the modifications made to the tree.
List<AdGroupCriterionOperation> mutateOperations = tree.getMutateOperations();
assertEquals(7, mutateOperations.size());
// Put the mutate operations in a map keyed by partition ID.
Map<Long, CriterionDescriptor> opsDescriptorMap = Maps.newHashMap();
int i = 0;
for (AdGroupCriterionOperation mutateOperation : mutateOperations) {
CriterionDescriptor descriptor = new CriterionDescriptor(mutateOperation.getOperand(), i++);
opsDescriptorMap.put(descriptor.partitionId, descriptor);
}
// Check the node that simply had a bid update.
int setOpNumber = opsDescriptorMap.get(offerANode.getProductPartitionId()).operationNumber;
assertEquals("Offer A node with a bid update should have a SET operation", Operator.SET, mutateOperations.get(setOpNumber).getOperator());
// Check the offer B node that went from excluded to biddable.
int addOfferBOpNumber = opsDescriptorMap.get(offerBNode.getProductPartitionId()).operationNumber;
assertEquals("Offer B node with a biddable change should have an ADD operation for the new ID", Operator.ADD, mutateOperations.get(addOfferBOpNumber).getOperator());
int removeOfferBOpNumber = opsDescriptorMap.get(offerBOriginalPartitionId).operationNumber;
assertEquals("Offer B node with a biddable change should have a REMOVE operation for the original ID", Operator.REMOVE, mutateOperations.get(removeOfferBOpNumber).getOperator());
// Check the offer C node that was added.
int addOfferCOpNumber = opsDescriptorMap.get(offerCNode.getProductPartitionId()).operationNumber;
assertEquals("New offer C node should have an ADD operation for the new ID", Operator.ADD, mutateOperations.get(addOfferCOpNumber).getOperator());
// Check the brand null node that went from excluded to biddable.
int addBrandOtherOpNumber = opsDescriptorMap.get(brandOtherNode.getProductPartitionId()).operationNumber;
assertEquals("Brand null node with a biddable change should have an ADD operation for the new ID", Operator.ADD, mutateOperations.get(addBrandOtherOpNumber).getOperator());
int brandOtherOpNumber = opsDescriptorMap.get(offerBOriginalPartitionId).operationNumber;
assertEquals("Brand null node with a biddable change should have a REMOVE operation for the original ID", Operator.REMOVE, mutateOperations.get(brandOtherOpNumber).getOperator());
// Check the brand Motorola node that was removed.
int brandMotorolaOpNumber = opsDescriptorMap.get(brandMotorolaOriginalPartitionId).operationNumber;
assertEquals("Removed node should have a REMOVE operation", Operator.REMOVE, mutateOperations.get(brandMotorolaOpNumber).getOperator());
}
use of com.google.api.ads.adwords.axis.v201809.cm.Bids 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.Bids in project googleads-java-lib by googleads.
the class AddDynamicSearchAdsCampaign method createAdGroup.
/**
* Creates the ad group.
*/
private static AdGroup createAdGroup(AdWordsServicesInterface adWordsServices, AdWordsSession session, Campaign campaign) throws ApiException, RemoteException {
// Get the AdGroupService.
AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
// Create the ad group.
AdGroup adGroup = new AdGroup();
// Required: Set the ad group's type to Dynamic Search Ads.
adGroup.setAdGroupType(AdGroupType.SEARCH_DYNAMIC_ADS);
adGroup.setName("Earth to Mars Cruises #" + System.currentTimeMillis());
adGroup.setCampaignId(campaign.getId());
adGroup.setStatus(AdGroupStatus.PAUSED);
// Recommended: Set a tracking URL template for your ad group if you want to use URL
// tracking software.
adGroup.setTrackingUrlTemplate("http://tracker.example.com/traveltracker/{escapedlpurl}");
// Set the ad group bids.
BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
CpcBid cpcBid = new CpcBid();
cpcBid.setBid(new Money());
cpcBid.getBid().setMicroAmount(3000000L);
biddingConfig.setBids(new Bids[] { cpcBid });
adGroup.setBiddingStrategyConfiguration(biddingConfig);
// Create the operation.
AdGroupOperation operation = new AdGroupOperation();
operation.setOperand(adGroup);
operation.setOperator(Operator.ADD);
AdGroup newAdGroup = adGroupService.mutate(new AdGroupOperation[] { operation }).getValue(0);
System.out.printf("Ad group with name '%s' and ID %d was added.%n", newAdGroup.getName(), newAdGroup.getId());
return newAdGroup;
}
use of com.google.api.ads.adwords.axis.v201809.cm.Bids 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.Bids 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;
}
Aggregations