示例#1
0
        /// <summary>
        /// Sets an adparam for running further tests.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The adgroup id to which criterionId belongs.
        /// </param>
        /// <param name="criterionId">The criterion id to which adparam is set.
        /// </param>
        public void SetAdParam(AdWordsUser user, long adGroupId, long criterionId)
        {
            AdParamService adParamService =
                (AdParamService)user.GetService(AdWordsService.v201806.AdParamService);

            // Prepare for setting ad parameters.
            AdParam adParam = new AdParam();

            adParam.adGroupId     = adGroupId;
            adParam.criterionId   = criterionId;
            adParam.paramIndex    = 1;
            adParam.insertionText = "$100";

            AdParamOperation adParamOperation = new AdParamOperation();

            adParamOperation.@operator = Operator.SET;
            adParamOperation.operand   = adParam;

            // Set ad parameters.
            AdParam[] newAdParams = adParamService.mutate(new AdParamOperation[]
            {
                adParamOperation
            });
            return;
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group that contains the criterion.
        /// </param>
        /// <param name="criterionId">Id of the keyword for which the ad
        /// parameters are set.</param>
        public void Run(AdWordsUser user, long adGroupId, long criterionId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(
                AdWordsService.v201601.AdGroupAdService);

            // Get the AdParamService.
            AdParamService adParamService = (AdParamService)user.GetService(
                AdWordsService.v201601.AdParamService);

            // Create the text ad.
            TextAd textAd = new TextAd();

            textAd.finalUrls    = new string[] { "http://www.example.com" };
            textAd.displayUrl   = "example.com";
            textAd.headline     = " Mars Cruises";
            textAd.description1 = "Low-gravity fun for {param1:cheap}.";
            textAd.description2 = "Only {param2:a few} seats left!";

            AdGroupAd adOperand = new AdGroupAd();

            adOperand.adGroupId = adGroupId;
            adOperand.status    = AdGroupAdStatus.ENABLED;
            adOperand.ad        = textAd;

            // Create the operation.
            AdGroupAdOperation adOperation = new AdGroupAdOperation();

            adOperation.operand   = adOperand;
            adOperation.@operator = Operator.ADD;

            try {
                // Create the text ad.
                AdGroupAdReturnValue retVal = adGroupAdService.mutate(
                    new AdGroupAdOperation[] { adOperation });

                // Display the results.
                if (retVal != null && retVal.value != null && retVal.value.Length > 0)
                {
                    Console.WriteLine("Text ad with id ='{0}' was successfully added.",
                                      retVal.value[0].ad.id);
                }
                else
                {
                    Console.WriteLine("No text ads were created.");
                    return;
                }
            } catch (Exception e) {
                Console.WriteLine("Failed to create text ads. Exception says \"{0}\"", e.Message);
                return;
            }

            // Create the ad param for price.
            AdParam priceParam = new AdParam();

            priceParam.adGroupId     = adGroupId;
            priceParam.criterionId   = criterionId;
            priceParam.paramIndex    = 1;
            priceParam.insertionText = "$100";

            // Create the ad param for seats.
            AdParam seatParam = new AdParam();

            seatParam.adGroupId     = adGroupId;
            seatParam.criterionId   = criterionId;
            seatParam.paramIndex    = 2;
            seatParam.insertionText = "50";

            // Create the operations.
            AdParamOperation priceOperation = new AdParamOperation();

            priceOperation.@operator = Operator.SET;
            priceOperation.operand   = priceParam;

            AdParamOperation seatOperation = new AdParamOperation();

            seatOperation.@operator = Operator.SET;
            seatOperation.operand   = seatParam;

            try {
                // Set the ad parameters.
                AdParam [] newAdParams = adParamService.mutate(new AdParamOperation[]
                                                               { priceOperation, seatOperation });

                // Display the results.
                if (newAdParams != null)
                {
                    Console.WriteLine("Ad parameters were successfully updated.");
                }
                else
                {
                    Console.WriteLine("No ad parameters were set.");
                }
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to set ad parameters.", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group that contains the criterion.
        /// </param>
        /// <param name="criterionId">Id of the keyword for which the ad
        /// parameters are set.</param>
        public void Run(AdWordsUser user, long adGroupId, long criterionId)
        {
            using (AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(
                       AdWordsService.v201802.AdGroupAdService))
                using (AdParamService adParamService = (AdParamService)user.GetService(
                           AdWordsService.v201802.AdParamService)) {
                    // Create the expanded text ad.
                    ExpandedTextAd expandedTextAd = new ExpandedTextAd {
                        headlinePart1 = "Mars Cruises",
                        headlinePart2 = "Low-gravity fun for {param1:cheap}.",
                        description   = "Only {param2:a few} seats left!",
                        finalUrls     = new string[] { "http://www.example.com" }
                    };

                    AdGroupAd adOperand = new AdGroupAd {
                        adGroupId = adGroupId,
                        status    = AdGroupAdStatus.ENABLED,
                        ad        = expandedTextAd
                    };

                    // Create the operation.
                    AdGroupAdOperation adOperation = new AdGroupAdOperation {
                        operand   = adOperand,
                        @operator = Operator.ADD
                    };


                    // Create the text ad.
                    AdGroupAdReturnValue retVal = adGroupAdService.mutate(
                        new AdGroupAdOperation[] { adOperation });

                    // Display the results.
                    if (retVal != null && retVal.value != null && retVal.value.Length > 0)
                    {
                        Console.WriteLine("Expanded text ad with id ='{0}' was successfully added.",
                                          retVal.value[0].ad.id);
                    }
                    else
                    {
                        throw new System.ApplicationException("Failed to create expanded text ads.");
                    }

                    // Create the ad param for price.
                    AdParam priceParam = new AdParam {
                        adGroupId     = adGroupId,
                        criterionId   = criterionId,
                        paramIndex    = 1,
                        insertionText = "$100"
                    };

                    // Create the ad param for seats.
                    AdParam seatParam = new AdParam {
                        adGroupId     = adGroupId,
                        criterionId   = criterionId,
                        paramIndex    = 2,
                        insertionText = "50"
                    };

                    // Create the operations.
                    AdParamOperation priceOperation = new AdParamOperation {
                        @operator = Operator.SET,
                        operand   = priceParam
                    };

                    AdParamOperation seatOperation = new AdParamOperation {
                        @operator = Operator.SET,
                        operand   = seatParam
                    };

                    try {
                        // Set the ad parameters.
                        AdParam[] newAdParams = adParamService.mutate(new AdParamOperation[]
                                                                      { priceOperation, seatOperation });

                        // Display the results.
                        if (newAdParams != null)
                        {
                            Console.WriteLine("Ad parameters were successfully updated.");
                        }
                        else
                        {
                            Console.WriteLine("No ad parameters were set.");
                        }
                    } catch (Exception e) {
                        throw new System.ApplicationException("Failed to set ad parameters.", e);
                    }
                }
        }