/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> public void Run(AdWordsUser user) { // Get the BatchJobService. BatchJobService batchJobService = (BatchJobService)user.GetService( AdWordsService.v201607.BatchJobService); try { // Create a BatchJob. BatchJobOperation addOp = new BatchJobOperation() { @operator = Operator.ADD, operand = new BatchJob() }; BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).value[0]; // Get the upload URL from the new job. string uploadUrl = batchJob.uploadUrl.url; Console.WriteLine("Created BatchJob with ID {0}, status '{1}' and upload URL {2}.", batchJob.id, batchJob.status, batchJob.uploadUrl.url); // Create the mutate request that will be sent to the upload URL. List <Operation> operations = new List <Operation>(); // Create and add an operation to create a new budget. BudgetOperation budgetOperation = BuildBudgetOperation(); operations.Add(budgetOperation); // Create and add operations to create new campaigns. List <CampaignOperation> campaignOperations = BuildCampaignOperations(budgetOperation.operand.budgetId); operations.AddRange(campaignOperations); // Create and add operations to create new ad groups. List <AdGroupOperation> adGroupOperations = new List <AdGroupOperation>(); foreach (CampaignOperation campaignOperation in campaignOperations) { adGroupOperations.AddRange(BuildAdGroupOperations(campaignOperation.operand.id)); } operations.AddRange(adGroupOperations); // Create and add operations to create new ad group criteria (keywords). foreach (AdGroupOperation adGroupOperation in adGroupOperations) { operations.AddRange(BuildAdGroupAdOperations(adGroupOperation.operand.id)); } // Create and add operations to create new ad group ads (text ads). foreach (AdGroupOperation adGroupOperation in adGroupOperations) { operations.AddRange(BuildAdGroupCriterionOperations(adGroupOperation.operand.id)); } BatchJobUtilities batchJobUploadHelper = new BatchJobUtilities(user); // Create a resumable Upload URL to upload the operations. string resumableUploadUrl = batchJobUploadHelper.GetResumableUploadUrl(uploadUrl); // Use the BatchJobUploadHelper to upload all operations. batchJobUploadHelper.Upload(resumableUploadUrl, operations.ToArray()); bool isCompleted = batchJobUploadHelper.WaitForPendingJob(batchJob.id, TIME_TO_WAIT_FOR_COMPLETION, delegate(BatchJob waitBatchJob, long timeElapsed) { Console.WriteLine("[{0} seconds]: Batch job ID {1} has status '{2}'.", timeElapsed / 1000, waitBatchJob.id, waitBatchJob.status); batchJob = waitBatchJob; return(false); }); if (!isCompleted) { throw new TimeoutException("Job is still in pending state after waiting for " + TIME_TO_WAIT_FOR_COMPLETION + " seconds."); } if (batchJob.processingErrors != null) { foreach (BatchJobProcessingError processingError in batchJob.processingErrors) { Console.WriteLine(" Processing error: {0}, {1}, {2}, {3}, {4}", processingError.ApiErrorType, processingError.trigger, processingError.errorString, processingError.fieldPath, processingError.reason); } } if (batchJob.downloadUrl != null && batchJob.downloadUrl.url != null) { BatchJobMutateResponse mutateResponse = batchJobUploadHelper.Download( batchJob.downloadUrl.url); Console.WriteLine("Downloaded results from {0}.", batchJob.downloadUrl.url); foreach (MutateResult mutateResult in mutateResponse.rval) { String outcome = mutateResult.errorList == null ? "SUCCESS" : "FAILURE"; Console.WriteLine(" Operation [{0}] - {1}", mutateResult.index, outcome); } } } catch (Exception e) { throw new System.ApplicationException("Failed to add campaigns using batch job.", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> public void Run(AdWordsUser user) { // Get the BatchJobService. BatchJobService batchJobService = (BatchJobService)user.GetService( AdWordsService.v201509.BatchJobService); try { // Create a BatchJob. BatchJobOperation addOp = new BatchJobOperation() { @operator = Operator.ADD, operand = new BatchJob() }; BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).value[0]; // Get the upload URL from the new job. string uploadUrl = batchJob.uploadUrl.url; Console.WriteLine("Created BatchJob with ID {0}, status '{1}' and upload URL {2}.", batchJob.id, batchJob.status, batchJob.uploadUrl.url); // Create the mutate request that will be sent to the upload URL. List <Operation> operations = new List <Operation>(); // Create and add an operation to create a new budget. BudgetOperation budgetOperation = BuildBudgetOperation(); operations.Add(budgetOperation); // Create and add operations to create new campaigns. List <CampaignOperation> campaignOperations = BuildCampaignOperations(budgetOperation.operand.budgetId); operations.AddRange(campaignOperations); // Create and add operations to create new ad groups. List <AdGroupOperation> adGroupOperations = new List <AdGroupOperation>(); foreach (CampaignOperation campaignOperation in campaignOperations) { adGroupOperations.AddRange(BuildAdGroupOperations(campaignOperation.operand.id)); } operations.AddRange(adGroupOperations); // Create and add operations to create new ad group criteria (keywords). foreach (AdGroupOperation adGroupOperation in adGroupOperations) { operations.AddRange(BuildAdGroupAdOperations(adGroupOperation.operand.id)); } // Create and add operations to create new ad group ads (text ads). foreach (AdGroupOperation adGroupOperation in adGroupOperations) { operations.AddRange(BuildAdGroupCriterionOperations(adGroupOperation.operand.id)); } BatchJobUtilities batchJobUploadHelper = new BatchJobUtilities(user); // Use the BatchJobUploadHelper to upload all operations. batchJobUploadHelper.Upload(uploadUrl, operations.ToArray()); long pollAttempts = 0; bool isPending = true; do { int sleepMillis = (int)Math.Pow(2, pollAttempts) * POLL_INTERVAL_SECONDS_BASE * 1000; Console.WriteLine("Sleeping {0} millis...", sleepMillis); Thread.Sleep(sleepMillis); Selector selector = new Selector() { fields = new string[] { BatchJob.Fields.Id, BatchJob.Fields.Status, BatchJob.Fields.DownloadUrl, BatchJob.Fields.ProcessingErrors, BatchJob.Fields.ProgressStats }, predicates = new Predicate[] { Predicate.Equals(BatchJob.Fields.Id, batchJob.id) } }; batchJob = batchJobService.get(selector).entries[0]; Console.WriteLine("Batch job ID {0} has status '{1}'.", batchJob.id, batchJob.status); isPending = PENDING_STATUSES.Contains(batchJob.status); } while (isPending && ++pollAttempts <= MAX_RETRIES); if (isPending) { throw new TimeoutException("Job is still in pending state after polling " + MAX_RETRIES + " times."); } if (batchJob.processingErrors != null) { foreach (BatchJobProcessingError processingError in batchJob.processingErrors) { Console.WriteLine(" Processing error: {0}, {1}, {2}, {3}, {4}", processingError.ApiErrorType, processingError.trigger, processingError.errorString, processingError.fieldPath, processingError.reason); } } if (batchJob.downloadUrl != null && batchJob.downloadUrl.url != null) { BatchJobMutateResponse mutateResponse = batchJobUploadHelper.Download( batchJob.downloadUrl.url); Console.WriteLine("Downloaded results from {0}.", batchJob.downloadUrl.url); foreach (MutateResult mutateResult in mutateResponse.rval) { String outcome = mutateResult.errorList == null ? "SUCCESS" : "FAILURE"; Console.WriteLine(" Operation [{0}] - {1}", mutateResult.index, outcome); } } } catch (Exception e) { throw new System.ApplicationException("Failed to add campaigns using batch job.", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad groups to which keywords are /// added.</param> public void Run(AdWordsUser user, long adGroupId) { using (BatchJobService batchJobService = (BatchJobService)user.GetService( AdWordsService.v201708.BatchJobService)) { BatchJobOperation addOp = new BatchJobOperation() { @operator = Operator.ADD, operand = new BatchJob() }; try { BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).value[0]; Console.WriteLine("Created BatchJob with ID {0}, status '{1}' and upload URL {2}.", batchJob.id, batchJob.status, batchJob.uploadUrl.url); List <AdGroupCriterionOperation> operations = CreateOperations(adGroupId); // Create a BatchJobUtilities instance for uploading operations. Use a // chunked upload. BatchJobUtilities batchJobUploadHelper = new BatchJobUtilities(user, true, CHUNK_SIZE); // Create a resumable Upload URL to upload the operations. string resumableUploadUrl = batchJobUploadHelper.GetResumableUploadUrl( batchJob.uploadUrl.url); // Use the BatchJobUploadHelper to upload all operations. batchJobUploadHelper.Upload(resumableUploadUrl, operations.ToArray()); // A flag to determine if the job was requested to be cancelled. This // typically comes from the user. bool wasCancelRequested = false; bool isComplete = batchJobUploadHelper.WaitForPendingJob(batchJob.id, TIME_TO_WAIT_FOR_COMPLETION, delegate(BatchJob waitBatchJob, long timeElapsed) { Console.WriteLine("[{0} seconds]: Batch job ID {1} has status '{2}'.", timeElapsed / 1000, waitBatchJob.id, waitBatchJob.status); batchJob = waitBatchJob; return(wasCancelRequested); }); // Optional: Cancel the job if it has not completed after waiting for // TIME_TO_WAIT_FOR_COMPLETION. bool shouldWaitForCancellation = false; if (!isComplete && wasCancelRequested) { BatchJobError cancellationError = null; try { batchJobUploadHelper.TryToCancelJob(batchJob.id); } catch (AdWordsApiException e) { cancellationError = GetBatchJobError(e); } if (cancellationError == null) { Console.WriteLine("Successfully requested job cancellation."); shouldWaitForCancellation = true; } else { Console.WriteLine("Job cancellation failed. Error says: {0}.", cancellationError.reason); } if (shouldWaitForCancellation) { isComplete = batchJobUploadHelper.WaitForPendingJob(batchJob.id, TIME_TO_WAIT_FOR_COMPLETION, delegate(BatchJob waitBatchJob, long timeElapsed) { Console.WriteLine("[{0} seconds]: Batch job ID {1} has status '{2}'.", timeElapsed / 1000, waitBatchJob.id, waitBatchJob.status); batchJob = waitBatchJob; return(false); }); } } if (!isComplete) { throw new TimeoutException("Job is still in pending state after waiting for " + TIME_TO_WAIT_FOR_COMPLETION + " seconds."); } if (batchJob.processingErrors != null) { foreach (BatchJobProcessingError processingError in batchJob.processingErrors) { Console.WriteLine(" Processing error: {0}, {1}, {2}, {3}, {4}", processingError.ApiErrorType, processingError.trigger, processingError.errorString, processingError.fieldPath, processingError.reason); } } if (batchJob.downloadUrl != null && batchJob.downloadUrl.url != null) { BatchJobMutateResponse mutateResponse = batchJobUploadHelper.Download( batchJob.downloadUrl.url); Console.WriteLine("Downloaded results from {0}.", batchJob.downloadUrl.url); foreach (MutateResult mutateResult in mutateResponse.rval) { String outcome = mutateResult.errorList == null ? "SUCCESS" : "FAILURE"; Console.WriteLine(" Operation [{0}] - {1}", mutateResult.index, outcome); } } else { Console.WriteLine("No results available for download."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create keywords using batch job.", e); } } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad groups to which keywords are /// added.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the MutateJobService. BatchJobService batchJobService = (BatchJobService)user.GetService( AdWordsService.v201603.BatchJobService); BatchJobOperation addOp = new BatchJobOperation() { @operator = Operator.ADD, operand = new BatchJob() }; try { BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).value[0]; Console.WriteLine("Created BatchJob with ID {0}, status '{1}' and upload URL {2}.", batchJob.id, batchJob.status, batchJob.uploadUrl.url); List <AdGroupCriterionOperation> operations = CreateOperations(adGroupId); // Create a BatchJobUtilities instance for uploading operations. Use a // chunked upload. BatchJobUtilities batchJobUploadHelper = new BatchJobUtilities(user, true, CHUNK_SIZE); // Create a resumable Upload URL to upload the operations. string resumableUploadUrl = batchJobUploadHelper.GetResumableUploadUrl( batchJob.uploadUrl.url); // Use the BatchJobUploadHelper to upload all operations. batchJobUploadHelper.Upload(resumableUploadUrl, operations.ToArray()); long pollAttempts = 0; bool isPending = false; batchJob = WaitWhileJobIsPending(batchJobService, batchJob, out isPending, out pollAttempts); // A flag to determine if the job was requested to be cancelled. This // typically comes from the user. bool wasCancelRequested = false; // Optional: Cancel the job if it has not completed after retrying // MAX_RETRIES times. if (isPending && !wasCancelRequested && pollAttempts == MAX_RETRIES) { batchJob = CancelJob(batchJobService, batchJob); batchJob = WaitWhileJobIsPending(batchJobService, batchJob, out isPending, out pollAttempts); } if (isPending) { throw new TimeoutException("Job is still in pending state after polling " + MAX_RETRIES + " times."); } if (batchJob.processingErrors != null) { foreach (BatchJobProcessingError processingError in batchJob.processingErrors) { Console.WriteLine(" Processing error: {0}, {1}, {2}, {3}, {4}", processingError.ApiErrorType, processingError.trigger, processingError.errorString, processingError.fieldPath, processingError.reason); } } if (batchJob.downloadUrl != null && batchJob.downloadUrl.url != null) { BatchJobMutateResponse mutateResponse = batchJobUploadHelper.Download( batchJob.downloadUrl.url); Console.WriteLine("Downloaded results from {0}.", batchJob.downloadUrl.url); foreach (MutateResult mutateResult in mutateResponse.rval) { String outcome = mutateResult.errorList == null ? "SUCCESS" : "FAILURE"; Console.WriteLine(" Operation [{0}] - {1}", mutateResult.index, outcome); } } else { Console.WriteLine("No results available for download."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create keywords using batch job.", e); } }