Example #1
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign from which targeting
        /// criteria are retrieved.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the CampaignCriterionService.
            CampaignCriterionService campaignCriterionService =
                (CampaignCriterionService)user.GetService(
                    AdWordsService.v201710.CampaignCriterionService);

            // Create the selector.
            Selector selector = new Selector()
            {
                fields = new string[] {
                    Criterion.Fields.Id, Criterion.Fields.CriteriaType, CampaignCriterion.Fields.CampaignId
                },
                predicates = new Predicate[] {
                    Predicate.Equals(CampaignCriterion.Fields.CampaignId, campaignId)
                },
                paging = Paging.Default
            };

            CampaignCriterionPage page = new CampaignCriterionPage();

            try {
                do
                {
                    // Get all campaign targets.
                    page = campaignCriterionService.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        int i = selector.paging.startIndex;
                        foreach (CampaignCriterion campaignCriterion in page.entries)
                        {
                            string negative = (campaignCriterion is NegativeCampaignCriterion) ? "Negative " : "";
                            Console.WriteLine("{0}) {1}Campaign criterion with id = '{2}' and Type = {3} was " +
                                              " found for campaign id '{4}'", i + 1, negative, campaignCriterion.criterion.id,
                                              campaignCriterion.criterion.type, campaignCriterion.campaignId);
                            i++;
                        }
                    }
                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);
                Console.WriteLine("Number of campaign targeting criteria found: {0}",
                                  page.totalNumEntries);
                campaignCriterionService.Close();
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to get campaign targeting criteria.", e);
            }
        }
Example #2
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign to which targeting criteria
        /// are added.</param>
        /// <param name="feedId">ID of a feed that has been configured for location
        /// targeting, meaning it has an ENABLED FeedMapping with criterionType of
        /// 77. Feeds linked to a GMB account automatically have this FeedMapping.
        /// If you don't have such a feed, set this value to null.</param>
        public void Run(AdWordsUser user, long campaignId, long?feedId)
        {
            // Get the CampaignCriterionService.
            CampaignCriterionService campaignCriterionService =
                (CampaignCriterionService)user.GetService(
                    AdWordsService.v201710.CampaignCriterionService);

            // Create locations. The IDs can be found in the documentation or
            // retrieved with the LocationCriterionService.
            Location california = new Location()
            {
                id = 21137L
            };

            Location mexico = new Location()
            {
                id = 2484L
            };

            // Create languages. The IDs can be found in the documentation or
            // retrieved with the ConstantDataService.
            Language english = new Language()
            {
                id = 1000L
            };

            Language spanish = new Language()
            {
                id = 1003L
            };

            List <Criterion> criteria = new List <Criterion>()
            {
                california, mexico, english, spanish
            };

            // Distance targeting. Area of 10 miles around the locations in the location feed.
            if (feedId != null)
            {
                LocationGroups radiusLocationGroup = new LocationGroups()
                {
                    feedId           = feedId.Value,
                    matchingFunction = new Function()
                    {
                        @operator  = FunctionOperator.IDENTITY,
                        lhsOperand = new FunctionArgumentOperand[] {
                            new LocationExtensionOperand()
                            {
                                radius = new ConstantOperand()
                                {
                                    type        = ConstantOperandConstantType.DOUBLE,
                                    unit        = ConstantOperandUnit.MILES,
                                    doubleValue = 10
                                }
                            }
                        }
                    }
                };

                criteria.Add(radiusLocationGroup);
            }

            // Create operations to add each of the criteria above.
            List <CampaignCriterionOperation> operations = new List <CampaignCriterionOperation>();

            foreach (Criterion criterion in criteria)
            {
                CampaignCriterionOperation operation = new CampaignCriterionOperation()
                {
                    operand = new CampaignCriterion()
                    {
                        campaignId = campaignId,
                        criterion  = criterion
                    },
                    @operator = Operator.ADD
                };

                operations.Add(operation);
            }

            // Add a negative campaign criterion.

            CampaignCriterion negativeCriterion = new NegativeCampaignCriterion()
            {
                campaignId = campaignId,
                criterion  = new Keyword()
                {
                    text      = "jupiter cruise",
                    matchType = KeywordMatchType.BROAD
                }
            };

            CampaignCriterionOperation negativeCriterionOperation = new CampaignCriterionOperation()
            {
                operand   = negativeCriterion,
                @operator = Operator.ADD
            };

            operations.Add(negativeCriterionOperation);

            try {
                // Set the campaign targets.
                CampaignCriterionReturnValue retVal = campaignCriterionService.mutate(operations.ToArray());

                if (retVal != null && retVal.value != null)
                {
                    // Display campaign targets.
                    foreach (CampaignCriterion criterion in retVal.value)
                    {
                        Console.WriteLine("Campaign criteria of type '{0}' was set to campaign with" +
                                          " id = '{1}'.", criterion.criterion.CriterionType, criterion.campaignId);
                    }
                }
                campaignCriterionService.Close();
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to set Campaign criteria.", e);
            }
        }
Example #3
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">The campaign id to add product scope.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the CampaignCriterionService.
            CampaignCriterionService campaignCriterionService =
                (CampaignCriterionService)user.GetService(
                    AdWordsService.v201710.CampaignCriterionService);

            ProductScope productScope = new ProductScope();
            // This set of dimensions is for demonstration purposes only. It would be
            // extremely unlikely that you want to include so many dimensions in your
            // product scope.
            ProductBrand nexusBrand = new ProductBrand();

            nexusBrand.value = "Nexus";

            ProductCanonicalCondition newProducts = new ProductCanonicalCondition();

            newProducts.condition = ProductCanonicalConditionCondition.NEW;

            ProductCustomAttribute customAttribute = new ProductCustomAttribute();

            customAttribute.type  = ProductDimensionType.CUSTOM_ATTRIBUTE_0;
            customAttribute.value = "my attribute value";

            ProductOfferId bookOffer = new ProductOfferId();

            bookOffer.value = "book1";

            ProductType mediaProducts = new ProductType();

            mediaProducts.type  = ProductDimensionType.PRODUCT_TYPE_L1;
            mediaProducts.value = "Media";

            ProductType bookProducts = new ProductType();

            bookProducts.type  = ProductDimensionType.PRODUCT_TYPE_L2;
            bookProducts.value = "Books";

            // The value for the bidding category is a fixed ID for the
            // 'Luggage & Bags' category. You can retrieve IDs for categories from
            // the ConstantDataService. See the 'GetProductCategoryTaxonomy' example
            // for more details.
            ProductBiddingCategory luggageBiddingCategory = new ProductBiddingCategory();

            luggageBiddingCategory.type  = ProductDimensionType.BIDDING_CATEGORY_L1;
            luggageBiddingCategory.value = -5914235892932915235;

            productScope.dimensions = new ProductDimension[] { nexusBrand, newProducts, bookOffer,
                                                               mediaProducts, luggageBiddingCategory };

            CampaignCriterion campaignCriterion = new CampaignCriterion();

            campaignCriterion.campaignId = campaignId;
            campaignCriterion.criterion  = productScope;

            // Create operation.
            CampaignCriterionOperation operation = new CampaignCriterionOperation();

            operation.operand   = campaignCriterion;
            operation.@operator = Operator.ADD;

            try {
                // Make the mutate request.
                CampaignCriterionReturnValue result = campaignCriterionService.mutate(
                    new CampaignCriterionOperation[] { operation });

                Console.WriteLine("Created a ProductScope criterion with ID '{0}'",
                                  result.value[0].criterion.id);
                campaignCriterionService.Close();
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to set shopping product scope.", e);
            }
        }