Пример #1
0
        public void TestChildNodeBasicFunctionality()
        {
            rootNode = rootNode.AsSubdivision();
            Assert.False(rootNode.IsUnit, "Parent should not be a unit.");
            Assert.True(rootNode.IsSubdivision, "Parent should be a subdivision.");
            ProductBrand         childDimension = ProductDimensions.CreateBrand("google");
            ProductPartitionNode childNode      = rootNode.AddChild(childDimension);

            Assert.AreSame(childDimension, childNode.Dimension,
                           "Child node merely wraps the " + "underlying dimension node.");
            Assert.AreSame(rootNode, childNode.Parent, "child.GetParent should return parentNode.");
            Assert.That(childNode.ProductPartitionId == 0, "Partition ID is incorrect.");

            Assert.That(childNode.Children.Count() == 0, "ChildNode should not have any children.");
            Assert.True(childNode.IsUnit, "New node should be a unit node by default.");
            Assert.True(childNode.IsBiddableUnit,
                        "New node should be a biddable unit node by default.");

            Assert.That(rootNode.HasChild(childDimension),
                        "rootNode.HasChild should return true when " +
                        "passed the dimension of the added child");
            Assert.False(rootNode.HasChild(ProductDimensions.CreateBrand("xyz")),
                         "rootNode.HasChild " +
                         "should return false when passed a dimension for a nonexistent child");
            Assert.False(rootNode.HasChild(null),
                         "rootNode.HasChild should return false when passed " +
                         "a dimension for a nonexistent child");
        }
        /// <summary>
        /// Creates a product partition tree.
        /// </summary>
        /// <param name="user">The AdWords user for which the product partition is created.</param>
        /// <param name="adGroupId">Ad group ID.</param>
        /// <returns>The product partition.</returns>
        private static ProductPartitionTree CreateProductPartition(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(AdWordsService.v201809
                                                                .AdGroupCriterionService))
            {
                // Build a new ProductPartitionTree using the ad group's current set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                Console.WriteLine("Original tree: {0}", partitionTree);

                // Clear out any existing criteria.
                ProductPartitionNode rootNode = partitionTree.Root.RemoveAllChildren();

                // Make the root node a subdivision.
                rootNode = rootNode.AsSubdivision();

                // Add a unit node for condition = NEW to include it.
                rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition
                                                               .NEW));

                // Add a unit node for condition = USED to include it.
                rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition
                                                               .USED));

                // Exclude everything else.
                rootNode.AddChild(ProductDimensions.CreateCanonicalCondition()).AsExcludedUnit();

                // Make the mutate request, using the operations returned by the
                // ProductPartitionTree.
                AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                if (mutateOperations.Length == 0)
                {
                    Console.WriteLine(
                        "Skipping the mutate call because the original tree and the current " +
                        "tree are logically identical.");
                }
                else
                {
                    adGroupCriterionService.mutate(mutateOperations);
                }

                // The request was successful, so create a new ProductPartitionTree based on the
                // updated state of the ad group.
                partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);
                return(partitionTree);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The ad group to which product partition is
        /// added.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(
                           AdWordsService.v201802.AdGroupCriterionService)) {
                // Build a new ProductPartitionTree using the ad group's current set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                Console.WriteLine("Original tree: {0}", partitionTree);

                // Clear out any existing criteria.
                ProductPartitionNode rootNode = partitionTree.Root.RemoveAllChildren();

                // Make the root node a subdivision.
                rootNode = rootNode.AsSubdivision();

                // Add a unit node for condition = NEW.
                ProductPartitionNode newConditionNode = rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.NEW));
                newConditionNode.AsBiddableUnit().CpcBid = 200000;

                ProductPartitionNode usedConditionNode = rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.USED));
                usedConditionNode.AsBiddableUnit().CpcBid = 100000;

                // Add a subdivision node for condition = null (everything else).
                ProductPartitionNode otherConditionNode =
                    rootNode.AddChild(ProductDimensions.CreateCanonicalCondition()).AsSubdivision();

                // Add a unit node under condition = null for brand = "CoolBrand".
                ProductPartitionNode coolBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand("CoolBrand"));
                coolBrandNode.AsBiddableUnit().CpcBid = 900000L;

                // Add a unit node under condition = null for brand = "CheapBrand".
                ProductPartitionNode cheapBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand("CheapBrand"));
                cheapBrandNode.AsBiddableUnit().CpcBid = 10000L;

                // Add a subdivision node under condition = null for brand = null (everything else).
                ProductPartitionNode otherBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand(null)).AsSubdivision();

                // Add unit nodes under condition = null/brand = null.
                // The value for each bidding category is a fixed ID for a specific
                // category. You can retrieve IDs for categories from the ConstantDataService.
                // See the 'GetProductCategoryTaxonomy' example for more details.

                // Add a unit node under condition = null/brand = null for product type
                // level 1 = 'Luggage & Bags'.
                ProductPartitionNode luggageAndBagNode = otherBrandNode.AddChild(
                    ProductDimensions.CreateBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1,
                                                            -5914235892932915235L));
                luggageAndBagNode.AsBiddableUnit().CpcBid = 750000L;

                // Add a unit node under condition = null/brand = null for product type
                // level 1 = null (everything else).
                ProductPartitionNode everythingElseNode = otherBrandNode.AddChild(
                    ProductDimensions.CreateBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1));
                everythingElseNode.AsBiddableUnit().CpcBid = 110000L;

                try {
                    // Make the mutate request, using the operations returned by the ProductPartitionTree.
                    AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                    if (mutateOperations.Length == 0)
                    {
                        Console.WriteLine("Skipping the mutate call because the original tree and the " +
                                          "current tree are logically identical.");
                    }
                    else
                    {
                        adGroupCriterionService.mutate(mutateOperations);
                    }

                    // The request was successful, so create a new ProductPartitionTree based on the updated
                    // state of the ad group.
                    partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                    Console.WriteLine("Final tree: {0}", partitionTree);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to set shopping product partition.", e);
                }
            }
        }
        /// <summary>
        /// Using the criteria in <paramref name="parentIdMap"/>, recursively adds
        /// all children under the partition ID of <paramref name="parentNode"/> to
        /// <paramref name="parentNode"/>.
        /// </summary>
        /// <param name="parentNode">The parent node.</param>
        /// <param name="parentIdMap">The multimap from parent partition ID to list
        /// of child criteria</param>
        private static void AddChildNodes(ProductPartitionNode parentNode,
        Dictionary<long, List<AdGroupCriterion>> parentIdMap)
        {
            List<AdGroupCriterion> childCriteria = null;
              if (parentIdMap.ContainsKey(parentNode.ProductPartitionId)) {
            childCriteria = parentIdMap[parentNode.ProductPartitionId];
              }

              // no children, return.
              if (childCriteria == null || childCriteria.Count == 0) {
            return;
              }

              // Ensure that the parent is a subdivision.
              parentNode.AsSubdivision();

              foreach (AdGroupCriterion childCriterion in childCriteria) {
            ProductPartition partition = (ProductPartition) childCriterion.criterion;
            ProductPartitionNode childNode = parentNode.AddChild(partition.caseValue);
            childNode.ProductPartitionId = partition.id;

            if (childCriterion is BiddableAdGroupCriterion) {
              childNode = childNode.AsBiddableUnit();
              Money cpcBidAmount = GetBid((BiddableAdGroupCriterion) childCriterion);
              if (cpcBidAmount != null) {
            childNode.CpcBid = cpcBidAmount.microAmount;
              }
            } else {
              childNode = childNode.AsExcludedUnit();
            }

            AddChildNodes(childNode, parentIdMap);
              }
        }