// [END createBudget] MOE:strip_line /// <summary> /// Creates the campaign. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="budget">The campaign budget.</param> /// <returns>The newly created campaign.</returns> // [START createCampaign] MOE:strip_line private static Campaign CreateCampaign(AdWordsUser user, Budget budget) { // Get the CampaignService. CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201705.CampaignService); // Create a Dynamic Search Ads campaign. Campaign campaign = new Campaign(); campaign.name = "Interplanetary Cruise #" + ExampleUtilities.GetRandomString(); campaign.advertisingChannelType = AdvertisingChannelType.SEARCH; // Recommendation: Set the campaign to PAUSED when creating it to prevent // the ads from immediately serving. Set to ENABLED once you've added // targeting and the ads are ready to serve. campaign.status = CampaignStatus.PAUSED; BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration(); biddingConfig.biddingStrategyType = BiddingStrategyType.MANUAL_CPC; campaign.biddingStrategyConfiguration = biddingConfig; campaign.budget = new Budget(); campaign.budget.budgetId = budget.budgetId; // Required: Set the campaign's Dynamic Search Ads settings. DynamicSearchAdsSetting dynamicSearchAdsSetting = new DynamicSearchAdsSetting(); // Required: Set the domain name and language. dynamicSearchAdsSetting.domainName = "example.com"; dynamicSearchAdsSetting.languageCode = "en"; // Set the campaign settings. campaign.settings = new Setting[] { dynamicSearchAdsSetting }; // Optional: Set the start date. campaign.startDate = DateTime.Now.AddDays(1).ToString("yyyyMMdd"); // Optional: Set the end date. campaign.endDate = DateTime.Now.AddYears(1).ToString("yyyyMMdd"); // Create the operation. CampaignOperation operation = new CampaignOperation(); operation.@operator = Operator.ADD; operation.operand = campaign; try { // Add the campaign. CampaignReturnValue retVal = campaignService.mutate(new CampaignOperation[] { operation }); // Display the results. Campaign newCampaign = retVal.value[0]; Console.WriteLine("Campaign with id = '{0}' and name = '{1}' was added.", newCampaign.id, newCampaign.name); return(newCampaign); } catch (Exception e) { throw new System.ApplicationException("Failed to add campaigns.", e); } }
/// <summary> /// Returns the DSA settings for a campaign. Throws an error if the campaign does not /// exist or is not a DSA campaign. /// </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">ID of the campaign for which DSA settings are fetched.</param> /// <returns>The DSA settings.</returns> private DynamicSearchAdsSetting GetDsaSetting(GoogleAdsClient client, long customerId, long campaignId) { // Get the GoogleAdsService. GoogleAdsServiceClient googleAdsService = client.GetService( Services.V3.GoogleAdsService); // Creates the query. // You must request all DSA fields in order to update the DSA settings in the // following step. string query = "SELECT " + "campaign.id, " + "campaign.name, " + "campaign.dynamic_search_ads_setting.domain_name, " + "campaign.dynamic_search_ads_setting.language_code, " + "campaign.dynamic_search_ads_setting.use_supplied_urls_only " + "FROM " + "campaign " + "WHERE " + "campaign.id = " + campaignId; GoogleAdsRow result = googleAdsService.Search( customerId.ToString(), query).FirstOrDefault(); if (result == null) { throw new Exception("No campaign found with ID: " + campaignId); } // Gets the DSA settings. DynamicSearchAdsSetting dynamicSearchAdsSetting = result.Campaign.DynamicSearchAdsSetting; // Throws an exception if the campaign is not a DSA campaign. if (dynamicSearchAdsSetting == null || string.IsNullOrEmpty( dynamicSearchAdsSetting.DomainName)) { throw new Exception($"Campaign with ID {campaignId} is not a DSA campaign."); } return(dynamicSearchAdsSetting); }
/// <summary> /// Updates a campaign to set the DSA feed. /// </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="feedResourceName">The DSA feed resource name</param> /// <param name="campaignId">ID of the campaign for which DSA settings are updated.</param> private void UpdateCampaignDsaSetting(GoogleAdsClient client, long customerId, string feedResourceName, long campaignId) { // Get the CampaignService. CampaignServiceClient campaignService = client.GetService( Services.V3.CampaignService); DynamicSearchAdsSetting dsaSetting = GetDsaSetting(client, customerId, campaignId); dsaSetting.Feeds.Add(feedResourceName); // Create the campaign. Campaign campaign = new Campaign() { ResourceName = ResourceNames.Campaign(customerId, campaignId), DynamicSearchAdsSetting = dsaSetting }; // Create the operation. CampaignOperation operation = new CampaignOperation() { Update = campaign, UpdateMask = FieldMasks.AllSetFieldsOf(campaign) }; // Update the campaign. MutateCampaignsResponse response = campaignService.MutateCampaigns(customerId.ToString(), new[] { operation }); // Display the results. foreach (MutateCampaignResult mutateCampaignResult in response.Results) { Console.WriteLine($"Updated campaign with resourceName: " + $"'{mutateCampaignResult.ResourceName}'."); } }
/// <summary> /// Updates the campaign DSA setting to add DSA pagefeeds. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">The Campaign ID.</param> /// <param name="feedId">The page feed ID.</param> private static void UpdateCampaignDsaSetting(AdWordsUser user, long campaignId, long feedId) { using (CampaignService campaignService = (CampaignService)user.GetService( AdWordsService.v201802.CampaignService)) { Selector selector = new Selector() { fields = new string[] { Campaign.Fields.Id, Campaign.Fields.Settings }, predicates = new Predicate[] { Predicate.Equals(Campaign.Fields.Id, campaignId) }, paging = Paging.Default }; CampaignPage page = campaignService.get(selector); if (page == null || page.entries == null || page.entries.Length == 0) { throw new System.ApplicationException(string.Format( "Failed to retrieve campaign with ID = {0}.", campaignId)); } Campaign campaign = page.entries[0]; if (campaign.settings == null) { throw new System.ApplicationException("This is not a DSA campaign."); } DynamicSearchAdsSetting dsaSetting = null; Setting[] campaignSettings = campaign.settings; for (int i = 0; i < campaign.settings.Length; i++) { Setting setting = campaignSettings[i]; if (setting is DynamicSearchAdsSetting) { dsaSetting = (DynamicSearchAdsSetting)setting; break; } } if (dsaSetting == null) { throw new System.ApplicationException("This is not a DSA campaign."); } // Use a page feed to specify precisely which URLs to use with your // Dynamic Search Ads. dsaSetting.pageFeed = new PageFeed() { feedIds = new long[] { feedId }, }; // Optional: Specify whether only the supplied URLs should be used with your // Dynamic Search Ads. dsaSetting.useSuppliedUrlsOnly = true; Campaign campaignToUpdate = new Campaign(); campaignToUpdate.id = campaignId; campaignToUpdate.settings = campaignSettings; CampaignOperation operation = new CampaignOperation(); operation.operand = campaignToUpdate; operation.@operator = Operator.SET; try { CampaignReturnValue retval = campaignService.mutate( new CampaignOperation[] { operation }); Campaign updatedCampaign = retval.value[0]; Console.WriteLine("DSA page feed for campaign ID '{0}' was updated with feed ID '{1}'.", updatedCampaign.id, feedId); } catch (Exception e) { throw new System.ApplicationException("Failed to set page feed for campaign.", e); } } }
/// <summary> /// Creates a test campaign for running further tests. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="channelType">The advertising channel type for this /// campaign.</param> /// <param name="strategyType">The bidding strategy to be used for /// this campaign.</param> /// <param name="isMobile">True, if this campaign is mobile-only, false /// otherwise.</param> /// <param name="isDsa">True, if this campaign is for DSA, false /// otherwise.</param> /// <returns>The campaign id.</returns> public long CreateCampaign(AdWordsUser user, AdvertisingChannelType channelType, BiddingStrategyType strategyType, bool isMobile, bool isDsa) { CampaignService campaignService = (CampaignService)user.GetService(AdWordsService.v201705.CampaignService); Campaign campaign = new Campaign() { name = string.Format("Campaign {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff")), advertisingChannelType = channelType, // Set the test campaign to PAUSED when creating it to prevent the ads from serving. status = CampaignStatus.PAUSED, biddingStrategyConfiguration = new BiddingStrategyConfiguration() { biddingStrategyType = strategyType }, budget = new Budget() { budgetId = CreateBudget(user), amount = new Money() { microAmount = 100000000, }, deliveryMethod = BudgetBudgetDeliveryMethod.STANDARD } }; if (isMobile) { switch (campaign.advertisingChannelType) { case AdvertisingChannelType.SEARCH: campaign.advertisingChannelSubType = AdvertisingChannelSubType.SEARCH_MOBILE_APP; break; case AdvertisingChannelType.DISPLAY: campaign.advertisingChannelSubType = AdvertisingChannelSubType.DISPLAY_MOBILE_APP; break; } } List <Setting> settings = new List <Setting>(); if (channelType == AdvertisingChannelType.SHOPPING) { // All Shopping campaigns need a ShoppingSetting. ShoppingSetting shoppingSetting = new ShoppingSetting() { salesCountry = "US", campaignPriority = 0, merchantId = (user.Config as AdWordsAppConfig).MerchantCenterId }; settings.Add(shoppingSetting); } if (isDsa) { // Required: Set the campaign's Dynamic Search Ads settings. DynamicSearchAdsSetting dynamicSearchAdsSetting = new DynamicSearchAdsSetting(); // Required: Set the domain name and language. dynamicSearchAdsSetting.domainName = "example.com"; dynamicSearchAdsSetting.languageCode = "en"; settings.Add(dynamicSearchAdsSetting); } campaign.settings = settings.ToArray(); CampaignOperation campaignOperation = new CampaignOperation() { @operator = Operator.ADD, operand = campaign }; CampaignReturnValue retVal = campaignService.mutate(new CampaignOperation[] { campaignOperation }); return(retVal.value[0].id); }