Search in sources :

Example 1 with Operation

use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.

the class ProductPartitionTreeTest method testCreateUltimatelyEmptyTree.

/**
 * Tests creating a tree that in its <em>final</em> state is just an empty tree.
 */
@Test
public void testCreateUltimatelyEmptyTree() {
    ProductPartitionTree tree = ProductPartitionTree.createAdGroupTree(-1L, biddingStrategyConfig, Collections.<AdGroupCriterion>emptyList());
    ProductPartitionNode rootNode = tree.getRoot().asSubdivision();
    ProductPartitionNode brand1 = rootNode.addChild(ProductDimensions.createBrand("google")).asSubdivision();
    brand1.addChild(ProductDimensions.createOfferId("A")).asBiddableUnit().setBid(1000000L);
    brand1.addChild(ProductDimensions.createOfferId(null)).asExcludedUnit();
    ProductPartitionNode brand2 = rootNode.addChild(ProductDimensions.createBrand(null)).asExcludedUnit();
    // Now remove the two child nodes under the root and set the root back to a UNIT. This should
    // result in operations that simply create the root node.
    rootNode.removeChild(brand1.getDimension());
    rootNode.removeChild(brand2.getDimension());
    rootNode = rootNode.asBiddableUnit();
    List<AdGroupCriterionOperation> mutateOperations = tree.getMutateOperations();
    assertEquals("Number of operations is incorrect", 1, mutateOperations.size());
    AdGroupCriterionOperation operation = mutateOperations.iterator().next();
    assertEquals("Should have a single operation to ADD the root node", Operator.ADD, operation.getOperator());
    BiddableAdGroupCriterion adGroupCriterion = (BiddableAdGroupCriterion) operation.getOperand();
    assertNull("Product dimension of operation's operand should be null", ((ProductPartition) adGroupCriterion.getCriterion()).getCaseValue());
    assertTrue("Partition ID of the operand should be negative", adGroupCriterion.getCriterion().getId().longValue() < 0L);
}
Also used : AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest) Test(org.junit.Test)

Example 2 with Operation

use of com.google.api.ads.adwords.axis.v201809.cm.Operation 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());
}
Also used : ProductBrand(com.google.api.ads.adwords.axis.v201809.cm.ProductBrand) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) NegativeAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) Map(java.util.Map) HashMap(java.util.HashMap) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest) Test(org.junit.Test)

Example 3 with Operation

use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.

the class ProductPartitionTreeTest method testCreateEmptyTree.

/**
 * Tests creating an empty ad group tree. In this case, all operations generated should be ADD
 * operations.
 */
@Test
public void testCreateEmptyTree() {
    ProductPartitionTree tree = ProductPartitionTree.createAdGroupTree(-1L, biddingStrategyConfig, Collections.<AdGroupCriterion>emptyList());
    assertNotNull("Even an empty tree should automatically have a root node", tree.getRoot());
    assertTrue("The root node for an empty tree should have a negative (temporary) ID", tree.getRoot().getProductPartitionId().longValue() < 0L);
    assertTrue("The root node for an empty tree should be a UNIT", tree.getRoot().isUnit());
    List<AdGroupCriterionOperation> mutateOperations = tree.getMutateOperations();
    assertEquals("Number of operations is incorrect", 1, mutateOperations.size());
    AdGroupCriterionOperation operation = mutateOperations.iterator().next();
    assertEquals("Should have a single operation to ADD the root node", Operator.ADD, operation.getOperator());
    BiddableAdGroupCriterion adGroupCriterion = (BiddableAdGroupCriterion) operation.getOperand();
    assertNull("Product dimension of operation's operand should be null", ((ProductPartition) adGroupCriterion.getCriterion()).getCaseValue());
    assertTrue("Partition ID of the operand should be negative", adGroupCriterion.getCriterion().getId().longValue() < 0L);
}
Also used : AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest) Test(org.junit.Test)

Example 4 with Operation

use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.

