Search in sources :

Example 6 with ProductPartition

use of com.google.api.ads.adwords.axis.v201809.cm.ProductPartition 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;
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) 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) Bids(com.google.api.ads.adwords.axis.v201809.cm.Bids) CpcBid(com.google.api.ads.adwords.axis.v201809.cm.CpcBid) ProductPartition(com.google.api.ads.adwords.axis.v201809.cm.ProductPartition) NegativeAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion)

Example 7 with ProductPartition

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

the class ProductPartitionTreeImpl method createAdGroupTree.

/**
 * Returns a new instance of this class by retrieving the product partitions of the
 * specified ad group. All parameters are required.
 */
static ProductPartitionTreeImpl createAdGroupTree(AdWordsServicesInterface services, AdWordsSession session, Long adGroupId) throws ApiException, RemoteException {
    // Get the AdGroupCriterionService.
    AdGroupCriterionServiceInterface criterionService = services.get(session, AdGroupCriterionServiceInterface.class);
    SelectorBuilder selectorBuilder = new SelectorBuilder().fields(REQUIRED_SELECTOR_FIELD_ENUMS.toArray(new AdGroupCriterionField[REQUIRED_SELECTOR_FIELD_ENUMS.size()])).equals(AdGroupCriterionField.AdGroupId, adGroupId.toString()).equals(AdGroupCriterionField.CriteriaType, "PRODUCT_PARTITION").in(AdGroupCriterionField.Status, UserStatus.ENABLED.getValue(), UserStatus.PAUSED.getValue()).limit(PAGE_SIZE);
    AdGroupCriterionPage adGroupCriterionPage;
    // A multimap from each product partition ID to its direct children.
    ListMultimap<Long, AdGroupCriterion> parentIdMap = LinkedListMultimap.create();
    int offset = 0;
    do {
        // Get the next page of results.
        adGroupCriterionPage = criterionService.get(selectorBuilder.build());
        if (adGroupCriterionPage != null && adGroupCriterionPage.getEntries() != null) {
            for (AdGroupCriterion adGroupCriterion : adGroupCriterionPage.getEntries()) {
                ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
                parentIdMap.put(partition.getParentCriterionId(), adGroupCriterion);
            }
            offset += adGroupCriterionPage.getEntries().length;
            selectorBuilder.increaseOffsetBy(PAGE_SIZE);
        }
    } while (offset < adGroupCriterionPage.getTotalNumEntries());
    // Construct the ProductPartitionTree from the parentIdMap.
    if (!parentIdMap.containsKey(null)) {
        Preconditions.checkState(parentIdMap.isEmpty(), "No root criterion found in the tree but the tree is not empty");
        return createEmptyAdGroupTree(adGroupId, getAdGroupBiddingStrategyConfiguration(services, session, adGroupId));
    }
    return createNonEmptyAdGroupTree(adGroupId, parentIdMap);
}
Also used : AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) AdGroupCriterionPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionPage) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ProductPartition(com.google.api.ads.adwords.axis.v201809.cm.ProductPartition) AdGroupCriterionField(com.google.api.ads.adwords.lib.selectorfields.v201809.cm.AdGroupCriterionField)

Example 8 with ProductPartition

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

the class ProductPartitionTreeImpl method createAdGroupTree.

/**
 * Returns a new instance of this class based on the collection of ad group criteria provided.
 * <p>NOTE: If retrieving existing criteria for use with this method, you must include all of the
 * fields in {@link #REQUIRED_SELECTOR_FIELD_ENUMS} in your {@link Selector}.
 *
 * @param adGroupId the ID of the ad group
 * @param biddingStrategyConfig the {@link BiddingStrategyConfiguration} for the ad group
 * @param adGroupCriteria the non-null (but possibly empty) list of ad group criteria
 *
 * @throws NullPointerException if any argument is null, any element in {@code adGroupCriteria} is
 *         null, or any required field from {@link #REQUIRED_SELECTOR_FIELD_ENUMS} is missing from
 *         an element in {@code adGroupCriteria}
 * @throws IllegalArgumentException if {@code adGroupCriteria} does not include the root criterion
 *         of the product partition tree
 */
