/// <summary>
        /// Creates a tree from criteria passed into the factory method.
        /// </summary>
        private void CreateTreeFromCriteria()
        {
            AdGroupCriterionService service = (AdGroupCriterionService)user.GetService(
                AdWordsService.v201806.AdGroupCriterionService);

            string[] REQUIRED_SELECTOR_FIELD_ENUMS = new string[] {
                AdGroupCriterion.Fields.AdGroupId,
                Criterion.Fields.Id,
                ProductPartition.Fields.ParentCriterionId,
                ProductPartition.Fields.PartitionType,
                ProductPartition.Fields.CriteriaType,
                ProductPartition.Fields.CaseValue,
                CpcBid.Fields.CpcBid,
                CpcBid.Fields.CpcBidSource,
                BiddableAdGroupCriterion.Fields.Status
            };

            Selector selector = new Selector()
            {
                fields     = REQUIRED_SELECTOR_FIELD_ENUMS,
                predicates = new Predicate[] {
                    Predicate.Equals(AdGroupCriterion.Fields.AdGroupId, ADGROUP_ID)
                }
            };

            AdGroupCriterionPage retval = service.get(selector);

            tree = ProductPartitionTree.CreateAdGroupTree(ADGROUP_ID,
                                                          new List <AdGroupCriterion>(retval.entries));
        }
        /// <summary>
        /// Creates the default partition.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The ad group ID.</param>
        private void CreateDefaultPartitionTree(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(
                           AdWordsService.v201802.AdGroupCriterionService)) {
                // Build a new ProductPartitionTree using an empty set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.CreateAdGroupTree(adGroupId, new List <AdGroupCriterion>());
                partitionTree.Root.AsBiddableUnit().CpcBid = 1000000;

                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);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Creates the test tree.
        /// </summary>
        /// <param name="adGroupId">The ad group ID.</param>
        /// <param name="rootId">The root product partition ID.</param>
        /// <returns>A product partition tree for running tests.</returns>
        private ProductPartitionTree CreateTestTree(long adGroupId, long rootId)
        {
            // The tree structure is:
            // root
            //   Google
            //     offerA,
            //     offerB,
            //     Other
            //   Motorola
            //   Other

            long counterStart = rootId - 1;

            List <AdGroupCriterion> adGroupCriteria = new List <AdGroupCriterion>()
            {
                CreateCriterionForProductPartition(rootId,
                                                   ProductPartitionTree.ROOT_PARENT_ID, null, false, false), // root
                CreateCriterionForProductPartition(counterStart - 1L, rootId,
                                                   BRAND_GOOGLE, true, false, 100000L),                      // google
                CreateCriterionForProductPartition(counterStart - 2L, rootId,
                                                   BRAND_MOTOROLA, true, false, 50000L),                     // Motorola
                CreateCriterionForProductPartition(counterStart - 3L, rootId,
                                                   BRAND_OTHER, true, true),                                 // Other

                CreateCriterionForProductPartition(counterStart - 4L, counterStart - 1L, OFFER_A, true,
                                                   false, 30000L), // offerA
                CreateCriterionForProductPartition(counterStart - 5L, counterStart - 1L, OFFER_B, true,
                                                   true, 30000L),  // offerB
                CreateCriterionForProductPartition(counterStart - 6L, counterStart - 1L, OFFER_OTHER, true,
                                                   false, 50000L), // Other
            };

            return(ProductPartitionTree.CreateAdGroupTree(adGroupId, adGroupCriteria));
        }
        public void TestCreateMultiNodeTreeFromScratch()
        {
            ProductPartitionTree tree =
                ProductPartitionTree.CreateAdGroupTree(new List <AdGroupCriterion>());

            ProductPartitionNode rootNode = tree.Root.AsSubdivision();
            ProductPartitionNode brand1   = rootNode.AddChild(ProductDimensions.CreateBrand("google"))
                                            .AsSubdivision();
            ProductPartitionNode brand1Offer1 =
                brand1.AddChild(ProductDimensions.CreateOfferId("A"));

            brand1Offer1.AsBiddableUnit().CpcBid = 1000000L;
            ProductPartitionNode brand1Offer2    =
                brand1.AddChild(ProductDimensions.CreateOfferId()).AsExcludedUnit();
            ProductPartitionNode brand2 =
                rootNode.AddChild(ProductDimensions.CreateBrand()).AsExcludedUnit();

            ProductPartitionNode[] nodes = new ProductPartitionNode[]
            {
                rootNode,
                brand1,
                brand1Offer1,
                brand1Offer2,
                brand2
            };

            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();

            for (int i = 0; i < nodes.Length; i++)
            {
                List <AdGroupCriterionOperation> nodeOperations =
                    shoppingTestUtils.GetOperationsForNode(nodes[i], mutateOperations);
                Assert.That(nodeOperations.Count == 1);
                Assert.That(nodeOperations[0].@operator == Operator.ADD);
                ProductPartition partition = (ProductPartition)nodeOperations[0].operand.criterion;
                Assert.That(partition.id == nodes[i].ProductPartitionId);
                if (nodes[i].Parent == null)
                {
                    Assert.That(partition.parentCriterionIdSpecified == false);
                }
                else
                {
                    Assert.That(partition.parentCriterionId == nodes[i].Parent.ProductPartitionId);
                }

                Assert.That(partition.caseValue == nodes[i].Dimension);
            }
        }
        public void TestRemovedCriteriaIgnored()
        {
            AdGroupCriterion rootCriterion = ShoppingTestUtils.CreateCriterionForProductPartition(
                1L, 0L, null, true, false);
            List <AdGroupCriterion> criteria = new List <AdGroupCriterion>();

            criteria.Add(rootCriterion);

            // Create a criteria for a child node and set its UserStatus to REMOVED.
            ProductBrand     brandGoogle      = ProductDimensions.CreateBrand("google");
            AdGroupCriterion removedCriterion = ShoppingTestUtils.CreateCriterionForProductPartition(
                2L, 1L, brandGoogle, true, false);

            ((BiddableAdGroupCriterion)removedCriterion).userStatus = UserStatus.REMOVED;
            criteria.Add(removedCriterion);

            ProductPartitionTree tree =
                ProductPartitionTree.CreateAdGroupTree(criteria);

            Assert.False(tree.Root.HasChild(brandGoogle),
                         "Brand = google criteria had status removed, but it is in the tree.");
        }
        public void TestCreateUltimatelyEmptyTree()
        {
            ProductPartitionTree tree =
                ProductPartitionTree.CreateAdGroupTree(new List <AdGroupCriterion>());

            ProductPartitionNode rootNode = tree.Root.AsSubdivision();
            ProductPartitionNode brand1   = rootNode.AddChild(ProductDimensions.CreateBrand("google"))
                                            .AsSubdivision();
            ProductPartitionNode offerNode = brand1.AddChild(ProductDimensions.CreateOfferId("A"));

            offerNode.AsBiddableUnit().CpcBid = 1000000L;

            brand1.AddChild(ProductDimensions.CreateOfferId()).AsExcludedUnit();
            ProductPartitionNode brand2 =
                rootNode.AddChild(ProductDimensions.CreateBrand()).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.Dimension);
            rootNode.RemoveChild(brand2.Dimension);
            rootNode = rootNode.AsBiddableUnit();

            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();

            Assert.AreEqual(mutateOperations.Count(), 1, "Number of operations is incorrect.");
            AdGroupCriterionOperation operation = mutateOperations[0];

            Assert.AreEqual(Operator.ADD, operation.@operator,
                            "Should have a single operation to ADD the root node.");
            BiddableAdGroupCriterion adGroupCriterion =
                (BiddableAdGroupCriterion)operation.operand;

            Assert.Null(((ProductPartition)adGroupCriterion.criterion).caseValue,
                        "Product dimension of operation's operand should be null.");
            Assert.True(adGroupCriterion.criterion.id < 0L,
                        "Partition ID of the operand should be negative.");
        }
        public void TestCreateEmptyTree()
        {
            ProductPartitionTree tree = ProductPartitionTree.CreateAdGroupTree(
                new List <AdGroupCriterion>());

            Assert.NotNull(tree.Root, "Even an empty tree should automatically have a root node.");
            Assert.True(tree.Root.ProductPartitionId < 0L,
                        "The root node for an empty tree should have a negative (temporary) ID.");
            Assert.True(tree.Root.IsUnit, "The root node for an empty tree should be a UNIT.");

            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();

            Assert.That(mutateOperations.Count() == 1, "Number of operations is incorrect.");
            AdGroupCriterionOperation operation = mutateOperations[0];

            Assert.AreEqual(Operator.ADD, operation.@operator,
                            "Should have a single operation to ADD the root node.");
            BiddableAdGroupCriterion adGroupCriterion = (BiddableAdGroupCriterion)operation.operand;

            Assert.Null(((ProductPartition)adGroupCriterion.criterion).caseValue,
                        "Product dimension of operation's operand should be null.");
            Assert.True(adGroupCriterion.criterion.id < 0L);
        }
        public void Init()
        {
            CAMPAIGN_ID = utils.CreateShoppingCampaign(user, BiddingStrategyType.MANUAL_CPC);
            ADGROUP_ID  = utils.CreateAdGroup(user, CAMPAIGN_ID);

            tree = ProductPartitionTree.CreateAdGroupTree(ADGROUP_ID,
                                                          new List <AdGroupCriterion>());
            ProductPartitionNode root = tree.Root.AsSubdivision();

            ProductPartitionNode clothing = root.AddChild(ProductDimensions.CreateType(
                                                              ProductDimensionType.PRODUCT_TYPE_L1, "clothing"));

            clothing.AsBiddableUnit().CpcBid = 200000;
            ProductPartitionNode shoes       = root.AddChild(ProductDimensions.CreateType(
                                                                 ProductDimensionType.PRODUCT_TYPE_L1, "shoes"));

            shoes.AsBiddableUnit().CpcBid  = 400000;
            ProductPartitionNode otherNode = root.AddChild(ProductDimensions.CreateType(
                                                               ProductDimensionType.PRODUCT_TYPE_L1));

            otherNode.AsBiddableUnit().CpcBid = 300000;

            tree = ExecuteTreeOperations();
        }