Search in sources :

Example 1 with BudgetOperation

use of com.google.api.ads.adwords.jaxws.v201809.cm.BudgetOperation 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)

Example 2 with BudgetOperation

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

the class AddCompleteCampaignsUsingBatchJob method buildBudgetOperation.

private static BudgetOperation buildBudgetOperation(Iterator<Long> tempIdGenerator, String namePrefix) {
    Budget budget = new Budget();
    budget.setBudgetId(tempIdGenerator.next());
    budget.setName(String.format("Interplanetary Cruise %s", namePrefix));
    Money budgetAmount = new Money();
    budgetAmount.setMicroAmount(50000000L);
    budget.setAmount(budgetAmount);
    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    BudgetOperation budgetOperation = new BudgetOperation();
    budgetOperation.setOperand(budget);
    budgetOperation.setOperator(Operator.ADD);
    return budgetOperation;
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) BudgetOperation(com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget)

Example 3 with BudgetOperation

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

the class GraduateTrial method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param trialId the ID of the trial to graduate.
 * @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 trialId) throws RemoteException {
    // Get the TrialService and BudgetService.
    TrialServiceInterface trialService = adWordsServices.get(session, TrialServiceInterface.class);
    BudgetServiceInterface budgetService = adWordsServices.get(session, BudgetServiceInterface.class);
    // To graduate a trial, you must specify a different budget from the base campaign. The base
    // campaign (in order to have had a trial based on it) must have a non-shared budget, so it
    // cannot be shared with the new independent campaign created by graduation.
    Budget budget = new Budget();
    budget.setName("Budget #" + System.currentTimeMillis());
    Money budgetAmount = new Money();
    budgetAmount.setMicroAmount(50000000L);
    budget.setAmount(budgetAmount);
    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    BudgetOperation budgetOperation = new BudgetOperation();
    budgetOperation.setOperator(Operator.ADD);
    budgetOperation.setOperand(budget);
    // Add budget.
    long budgetId = budgetService.mutate(new BudgetOperation[] { budgetOperation }).getValue(0).getBudgetId();
    Trial trial = new Trial();
    trial.setId(trialId);
    trial.setBudgetId(budgetId);
    trial.setStatus(TrialStatus.GRADUATED);
    TrialOperation trialOperation = new TrialOperation();
    trialOperation.setOperator(Operator.SET);
    trialOperation.setOperand(trial);
    // Update the trial.
    trial = trialService.mutate(new TrialOperation[] { trialOperation }).getValue(0);
    // Graduation is a synchronous operation, so the campaign is already ready. If you promote
    // instead, make sure to see the polling scheme demonstrated in AddTrial.java to wait for the
    // asynchronous operation to finish.
    System.out.printf("Trial ID %d graduated. Campaign ID %d was given a new budget ID %d and " + "is no longer dependent on this trial.%n", trial.getId(), trial.getTrialCampaignId(), budgetId);
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) TrialServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.TrialServiceInterface) Trial(com.google.api.ads.adwords.axis.v201809.cm.Trial) BudgetOperation(com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation) BudgetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.BudgetServiceInterface) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget) TrialOperation(com.google.api.ads.adwords.axis.v201809.cm.TrialOperation)

Example 4 with BudgetOperation

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

the class AddDynamicSearchAdsCampaign method createBudget.

/**
 * Creates the budget.
 */
private static Budget createBudget(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException, ApiException {
    // Get the BudgetService.
    BudgetServiceInterface budgetService = adWordsServices.get(session, BudgetServiceInterface.class);
    // Create a budget, which can be shared by multiple campaigns.
    Budget sharedBudget = new Budget();
    sharedBudget.setName("Interplanetary Cruise #" + System.currentTimeMillis());
    Money budgetAmount = new Money();
    budgetAmount.setMicroAmount(50000000L);
    sharedBudget.setAmount(budgetAmount);
    sharedBudget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    BudgetOperation budgetOperation = new BudgetOperation();
    budgetOperation.setOperand(sharedBudget);
    budgetOperation.setOperator(Operator.ADD);
    // Add the budget
    Budget budget = budgetService.mutate(new BudgetOperation[] { budgetOperation }).getValue(0);
    return budget;
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) BudgetOperation(com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation) BudgetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.BudgetServiceInterface) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget)

Example 5 with BudgetOperation

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

the class AddUniversalAppCampaign method createBudget.

/**
 * Creates the budget for the campaign.
 *
 * @return the new budget.
 */
private static Long createBudget(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException, ApiException {
    // Get the BudgetService.
    BudgetServiceInterface budgetService = adWordsServices.get(session, BudgetServiceInterface.class);
    // Create the campaign budget.
    Budget budget = new Budget();
    budget.setName("Interplanetary Cruise App Budget #" + System.currentTimeMillis());
    Money budgetAmount = new Money();
    budgetAmount.setMicroAmount(50000000L);
    budget.setAmount(budgetAmount);
    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    // Universal app campaigns don't support shared budgets.
    budget.setIsExplicitlyShared(false);
    BudgetOperation budgetOperation = new BudgetOperation();
    budgetOperation.setOperand(budget);
    budgetOperation.setOperator(Operator.ADD);
    // Add the budget
    Budget addedBudget = budgetService.mutate(new BudgetOperation[] { budgetOperation }).getValue(0);
    System.out.printf("Budget with name '%s' and ID %d was created.%n", addedBudget.getName(), addedBudget.getBudgetId());
    return addedBudget.getBudgetId();
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) BudgetOperation(com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation) BudgetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.BudgetServiceInterface) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget)

Aggregations

BudgetOperation (com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation)11 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)10 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)10 BudgetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.BudgetServiceInterface)8 Budget (com.google.api.ads.adwords.jaxws.v201809.cm.Budget)4 BudgetOperation (com.google.api.ads.adwords.jaxws.v201809.cm.BudgetOperation)4 Money (com.google.api.ads.adwords.jaxws.v201809.cm.Money)4 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)4 MockHttpIntegrationTest (com.google.api.ads.common.lib.testing.MockHttpIntegrationTest)4 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)4 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 AdWordsServices (com.google.api.ads.adwords.jaxws.factory.AdWordsServices)3 BudgetServiceInterface (com.google.api.ads.adwords.jaxws.v201809.cm.BudgetServiceInterface)3 Test (org.junit.Test)3 Diff (org.xmlunit.diff.Diff)3 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)2 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)2 DiffBuilder (org.xmlunit.builder.DiffBuilder)2 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1