Ejemplo n.º 1
0
        /// <summary>
        /// Adds a set of keywords to a shared set.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="sharedSetId">The shared set id.</param>
        /// <param name="keywordTexts">The keywords to be added to the shared set.</param>
        /// <returns>The newly added set of shared criteria.</returns>
        public SharedCriterion[] AddKeywordsToSharedSet(AdWordsUser user, long sharedSetId,
                                                        string[] keywordTexts)
        {
            // Get the SharedCriterionService.
            SharedCriterionService sharedSetService = (SharedCriterionService)
                                                      user.GetService(AdWordsService.v201406.SharedCriterionService);

            List <SharedCriterionOperation> operations = new List <SharedCriterionOperation>();

            foreach (string keywordText in keywordTexts)
            {
                Keyword keyword = new Keyword();
                keyword.text      = keywordText;
                keyword.matchType = KeywordMatchType.BROAD;

                SharedCriterion sharedCriterion = new SharedCriterion();
                sharedCriterion.criterion   = keyword;
                sharedCriterion.negative    = true;
                sharedCriterion.sharedSetId = sharedSetId;
                SharedCriterionOperation operation = new SharedCriterionOperation();
                operation.@operator = Operator.ADD;
                operation.operand   = sharedCriterion;
                operations.Add(operation);
            }

            SharedCriterionReturnValue retval = sharedSetService.mutate(operations.ToArray());

            return(retval.value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a set of keywords to a shared set.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="sharedSetId">The shared set id.</param>
        /// <param name="keywordTexts">The keywords to be added to the shared set.</param>
        /// <returns>The newly added set of shared criteria.</returns>
        public SharedCriterion[] AddKeywordsToSharedSet(AdWordsUser user, long sharedSetId,
                                                        string[] keywordTexts)
        {
            using (SharedCriterionService sharedCriterionService = (SharedCriterionService)
                                                                   user.GetService(AdWordsService.v201802.SharedCriterionService)) {
                List <SharedCriterionOperation> operations = new List <SharedCriterionOperation>();
                foreach (string keywordText in keywordTexts)
                {
                    Keyword keyword = new Keyword {
                        text      = keywordText,
                        matchType = KeywordMatchType.BROAD
                    };

                    SharedCriterion sharedCriterion = new SharedCriterion {
                        criterion   = keyword,
                        negative    = true,
                        sharedSetId = sharedSetId
                    };
                    SharedCriterionOperation operation = new SharedCriterionOperation {
                        @operator = Operator.ADD,
                        operand   = sharedCriterion
                    };
                    operations.Add(operation);
                }

                SharedCriterionReturnValue retval = sharedCriterionService.mutate(operations.ToArray());
                return(retval.value);
            }
        }
 /// <summary>Snippet for MutateSharedCriteria</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void MutateSharedCriteria()
 {
     // Create client
     SharedCriterionServiceClient sharedCriterionServiceClient = SharedCriterionServiceClient.Create();
     // Initialize request argument(s)
     string customerId = "";
     IEnumerable <SharedCriterionOperation> operations = new SharedCriterionOperation[]
     {
         new SharedCriterionOperation(),
     };
     // Make the request
     MutateSharedCriteriaResponse response = sharedCriterionServiceClient.MutateSharedCriteria(customerId, operations);
 }
Ejemplo n.º 4
0
        /// <summary>Snippet for MutateSharedCriteriaAsync</summary>
        public async Task MutateSharedCriteriaAsync()
        {
            // Snippet: MutateSharedCriteriaAsync(string, IEnumerable<SharedCriterionOperation>, CallSettings)
            // Additional: MutateSharedCriteriaAsync(string, IEnumerable<SharedCriterionOperation>, CancellationToken)
            // Create client
            SharedCriterionServiceClient sharedCriterionServiceClient = await SharedCriterionServiceClient.CreateAsync();

            // Initialize request argument(s)
            string customerId = "";
            IEnumerable <SharedCriterionOperation> operations = new SharedCriterionOperation[]
            {
                new SharedCriterionOperation(),
            };
            // Make the request
            MutateSharedCriteriaResponse response = await sharedCriterionServiceClient.MutateSharedCriteriaAsync(customerId, operations);

            // End snippet
        }
        /// <summary>
        /// Adds a set of keywords to a shared set.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="sharedSetId">The shared set id.</param>
        /// <param name="keywordTexts">The keywords to be added to the shared set.</param>
        /// <returns>The newly added set of shared criteria.</returns>
        public SharedCriterion[] AddKeywordsToSharedSet(AdWordsUser user, long sharedSetId,
        string[] keywordTexts)
        {
            // Get the SharedCriterionService.
              SharedCriterionService sharedCriterionService = (SharedCriterionService)
              user.GetService(AdWordsService.v201601.SharedCriterionService);

              List<SharedCriterionOperation> operations = new List<SharedCriterionOperation>();
              foreach (string keywordText in keywordTexts) {
            Keyword keyword = new Keyword();
            keyword.text = keywordText;
            keyword.matchType = KeywordMatchType.BROAD;

            SharedCriterion sharedCriterion = new SharedCriterion();
            sharedCriterion.criterion = keyword;
            sharedCriterion.negative = true;
            sharedCriterion.sharedSetId = sharedSetId;
            SharedCriterionOperation operation = new SharedCriterionOperation();
            operation.@operator = Operator.ADD;
            operation.operand = sharedCriterion;
            operations.Add(operation);
              }

              SharedCriterionReturnValue retval = sharedCriterionService.mutate(operations.ToArray());
              return retval.value;
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <param name="campaignId">The ID of the campaign for which shared criterion is updated.
        /// </param>
        public void Run(GoogleAdsClient client, long customerId, long campaignId)
        {
            try
            {
                SharedCriterionServiceClient sharedCriterionService = client.GetService(
                    Services.V3.SharedCriterionService);

                GoogleAdsServiceClient googleAdsService = client.GetService(
                    Services.V3.GoogleAdsService);

                List <long?>  sharedSetIds       = new List <long?>();
                List <string> criterionResources = new List <string>();

                // First, retrieve all shared sets associated with the campaign.
                string sharedSetQuery = $"SELECT shared_set.id, shared_set.name FROM " +
                                        $"campaign_shared_set WHERE campaign.id = {campaignId}";

                PagedEnumerable <SearchGoogleAdsResponse, GoogleAdsRow> sharedSetResponse =
                    googleAdsService.Search(customerId.ToString(), sharedSetQuery);

                // Display the results.
                foreach (GoogleAdsRow googleAdsRow in sharedSetResponse)
                {
                    SharedSet sharedSet = googleAdsRow.SharedSet;
                    Console.WriteLine("Campaign shared set ID {0} and name '{1}' was found.",
                                      sharedSet.Id, sharedSet.Name);

                    sharedSetIds.Add(sharedSet.Id);
                }

                // Next, retrieve shared criteria for all found shared sets.
                string sharedCriterionQuery =
                    "SELECT shared_criterion.type, shared_criterion.keyword.text, " +
                    "shared_criterion.keyword.match_type, shared_set.id FROM shared_criterion " +
                    $"WHERE shared_set.id IN(" +
                    string.Join(",", sharedSetIds.ConvertAll(x => x.ToString())) + ")";

                PagedEnumerable <SearchGoogleAdsResponse, GoogleAdsRow> sharedCriterionResponse =
                    googleAdsService.Search(customerId.ToString(), sharedCriterionQuery);

                // Display the results.
                foreach (GoogleAdsRow googleAdsRow in sharedCriterionResponse)
                {
                    SharedCriterion sharedCriterion = googleAdsRow.SharedCriterion;
                    if (sharedCriterion.Type == CriterionType.Keyword)
                    {
                        Console.WriteLine("Shared criterion with resource name '{0}' for " +
                                          "negative keyword with text '{1}' and match type '{2}' was found.",
                                          sharedCriterion.ResourceName,
                                          sharedCriterion.Keyword.Text,
                                          sharedCriterion.Keyword.MatchType);
                    }
                    else
                    {
                        Console.WriteLine("Shared criterion with resource name '{0}' was found.",
                                          sharedCriterion.ResourceName);
                    }
                    criterionResources.Add(sharedCriterion.ResourceName);
                }

                // Finally, remove the criteria.
                List <SharedCriterionOperation> operations = new List <SharedCriterionOperation>();
                foreach (string criterionResource in criterionResources)
                {
                    SharedCriterionOperation operation = new SharedCriterionOperation()
                    {
                        Remove = criterionResource
                    };
                    operations.Add(operation);
                }

                MutateSharedCriteriaResponse response =
                    sharedCriterionService.MutateSharedCriteria(
                        customerId.ToString(), operations);

                foreach (MutateSharedCriterionResult result in response.Results)
                {
                    Console.WriteLine($"Removed shared criterion {result.ResourceName}.");
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
            }
        }