/// <summary>Snippet for Mutate</summary> /// <remarks> /// This snippet has been automatically generated for illustrative purposes only. /// It may require modifications to work in your environment. /// </remarks> public void Mutate() { // Create client GoogleAdsServiceClient googleAdsServiceClient = GoogleAdsServiceClient.Create(); // Initialize request argument(s) string customerId = ""; IEnumerable <MutateOperation> mutateOperations = new MutateOperation[] { new MutateOperation(), }; // Make the request MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(customerId, mutateOperations); }
/// <summary> /// Execute a list of operations and return results. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="operations">The list of operations.</param> /// <returns>The list of mutate responses from executing the operations.</returns> internal static IEnumerable <MutateOperationResponse> ExecuteOperations( GoogleAdsClient client, MutateOperation[] operations) { GoogleAdsServiceClient googleAdsService = client.GetService( Services.V6.GoogleAdsService); MutateGoogleAdsRequest request = new MutateGoogleAdsRequest() { CustomerId = client.Config.ClientCustomerId.ToString(), }; request.MutateOperations.AddRange(operations); return(googleAdsService.Mutate(request).MutateOperationResponses); }
// [END add_performance_max_campaign_4] // [START add_performance_max_campaign_5] /// <summary> /// Creates multiple text assets and returns the list of resource names. /// </summary> /// <param name="client">The Google Ads Client.</param> /// <param name="customerId">The customer's ID.</param> /// <param name="texts">The texts to add.</param> /// <returns>A list of asset resource names.</returns> private List <string> CreateMultipleTextAssets( GoogleAdsClient client, long customerId, string[] texts) { // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsServiceClient = client.GetService(Services.V10.GoogleAdsService); MutateGoogleAdsRequest request = new MutateGoogleAdsRequest() { CustomerId = customerId.ToString() }; foreach (string text in texts) { request.MutateOperations.Add( new MutateOperation() { AssetOperation = new AssetOperation() { Create = new Asset() { TextAsset = new TextAsset() { Text = text } } } } ); } // Send the operations in a single Mutate request. MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(request); List <string> assetResourceNames = new List <string>(); foreach (MutateOperationResponse operationResponse in response.MutateOperationResponses) { MutateAssetResult assetResult = operationResponse.AssetResult; assetResourceNames.Add(assetResult.ResourceName); } PrintResponseDetails(response); return(assetResourceNames); }
/// <summary>Snippet for Mutate</summary> /// <remarks> /// This snippet has been automatically generated for illustrative purposes only. /// It may require modifications to work in your environment. /// </remarks> public void MutateRequestObject() { // Create client GoogleAdsServiceClient googleAdsServiceClient = GoogleAdsServiceClient.Create(); // Initialize request argument(s) MutateGoogleAdsRequest request = new MutateGoogleAdsRequest { CustomerId = "", MutateOperations = { new MutateOperation(), }, PartialFailure = false, ValidateOnly = false, }; // Make the request MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(request); }
/// <summary>Snippet for Mutate</summary> public void MutateRequestObject() { // Snippet: Mutate(MutateGoogleAdsRequest, CallSettings) // Create client GoogleAdsServiceClient googleAdsServiceClient = GoogleAdsServiceClient.Create(); // Initialize request argument(s) MutateGoogleAdsRequest request = new MutateGoogleAdsRequest { CustomerId = "", MutateOperations = { new MutateOperation(), }, PartialFailure = false, ValidateOnly = false, ResponseContentType = ResponseContentTypeEnum.Types.ResponseContentType.Unspecified, }; // Make the request MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(request); // End snippet }
/// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads API client.</param> /// <param name="customerId">The Google Ads customer ID.</param> /// <param name="campaignId">ID of the campaign from which sitelinks will be removed. /// </param> // [START remove_entire_sitelink_campaign_extension_setting] public void Run(GoogleAdsClient client, long customerId, long campaignId) { // Get the GoogleAdsService client. GoogleAdsServiceClient googleAdsServiceClient = client.GetService(Services.V10.GoogleAdsService); List <MutateOperation> mutateOperations = new List <MutateOperation>(); try { // Create a mutate operation the contains the campaign extension setting operation // to remove the specified sitelink campaign extension setting. mutateOperations.Add( CreateSitelinkCampaignExtensionSettingMutateOperation(customerId, campaignId)); // Get all sitelink extension feed items of the specified campaign. List <string> extensionFeedItemResourceNames = GetAllSitelinkExtensionFeedItems(googleAdsServiceClient, customerId, campaignId); // Create mutate operations, each of which contains an extension feed item // operation to remove the specified extension feed items. mutateOperations.AddRange(CreateExtensionFeedItemMutateOperations( extensionFeedItemResourceNames)); // Issue a mutate request to remove the campaign extension setting and its // extension feed items. MutateGoogleAdsResponse mutateGoogleAdsResponse = googleAdsServiceClient.Mutate( customerId .ToString(), mutateOperations); RepeatedField <MutateOperationResponse> mutateOpResponses = mutateGoogleAdsResponse.MutateOperationResponses; // Print the information on the removed campaign extension setting and its // extension feed items. // Each mutate operation response is returned in the same order as we passed its // corresponding operation. Therefore, the first belongs to the campaign setting // operation, and the rest belong to the extension feed item operations. Console.WriteLine("Removed a campaign extension setting with resource name: " + $"'{mutateOpResponses.First().CampaignExtensionSettingResult.ResourceName}'."); foreach (MutateOperationResponse response in mutateOpResponses.Skip(1)) { Console.WriteLine("Removed an extension feed item with resource name: " + $"'{response.ExtensionFeedItemResult.ResourceName}'."); } } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } catch (ArgumentException e) { Console.WriteLine("Argument Exception:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine(e.StackTrace); throw; } }
// [START add_performance_max_campaign_1] /// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID.</param> /// <param name="audienceId">The optional audience ID.</param> public void Run(GoogleAdsClient client, long customerId, long?audienceId) { try { // [START add_performance_max_campaign_1] GoogleAdsServiceClient googleAdsServiceClient = client.GetService(Services.V10.GoogleAdsService); // Performance Max campaigns require that repeated assets such as headlines and // descriptions be created before the campaign. // // For the list of required assets for a Performance Max campaign, see // https://developers.google.com/google-ads/api/docs/performance-max/assets // // Create the headlines. List <string> headlineAssetResourceNames = CreateMultipleTextAssets( client, customerId, new[] { "Travel", "Travel Reviews", "Book travel" } ); // Create the descriptions. List <string> descriptionAssetResourceNames = CreateMultipleTextAssets( client, customerId, new[] { "Take to the air!", "Fly to the sky!" } ); string tempResourceNameCampaignBudget = ResourceNames.CampaignBudget( customerId, TEMPORARY_ID_BUDGET ); // The below methods create and return MutateOperations that we later provide to // the GoogleAdsService.Mutate method in order to create the entities in a single // request. Since the entities for a Performance Max campaign are closely tied to // one-another, it is considered a best practice to create them in a single Mutate // request so they all complete successfully or fail entirely, leaving no // orphaned entities. // // See: https://developers.google.com/google-ads/api/docs/mutating/overview MutateOperation campaignBudgetOperation = CreateCampaignBudgetOperation( tempResourceNameCampaignBudget ); string tempResourceNameCampaign = ResourceNames.Campaign( customerId, TEMPORARY_ID_CAMPAIGN ); MutateOperation performanceMaxCampaignOperation = CreatePerformanceMaxCampaignOperation( tempResourceNameCampaign, tempResourceNameCampaignBudget ); List <MutateOperation> campaignCriterionOperations = CreateCampaignCriterionOperations(tempResourceNameCampaign); List <MutateOperation> assetGroupOperations = CreateAssetGroupOperations( tempResourceNameCampaign, ResourceNames.AssetGroup(customerId, TEMPORARY_ID_ASSET_GROUP), headlineAssetResourceNames, descriptionAssetResourceNames, new AssetGroupAssetTemporaryResourceNameGenerator( customerId, TEMPORARY_ID_ASSET_GROUP ), client.Config ); List <MutateOperation> assetGroupSignalOperations = CreateAssetGroupSignalOperations( customerId, ResourceNames.AssetGroup(customerId, TEMPORARY_ID_ASSET_GROUP), audienceId ); MutateGoogleAdsRequest request = new MutateGoogleAdsRequest { CustomerId = customerId.ToString() }; // It's important to create these entities in this order because they depend on // each other. // // Additionally, we take several lists of operations and flatten them into one // large list. request.MutateOperations.Add(campaignBudgetOperation); request.MutateOperations.Add(performanceMaxCampaignOperation); request.MutateOperations.AddRange(campaignCriterionOperations); request.MutateOperations.AddRange(assetGroupOperations); request.MutateOperations.AddRange(assetGroupSignalOperations); MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(request); PrintResponseDetails(response); // [END add_performance_max_campaign_1] } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID.</param> /// <param name="keywordText">A keyword string used for generating keyword themes.</param> /// <param name="freeFormKeywordText">A keyword used to create a free-form keyword theme. /// </param> /// <param name="businessLocationId">The ID of a Business Profile location.</param> /// <param name="businessName">The name of a Business Profile business.</param> public void Run(GoogleAdsClient client, long customerId, string keywordText, string freeFormKeywordText, ulong?businessLocationId, string businessName) { GoogleAdsServiceClient googleAdsServiceClient = client.GetService(Services.V10.GoogleAdsService); try { // [START add_smart_campaign_12] // Gets the SmartCampaignSuggestionInfo object which acts as the basis for many // of the entities necessary to create a Smart campaign. It will be reused a number // of times to retrieve suggestions for keyword themes, budget amount, ad //creatives, and campaign criteria. SmartCampaignSuggestionInfo suggestionInfo = GetSmartCampaignSuggestionInfo(client, businessLocationId, businessName); // Generates a list of keyword themes using the SuggestKeywordThemes method on the // SmartCampaignSuggestService. It is strongly recommended that you use this // strategy for generating keyword themes. List <KeywordThemeConstant> keywordThemeConstants = GetKeywordThemeSuggestions(client, customerId, suggestionInfo); // Optionally retrieves auto-complete suggestions for the given keyword text and // adds them to the list of keyWordThemeConstants. if (keywordText != null) { keywordThemeConstants.AddRange(GetKeywordTextAutoCompletions( client, keywordText)); } // Converts the KeywordThemeConstants to KeywordThemeInfos. List <KeywordThemeInfo> keywordThemeInfos = keywordThemeConstants.Select( constant => new KeywordThemeInfo { KeywordThemeConstant = constant.ResourceName }) .ToList(); // Optionally includes any freeform keywords verbatim. if (freeFormKeywordText != null) { keywordThemeInfos.Add(new KeywordThemeInfo() { FreeFormKeywordTheme = freeFormKeywordText }); } // Includes the keyword suggestions in the overall SuggestionInfo object. // [START add_smart_campaign_13] suggestionInfo.KeywordThemes.Add(keywordThemeInfos); // [END add_smart_campaign_13] // [END add_smart_campaign_12] SmartCampaignAdInfo adSuggestions = GetAdSuggestions(client, customerId, suggestionInfo); long suggestedBudgetAmount = GetBudgetSuggestion(client, customerId, suggestionInfo); // [START add_smart_campaign_7] // The below methods create and return MutateOperations that we later provide to // the GoogleAdsService.Mutate method in order to create the entities in a single // request. Since the entities for a Smart campaign are closely tied to one-another // it's considered a best practice to create them in a single Mutate request; the // entities will either all complete successfully or fail entirely, leaving no // orphaned entities. See: // https://developers.google.com/google-ads/api/docs/mutating/overview MutateOperation campaignBudgetOperation = CreateCampaignBudgetOperation(customerId, suggestedBudgetAmount); MutateOperation smartCampaignOperation = CreateSmartCampaignOperation(customerId); MutateOperation smartCampaignSettingOperation = CreateSmartCampaignSettingOperation(customerId, businessLocationId, businessName); IEnumerable <MutateOperation> campaignCriterionOperations = CreateCampaignCriterionOperations(customerId, keywordThemeInfos, suggestionInfo); MutateOperation adGroupOperation = CreateAdGroupOperation(customerId); MutateOperation adGroupAdOperation = CreateAdGroupAdOperation(customerId, adSuggestions); // Send the operations in a single mutate request. MutateGoogleAdsRequest mutateGoogleAdsRequest = new MutateGoogleAdsRequest { CustomerId = customerId.ToString() }; // It's important to create these entities in this order because they depend on // each other, for example the SmartCampaignSetting and ad group depend on the // campaign, and the ad group ad depends on the ad group. mutateGoogleAdsRequest.MutateOperations.Add(campaignBudgetOperation); mutateGoogleAdsRequest.MutateOperations.Add(smartCampaignOperation); mutateGoogleAdsRequest.MutateOperations.Add(smartCampaignSettingOperation); mutateGoogleAdsRequest.MutateOperations.Add(campaignCriterionOperations); mutateGoogleAdsRequest.MutateOperations.Add(adGroupOperation); mutateGoogleAdsRequest.MutateOperations.Add(adGroupAdOperation); MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(mutateGoogleAdsRequest); PrintResponseDetails(response); // [END add_smart_campaign_7] } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } }
// [START add_performance_max_product_listing_group_tree] /// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID.</param> /// <param name="assetGroupId">The asset group id for the Performance Max campaign.</param> /// <param name="replaceExistingTree">Option to remove existing product tree /// from the passed in asset group.</param> public void Run( GoogleAdsClient client, long customerId, long assetGroupId, bool replaceExistingTree) { GoogleAdsServiceClient googleAdsServiceClient = client.GetService(Services.V10.GoogleAdsService); string assetGroupResourceName = ResourceNames.AssetGroup(customerId, assetGroupId); // We use a factory to create all the MutateOperations that manipulate a specific // AssetGroup for a specific customer. The operations returned by the factory's methods // are used to optionally remove all AssetGroupListingGroupFilters from the tree, and // then to construct a new tree of filters. These filters can have a parent-child // relationship, and also include a special root that includes all children. // // When creating these filters, we use temporary IDs to create the hierarchy between // the root listing group filter, and the subdivisions and leave nodes beneath that. // // The factory specific to a customerId and assetGroupId is created below. AssetGroupListingGroupFilterCreateOperationFactory createOperationFactory = new AssetGroupListingGroupFilterCreateOperationFactory( customerId, assetGroupId, TEMPORARY_ID_LISTING_GROUP_ROOT ); MutateGoogleAdsRequest request = new MutateGoogleAdsRequest { CustomerId = customerId.ToString() }; if (replaceExistingTree) { List <AssetGroupListingGroupFilter> existingListingGroupFilters = GetAllExistingListingGroupFilterAssetsInAssetGroup( client, customerId, assetGroupResourceName ); if (existingListingGroupFilters.Count > 0) { // A special factory object that ensures the creation of remove operations in the // correct order (child listing group filters must be removed before their parents). AssetGroupListingGroupFilterRemoveOperationFactory removeOperationFactory = new AssetGroupListingGroupFilterRemoveOperationFactory( existingListingGroupFilters ); request.MutateOperations.AddRange(removeOperationFactory.RemoveAll()); } } request.MutateOperations.Add(createOperationFactory.CreateRoot()); request.MutateOperations.Add( createOperationFactory.CreateUnit( TEMPORARY_ID_LISTING_GROUP_ROOT, createOperationFactory.NextId(), new ListingGroupFilterDimension() { ProductCondition = new ListingGroupFilterDimension.Types.ProductCondition() { Condition = ListingGroupFilterProductCondition.New } } ) ); request.MutateOperations.Add( createOperationFactory.CreateUnit( TEMPORARY_ID_LISTING_GROUP_ROOT, createOperationFactory.NextId(), new ListingGroupFilterDimension() { ProductCondition = new ListingGroupFilterDimension.Types.ProductCondition() { Condition = ListingGroupFilterProductCondition.Used } } ) ); // We save this ID because create child nodes underneath it. long subdivisionIdConditionOther = createOperationFactory.NextId(); request.MutateOperations.Add( // We're calling CreateSubdivision because this listing group will have children. createOperationFactory.CreateSubdivision( TEMPORARY_ID_LISTING_GROUP_ROOT, subdivisionIdConditionOther, new ListingGroupFilterDimension() { // All sibling nodes must have the same dimension type. We use an empty // ProductCondition to indicate that this is an "Other" partition. ProductCondition = new ListingGroupFilterDimension.Types.ProductCondition() } ) ); request.MutateOperations.Add( createOperationFactory.CreateUnit( subdivisionIdConditionOther, createOperationFactory.NextId(), new ListingGroupFilterDimension() { ProductBrand = new ProductBrand() { Value = "CoolBrand" } } ) ); request.MutateOperations.Add( createOperationFactory.CreateUnit( subdivisionIdConditionOther, createOperationFactory.NextId(), new ListingGroupFilterDimension() { ProductBrand = new ProductBrand() { Value = "CheapBrand" } } ) ); request.MutateOperations.Add( createOperationFactory.CreateUnit( subdivisionIdConditionOther, createOperationFactory.NextId(), new ListingGroupFilterDimension() { ProductBrand = new ProductBrand() } ) ); MutateGoogleAdsResponse response = googleAdsServiceClient.Mutate(request); PrintResponseDetails(request, response); }