static ProductPartitionTreeImpl createAdGroupTree(Long adGroupId, BiddingStrategyConfiguration biddingStrategyConfig, List<AdGroupCriterion> adGroupCriteria) {
    Preconditions.checkNotNull(adGroupId, "Null ad group ID");
    Preconditions.checkNotNull(biddingStrategyConfig, "Null bidding strategy configuration");
    Preconditions.checkNotNull(adGroupCriteria, "Null criteria list");
    if (adGroupCriteria.isEmpty()) {
        return createEmptyAdGroupTree(adGroupId, biddingStrategyConfig);
    }
    ListMultimap<Long, AdGroupCriterion> parentIdMap = LinkedListMultimap.create();
    for (AdGroupCriterion adGroupCriterion : adGroupCriteria) {
        Preconditions.checkNotNull(adGroupCriterion.getCriterion(), "AdGroupCriterion has a null criterion");
        if (adGroupCriterion instanceof BiddableAdGroupCriterion) {
            BiddableAdGroupCriterion biddableCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
            Preconditions.checkNotNull(biddableCriterion.getUserStatus(), "User status is null for criterion ID %s", biddableCriterion.getCriterion().getId());
            if (UserStatus.REMOVED.equals(biddableCriterion.getUserStatus())) {
                // Skip REMOVED criteria.
                continue;
            }
        }
        if (adGroupCriterion.getCriterion() instanceof ProductPartition) {
            ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
            parentIdMap.put(partition.getParentCriterionId(), adGroupCriterion);
        }
    }
    return createNonEmptyAdGroupTree(adGroupId, parentIdMap);
}
Also used : BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ProductPartition(com.google.api.ads.adwords.axis.v201809.cm.ProductPartition)

Example 9 with ProductPartition

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

the class ProductPartitionTreeImpl method addChildNodes.

/**
 * Using the criteria in {@code parentIdMap}, recursively adds all children under the partition ID
 * of {@code parentNode} to {@code parentNode}.
 *
 * @param parentNode required
 * @param parentIdMap the multimap from parent partition ID to list of child criteria
 */
private static void addChildNodes(ProductPartitionNode parentNode, ListMultimap<Long, AdGroupCriterion> parentIdMap) {
    if (parentIdMap.containsKey(parentNode.getProductPartitionId())) {
        parentNode = parentNode.asSubdivision();
    }
    for (AdGroupCriterion adGroupCriterion : parentIdMap.get(parentNode.getProductPartitionId())) {
        ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
        ProductPartitionNode childNode = parentNode.addChild(partition.getCaseValue());
        childNode = childNode.setProductPartitionId(partition.getId());
        if (ProductPartitionType.SUBDIVISION.equals(partition.getPartitionType())) {
            childNode = childNode.asSubdivision();
        } else {
            if (adGroupCriterion instanceof BiddableAdGroupCriterion) {
                childNode = childNode.asBiddableUnit();
                BiddableAdGroupCriterion biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
                Money cpcBidAmount = getBid(biddableAdGroupCriterion);
                if (cpcBidAmount != null) {
                    childNode = childNode.setBid(cpcBidAmount.getMicroAmount());
                }
                childNode = childNode.setTrackingUrlTemplate(biddableAdGroupCriterion.getTrackingUrlTemplate());
                childNode = copyCustomParametersToNode(biddableAdGroupCriterion, childNode);
            } else {
                childNode = childNode.asExcludedUnit();
            }
        }
        addChildNodes(childNode, parentIdMap);
    }
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ProductPartition(com.google.api.ads.adwords.axis.v201809.cm.ProductPartition)

Aggregations

BiddableAdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion)9 ProductPartition (com.google.api.ads.adwords.axis.v201809.cm.ProductPartition)7 AdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion)6 CpcBid (com.google.api.ads.adwords.axis.v201809.cm.CpcBid)3 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)3 NegativeAdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion)3 AdGroupCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation)2 Bids (com.google.api.ads.adwords.axis.v201809.cm.Bids)2 MockHttpIntegrationTest (com.google.api.ads.common.lib.testing.MockHttpIntegrationTest)2 Test (org.junit.Test)2 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 AdGroupCriterionPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionPage)1 AdGroupCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface)1 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)1 Criterion (com.google.api.ads.adwords.axis.v201809.cm.Criterion)1 CustomParameter (com.google.api.ads.adwords.axis.v201809.cm.CustomParameter)1 CustomParameters (com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)1 AdGroupCriterionField (com.google.api.ads.adwords.lib.selectorfields.v201809.cm.AdGroupCriterionField)1 HashMap (java.util.HashMap)1