the class AddCompleteCampaignsUsingBatchJob method buildAdGroupAdOperations.

private static List<AdGroupAdOperation> buildAdGroupAdOperations(List<AdGroupOperation> adGroupOperations) {
    List<AdGroupAdOperation> operations = new ArrayList<>();
    for (AdGroupOperation adGroupOperation : adGroupOperations) {
        long adGroupId = adGroupOperation.getOperand().getId();
        AdGroupAd adGroupAd = new AdGroupAd();
        adGroupAd.setAdGroupId(adGroupId);
        ExpandedTextAd textAd = new ExpandedTextAd();
        textAd.setHeadlinePart1("Luxury Cruise to Mars");
        textAd.setHeadlinePart2("Visit the Red Planet in style.");
        textAd.setDescription("Low-gravity fun for everyone!");
        textAd.setFinalUrls(new String[] { "http://www.example.com/1" });
        adGroupAd.setAd(textAd);
        AdGroupAdOperation operation = new AdGroupAdOperation();
        operation.setOperator(Operator.ADD);
        operation.setOperand(adGroupAd);
        operations.add(operation);
    }
    return operations;
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) ArrayList(java.util.ArrayList) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Example 5 with Operation

use of com.google.api.ads.adwords.axis.v201809.cm.Operation in project googleads-java-lib by googleads.

