/// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="trialId">Id of the trial to be graduated.</param>
        public void Run(AdWordsUser user, long trialId)
        {
            // Get the TrialService and BudgetService.
            TrialService trialService = (TrialService)user.GetService(
                AdWordsService.v201609.TrialService);
            BudgetService budgetService = (BudgetService)user.GetService(
                AdWordsService.v201609.BudgetService);

            try {
                // To graduate a trial, you must specify a different budget from the
                // base campaign. The base campaign (in order to have had a trial based
                // on it) must have a non-shared budget, so it cannot be shared with
                // the new independent campaign created by graduation.
                Budget budget = new Budget()
                {
                    name   = "Budget #" + ExampleUtilities.GetRandomString(),
                    amount = new Money()
                    {
                        microAmount = 50000000L
                    },
                    deliveryMethod = BudgetBudgetDeliveryMethod.STANDARD
                };

                BudgetOperation budgetOperation = new BudgetOperation()
                {
                    @operator = Operator.ADD,
                    operand   = budget
                };

                // Add budget.
                long budgetId = budgetService.mutate(
                    new BudgetOperation[] { budgetOperation }).value[0].budgetId;

                Trial trial = new Trial()
                {
                    id       = trialId,
                    budgetId = budgetId,
                    status   = TrialStatus.GRADUATED
                };

                TrialOperation trialOperation = new TrialOperation()
                {
                    @operator = Operator.SET,
                    operand   = trial
                };

                // Update the trial.
                trial = trialService.mutate(new TrialOperation[] { trialOperation }).value[0];

                // Graduation is a synchronous operation, so the campaign is already
                // ready. If you promote instead, make sure to see the polling scheme
                // demonstrated in AddTrial.cs to wait for the asynchronous operation
                // to finish.
                Console.WriteLine("Trial ID {0} graduated. Campaign ID {1} was given a new budget " +
                                  "ID {1} and is no longer dependent on this trial.", trial.id, trial.trialCampaignId,
                                  budgetId);
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to graduate trial.", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="trialId">Id of the trial to be graduated.</param>
        public void Run(AdWordsUser user, long trialId)
        {
            using (TrialService trialService =
                       (TrialService)user.GetService(AdWordsService.v201806.TrialService))
            {
                // To graduate a trial, you must specify a different budget from the
                // base campaign. The base campaign (in order to have had a trial based
                // on it) must have a non-shared budget, so it cannot be shared with
                // the new independent campaign created by graduation.
                Budget budget = CreateBudget(user);

                Trial trial = new Trial()
                {
                    id       = trialId,
                    budgetId = budget.budgetId,
                    status   = TrialStatus.GRADUATED
                };

                TrialOperation trialOperation = new TrialOperation()
                {
                    @operator = Operator.SET,
                    operand   = trial
                };
                try
                {
                    // Update the trial.
                    trial = trialService.mutate(new TrialOperation[]
                    {
                        trialOperation
                    }).value[0];

                    // Graduation is a synchronous operation, so the campaign is already
                    // ready. If you promote instead, make sure to see the polling scheme
                    // demonstrated in AddTrial.cs to wait for the asynchronous operation
                    // to finish.
                    Console.WriteLine(
                        "Trial ID {0} graduated. Campaign ID {1} was given a new budget " +
                        "ID {2} and is no longer dependent on this trial.", trial.id,
                        trial.trialCampaignId, budget.budgetId);
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to graduate trial.", e);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="baseCampaignId">Id of the campaign to use as base of the
        /// trial.</param>
        /// <param name="draftId">Id of the draft.</param>
        public void Run(AdWordsUser user, long draftId, long baseCampaignId)
        {
            using (TrialService trialService = (TrialService)user.GetService(
                       AdWordsService.v201806.TrialService))
                using (TrialAsyncErrorService trialAsyncErrorService =
                           (TrialAsyncErrorService)user.GetService(
                               AdWordsService.v201806.TrialAsyncErrorService)) {
                    Trial trial = new Trial()
                    {
                        draftId             = draftId,
                        baseCampaignId      = baseCampaignId,
                        name                = "Test Trial #" + ExampleUtilities.GetRandomString(),
                        trafficSplitPercent = 50
                    };

                    TrialOperation trialOperation = new TrialOperation()
                    {
                        @operator = Operator.ADD,
                        operand   = trial
                    };
                    try {
                        long trialId = trialService.mutate(new TrialOperation[] { trialOperation }).value[0].id;

                        // Since creating a trial is asynchronous, we have to poll it to wait
                        // for it to finish.
                        Selector trialSelector = new Selector()
                        {
                            fields = new string[] {
                                Trial.Fields.Id, Trial.Fields.Status, Trial.Fields.BaseCampaignId,
                                Trial.Fields.TrialCampaignId
                            },
                            predicates = new Predicate[] {
                                Predicate.Equals(Trial.Fields.Id, trialId)
                            }
                        };

                        trial = null;
                        bool isPending    = true;
                        int  pollAttempts = 0;

                        do
                        {
                            int sleepMillis = (int)Math.Pow(2, pollAttempts) *
                                              POLL_INTERVAL_SECONDS_BASE * 1000;
                            Console.WriteLine("Sleeping {0} millis...", sleepMillis);
                            Thread.Sleep(sleepMillis);

                            trial = trialService.get(trialSelector).entries[0];

                            Console.WriteLine("Trial ID {0} has status '{1}'.", trial.id, trial.status);
                            pollAttempts++;
                            isPending = (trial.status == TrialStatus.CREATING);
                        } while (isPending && pollAttempts <= MAX_RETRIES);

                        if (trial.status == TrialStatus.ACTIVE)
                        {
                            // The trial creation was successful.
                            Console.WriteLine("Trial created with ID {0} and trial campaign ID {1}.",
                                              trial.id, trial.trialCampaignId);
                        }
                        else if (trial.status == TrialStatus.CREATION_FAILED)
                        {
                            // The trial creation failed, and errors can be fetched from the
                            // TrialAsyncErrorService.
                            Selector errorsSelector = new Selector()
                            {
                                fields = new string[] {
                                    TrialAsyncError.Fields.TrialId, TrialAsyncError.Fields.AsyncError
                                },
                                predicates = new Predicate[] {
                                    Predicate.Equals(TrialAsyncError.Fields.TrialId, trial.id)
                                }
                            };

                            TrialAsyncErrorPage trialAsyncErrorPage = trialAsyncErrorService.get(errorsSelector);
                            if (trialAsyncErrorPage.entries == null || trialAsyncErrorPage.entries.Length == 0)
                            {
                                Console.WriteLine("Could not retrieve errors for trial {0}.", trial.id);
                            }
                            else
                            {
                                Console.WriteLine("Could not create trial ID {0} for draft ID {1} due to the " +
                                                  "following errors:", trial.id, draftId);
                                int i = 0;
                                foreach (TrialAsyncError error in trialAsyncErrorPage.entries)
                                {
                                    ApiError asyncError = error.asyncError;
                                    Console.WriteLine("Error #{0}: errorType='{1}', errorString='{2}', " +
                                                      "trigger='{3}', fieldPath='{4}'", i++, asyncError.ApiErrorType,
                                                      asyncError.errorString, asyncError.trigger, asyncError.fieldPath);
                                }
                            }
                        }
                        else
                        {
                            // Most likely, the trial is still being created. You can continue
                            // polling, but we have limited the number of attempts in the
                            // example.
                            Console.WriteLine("Timed out waiting to create trial from draft ID {0} with " +
                                              "base campaign ID {1}.", draftId, baseCampaignId);
                        }
                    } catch (Exception e) {
                        throw new System.ApplicationException("Failed to create trial from draft.", e);
                    }
                }
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a test trial for running further tests.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="baseCampaignId">The base campaign ID for the draft.</param>
        /// <param name="draftId">ID of the draft to use when creating trial.</param>
        /// <returns>The trial ID.</returns>
        public long CreateTrial(AdWordsUser user, long draftId, long baseCampaignId)
        {
            // Get the TrialService.
            TrialService trialService = (TrialService)user.GetService(
                AdWordsService.v201607.TrialService);

            Trial trial = new Trial()
            {
                draftId             = draftId,
                baseCampaignId      = baseCampaignId,
                name                = "Test Trial #" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
                trafficSplitPercent = 50
            };

            TrialOperation trialOperation = new TrialOperation()
            {
                @operator = Operator.ADD,
                operand   = trial
            };

            long trialId = trialService.mutate(new TrialOperation[] { trialOperation }).value[0].id;

            // Since creating a trial is asynchronous, we have to poll it to wait
            // for it to finish.
            Selector trialSelector = new Selector()
            {
                fields = new string[] {
                    Trial.Fields.Id, Trial.Fields.Status, Trial.Fields.BaseCampaignId,
                    Trial.Fields.TrialCampaignId
                },
                predicates = new Predicate[] {
                    Predicate.Equals(Trial.Fields.Id, trialId)
                }
            };

            trial = null;
            bool isPending    = true;
            int  pollAttempts = 0;

            do
            {
                int sleepMillis = (int)Math.Pow(2, pollAttempts) *
                                  POLL_INTERVAL_SECONDS_BASE * 1000;
                Console.WriteLine("Sleeping {0} millis...", sleepMillis);
                Thread.Sleep(sleepMillis);

                trial = trialService.get(trialSelector).entries[0];

                Console.WriteLine("Trial ID {0} has status '{1}'.", trial.id, trial.status);
                pollAttempts++;
                isPending = (trial.status == TrialStatus.CREATING);
            } while (isPending && pollAttempts <= MAX_RETRIES);

            if (trial.status == TrialStatus.ACTIVE)
            {
                return(trial.id);
            }
            else
            {
                throw new System.ApplicationException("Failed to create an active trial for testing.");
            }
        }