Search in sources :

Example 1 with AdGroupOperation

use of com.google.api.ads.adwords.jaxws.v201809.cm.AdGroupOperation 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 2 with AdGroupOperation

use of com.google.api.ads.adwords.jaxws.v201809.cm.AdGroupOperation 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 3 with AdGroupOperation

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

the class AddCompleteCampaignsUsingBatchJob method buildAdGroupCriterionOperations.

private static List<AdGroupCriterionOperation> buildAdGroupCriterionOperations(List<AdGroupOperation> adGroupOperations) {
    List<AdGroupCriterionOperation> adGroupCriteriaOperations = new ArrayList<>();
    // Create AdGroupCriterionOperations to add keywords.
    for (AdGroupOperation adGroupOperation : adGroupOperations) {
        long newAdGroupId = adGroupOperation.getOperand().getId();
        for (int i = 0; i < NUMBER_OF_KEYWORDS_TO_ADD; i++) {
            // Create Keyword.
            String text = String.format("mars%d", i);
            // Make 50% of keywords invalid to demonstrate error handling.
            if (i % 2 == 0) {
                text = text + "!!!";
            }
            Keyword keyword = new Keyword();
            keyword.setText(text);
            keyword.setMatchType(KeywordMatchType.BROAD);
            // Create BiddableAdGroupCriterion.
            BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
            biddableAdGroupCriterion.setAdGroupId(newAdGroupId);
            biddableAdGroupCriterion.setCriterion(keyword);
            // Create AdGroupCriterionOperation.
            AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
            operation.setOperand(biddableAdGroupCriterion);
            operation.setOperator(Operator.ADD);
            // Add to list.
            adGroupCriteriaOperations.add(operation);
        }
    }
    return adGroupCriteriaOperations;
}
Also used : AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) ArrayList(java.util.ArrayList) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Example 4 with AdGroupOperation

use of com.google.api.ads.adwords.jaxws.v201809.cm.AdGroupOperation 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;
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) BiddingStrategyConfiguration(com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) CpcBid(com.google.api.ads.adwords.axis.v201809.cm.CpcBid) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Example 5 with AdGroupOperation

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

the class AddShoppingDynamicRemarketingCampaign method createAdGroup.

/**
 * Creates an ad group in the specified campaign.
 *
 * @param campaign the campaign to which the ad group should be attached.
 * @return the ad group that was created.
 */
private static AdGroup createAdGroup(AdWordsServicesInterface services, AdWordsSession session, Campaign campaign) throws RemoteException {
    AdGroupServiceInterface adGroupService = services.get(session, AdGroupServiceInterface.class);
    AdGroup group = new AdGroup();
    group.setName("Dynamic remarketing ad group");
    group.setCampaignId(campaign.getId());
    group.setStatus(AdGroupStatus.ENABLED);
    AdGroupOperation op = new AdGroupOperation();
    op.setOperand(group);
    op.setOperator(Operator.ADD);
    AdGroupReturnValue result = adGroupService.mutate(new AdGroupOperation[] { op });
    return result.getValue(0);
}
Also used : AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) AdGroupReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Aggregations

AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)12 AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)9 AdGroupServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface)7 AdGroupReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue)6 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)5 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)4 CpcBid (com.google.api.ads.adwords.axis.v201809.cm.CpcBid)4 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)4 ArrayList (java.util.ArrayList)4 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)3 AdGroupCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation)3 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)2 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)2 Operation (com.google.api.ads.adwords.axis.v201809.cm.Operation)2 InputStreamReader (java.io.InputStreamReader)2 Test (org.junit.Test)2 Diff (org.xmlunit.diff.Diff)2 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 BatchJobHelper (com.google.api.ads.adwords.axis.utils.v201809.batchjob.BatchJobHelper)1 BatchJobMutateRequest (com.google.api.ads.adwords.axis.utils.v201809.batchjob.BatchJobMutateRequest)1