the class AddCompleteCampaignsUsingBatchJob method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @throws BatchJobException if uploading operations or downloading results failed.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws InterruptedException if the thread was interrupted while sleeping between retries.
 * @throws TimeoutException if the job did not complete after job status was polled {@link
 *     #MAX_POLL_ATTEMPTS} times.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException, BatchJobException, InterruptedException, TimeoutException {
    // Get the MutateJobService.
    BatchJobServiceInterface batchJobService = adWordsServices.get(session, BatchJobServiceInterface.class);
    // Create a BatchJob.
    BatchJobOperation addOp = new BatchJobOperation();
    addOp.setOperator(Operator.ADD);
    addOp.setOperand(new BatchJob());
    BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).getValue(0);
    // Get the upload URL from the new job.
    String uploadUrl = batchJob.getUploadUrl().getUrl();
    System.out.printf("Created BatchJob with ID %d, status '%s' and upload URL %s.%n", batchJob.getId(), batchJob.getStatus(), uploadUrl);
    // Create a temporary ID generator that will produce a sequence of descending negative numbers.
    Iterator<Long> tempIdGenerator = new AbstractSequentialIterator<Long>(-1L) {

        @Override
        protected Long computeNext(Long previous) {
            return Long.MIN_VALUE == previous ? null : previous - 1;
        }
    };
    // Use a random UUID name prefix to avoid name collisions.
    String namePrefix = UUID.randomUUID().toString();
    // Create the mutate request that will be sent to the upload URL.
    List<Operation> operations = new ArrayList<>();
    // Create and add an operation to create a new budget.
    BudgetOperation budgetOperation = buildBudgetOperation(tempIdGenerator, namePrefix);
    operations.add(budgetOperation);
    // Create and add operations to create new campaigns.
    List<CampaignOperation> campaignOperations = buildCampaignOperations(tempIdGenerator, namePrefix, budgetOperation);
    operations.addAll(campaignOperations);
    // Create and add operations to create new negative keyword criteria for each campaign.
    operations.addAll(buildCampaignCriterionOperations(campaignOperations));
    // Create and add operations to create new ad groups.
    List<AdGroupOperation> adGroupOperations = new ArrayList<>(buildAdGroupOperations(tempIdGenerator, namePrefix, campaignOperations));
    operations.addAll(adGroupOperations);
    // Create and add operations to create new ad group criteria (keywords).
    operations.addAll(buildAdGroupCriterionOperations(adGroupOperations));
    // Create and add operations to create new ad group ads (text ads).
    operations.addAll(buildAdGroupAdOperations(adGroupOperations));
    // Use a BatchJobHelper to upload all operations.
    BatchJobHelper batchJobHelper = adWordsServices.getUtility(session, BatchJobHelper.class);
    batchJobHelper.uploadBatchJobOperations(operations, uploadUrl);
    System.out.printf("Uploaded %d operations for batch job with ID %d.%n", operations.size(), batchJob.getId());
    // Poll for completion of the batch job using an exponential back off.
    int pollAttempts = 0;
    boolean isPending;
    Selector selector = new SelectorBuilder().fields(BatchJobField.Id, BatchJobField.Status, BatchJobField.DownloadUrl, BatchJobField.ProcessingErrors, BatchJobField.ProgressStats).equalsId(batchJob.getId()).build();
    do {
        long sleepSeconds = (long) Math.scalb(30, pollAttempts);
        System.out.printf("Sleeping %d seconds...%n", sleepSeconds);
        Thread.sleep(sleepSeconds * 1000);
        batchJob = batchJobService.get(selector).getEntries(0);
        System.out.printf("Batch job ID %d has status '%s'.%n", batchJob.getId(), batchJob.getStatus());
        pollAttempts++;
        isPending = PENDING_STATUSES.contains(batchJob.getStatus());
    } while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
    if (isPending) {
        throw new TimeoutException("Job is still in pending state after polling " + MAX_POLL_ATTEMPTS + " times.");
    }
    if (batchJob.getProcessingErrors() != null) {
        int i = 0;
        for (BatchJobProcessingError processingError : batchJob.getProcessingErrors()) {
            System.out.printf("  Processing error [%d]: errorType=%s, trigger=%s, errorString=%s, fieldPath=%s" + ", reason=%s%n", i++, processingError.getApiErrorType(), processingError.getTrigger(), processingError.getErrorString(), processingError.getFieldPath(), processingError.getReason());
        }
    } else {
        System.out.println("No processing errors found.");
    }
    if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
        BatchJobMutateResponse mutateResponse = batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());
        System.out.printf("Downloaded results from %s:%n", batchJob.getDownloadUrl().getUrl());
        for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
            String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
            System.out.printf("  Operation [%d] - %s%n", mutateResult.getIndex(), outcome);
        }
    } else {
        System.out.println("No results available for download.");
    }
}
Also used : CampaignOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation) ArrayList(java.util.ArrayList) BatchJobServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.BatchJobServiceInterface) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation) BatchJobOperation(com.google.api.ads.adwords.axis.v201809.cm.BatchJobOperation) BudgetOperation(com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) Operation(com.google.api.ads.adwords.axis.v201809.cm.Operation) CampaignCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation) CampaignOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) BatchJobOperation(com.google.api.ads.adwords.axis.v201809.cm.BatchJobOperation) BatchJobMutateResponse(com.google.api.ads.adwords.axis.utils.v201809.batchjob.BatchJobMutateResponse) BatchJobHelper(com.google.api.ads.adwords.axis.utils.v201809.batchjob.BatchJobHelper) MutateResult(com.google.api.ads.adwords.axis.utils.v201809.batchjob.MutateResult) BatchJob(com.google.api.ads.adwords.axis.v201809.cm.BatchJob) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector) TimeoutException(java.util.concurrent.TimeoutException) BatchJobProcessingError(com.google.api.ads.adwords.axis.v201809.cm.BatchJobProcessingError) BudgetOperation(com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation) AbstractSequentialIterator(com.google.common.collect.AbstractSequentialIterator) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)

Aggregations

ArrayList (java.util.ArrayList)24 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)18 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)17 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)16 AdGroupCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation)15 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)14 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)14 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)13 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)13 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)12 BiddableAdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion)12 AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)11 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)11 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)10 CpcBid (com.google.api.ads.adwords.axis.v201809.cm.CpcBid)10 AdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion)9 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)8 AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)8 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)8 Keyword (com.google.api.ads.adwords.axis.v201809.cm.Keyword)8