Campaign budget
You can use an existing campaign budget or create a new one:
Java
private static String createCampaignBudget(GoogleAdsClient googleAdsClient, long customerId) { // Creates the budget. CampaignBudget campaignBudget = CampaignBudget.newBuilder() .setName("Interplanetary Cruise Budget #" + getPrintableDateTime()) .setAmountMicros(5_000_000) .setDeliveryMethod(BudgetDeliveryMethod.STANDARD) .build(); // Creates the operation. CampaignBudgetOperation operation = CampaignBudgetOperation.newBuilder().setCreate(campaignBudget).build(); // Creates the campaign budget service client. try (CampaignBudgetServiceClient campaignBudgetServiceClient = googleAdsClient.getLatestVersion().createCampaignBudgetServiceClient()) { // Adds the campaign budget. MutateCampaignBudgetsResponse response = campaignBudgetServiceClient.mutateCampaignBudgets( Long.toString(customerId), ImmutableList.of(operation)); // Displays the result. String budgetResourceName = response.getResults(0).getResourceName(); System.out.printf("Added budget with resource name '%s'.%n", budgetResourceName); return budgetResourceName; } }
C#
private static string CreateBudget(GoogleAdsClient client, long customerId) { // Get the BudgetService. CampaignBudgetServiceClient budgetService = client.GetService( Services.V10.CampaignBudgetService); // Create the campaign budget. CampaignBudget budget = new CampaignBudget() { Name = "Interplanetary Cruise Budget #" + ExampleUtilities.GetRandomString(), DeliveryMethod = BudgetDeliveryMethod.Standard, AmountMicros = 500000 }; // Create the operation. CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation() { Create = budget }; // Create the campaign budget. MutateCampaignBudgetsResponse response = budgetService.MutateCampaignBudgets( customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation }); string budgetResourceName = response.Results.First().ResourceName; // Print out some information about the added budget. Console.WriteLine($"Added campaign budget with resource name =" + $" '{budgetResourceName}'."); return budgetResourceName; }
PHP
private static function createCampaignBudget( GoogleAdsClient $googleAdsClient, int $customerId ) { // Creates a campaign budget. $campaignBudget = new CampaignBudget([ 'name' => 'Interplanetary Cruise Budget #' . Helper::getPrintableDatetime(), 'delivery_method' => BudgetDeliveryMethod::STANDARD, 'amount_micros' => 5000000 ]); // Creates a campaign budget operation. $campaignBudgetOperation = new CampaignBudgetOperation(); $campaignBudgetOperation->setCreate($campaignBudget); // Issues a mutate request to add campaign budgets. $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient(); /** @var MutateCampaignBudgetsResponse $campaignBudgetResponse */ $campaignBudgetResponse = $campaignBudgetServiceClient->mutateCampaignBudgets( $customerId, [$campaignBudgetOperation] ); // Prints out some information about the created campaign budget. $campaignBudgetResourceName = $campaignBudgetResponse->getResults()[0]->getResourceName(); printf("Added budget named '%s'.%s", $campaignBudgetResourceName, PHP_EOL); return $campaignBudgetResourceName; }
Python
def _create_budget(client, customer_id): campaign_budget_operation = client.get_type("CampaignBudgetOperation") campaign_budget = campaign_budget_operation.create campaign_budget.name = f"Interplanetary Cruise Budget #{uuid4()}" campaign_budget.delivery_method = ( client.enums.BudgetDeliveryMethodEnum.STANDARD ) campaign_budget.amount_micros = 500000 campaign_budget_service = client.get_service("CampaignBudgetService") try: campaign_budget_response = ( campaign_budget_service.mutate_campaign_budgets( customer_id=customer_id, operations=[campaign_budget_operation] ) ) except GoogleAdsException as ex: _handle_googleads_exception(ex) return campaign_budget_response.results[0].resource_name
Ruby
def create_campaign_budget(client, customer_id) # Creates a campaign budget operation. operation = client.operation.create_resource.campaign_budget do |b| b.name = "Interplanetary Cruise Budget #{(Time.new.to_f * 1000).to_i}" b.delivery_method = :STANDARD b.amount_micros = 5_000_000 end # Issues a mutate request to add campaign budgets. response = client.service.campaign_budget.mutate_campaign_budgets( customer_id: customer_id, operations: [operation], ) # Prints out some information about the created campaign budget. resource_name = response.results.first.resource_name puts "Created campaign budget #{resource_name}" resource_name end
Perl
sub create_campaign_budget { my ($api_client, $customer_id) = @_; # Create a campaign budget. my $campaign_budget = Google::Ads::GoogleAds::V10::Resources::CampaignBudget->new({ name => "Interplanetary Cruise Budget #" . uniqid(), deliveryMethod => STANDARD, amountMicros => 5000000 }); # Create a campaign budget operation. my $campaign_budget_operation = Google::Ads::GoogleAds::V10::Services::CampaignBudgetService::CampaignBudgetOperation ->new({ create => $campaign_budget }); # Issue a mutate request to add the campaign budget. my $campaign_budgets_response = $api_client->CampaignBudgetService()->mutate({ customerId => $customer_id, operations => [$campaign_budget_operation]}); # Print out some information about the created campaign budget. my $campaign_budget_resource_name = $campaign_budgets_response->{results}[0]{resourceName}; printf "Added budget named '%s'.\n", $campaign_budget_resource_name; return $campaign_budget_resource_name; }
Smart Display Campaign
To create a Smart Display campaign, follow the steps in our code example with these specifics:
Set the
advertising_channel_type
toDISPLAY
.Set the
advertising_channel_sub_type
toDISPLAY_SMART_CAMPAIGN
.Set the
target_cpa
field to a newly createdTargetCpa
object. Target CPA is the only allowed bidding strategy for this campaign type.
You can also set targeting options for smart display campaign, by following the Add Campaign Targeting Criteria code example. The only campaign-level targeting options allowed are:
Negative
ContentLabelInfo
criteriaPositive
LanguageInfo
criteriaPositive or negative
LocationInfo
criteriaPositive or negative
ProximityInfo
criteria
Java
private static String createSmartDisplayCampaign( GoogleAdsClient googleAdsClient, long customerId, String budgetResourceName) { // Creates the campaign. Campaign campaign = Campaign.newBuilder() .setName("Smart Display Campaign #" + getPrintableDateTime()) // Smart Display campaign requires the advertising channel type as 'DISPLAY'. .setAdvertisingChannelType(AdvertisingChannelType.DISPLAY) // Smart Display campaign requires the advertising channel sub type as // 'DISPLAY_SMART_CAMPAIGN'. .setAdvertisingChannelSubType(AdvertisingChannelSubType.DISPLAY_SMART_CAMPAIGN) // Smart Display campaign requires the TargetCpa bidding strategy. .setTargetCpa(TargetCpa.newBuilder().setTargetCpaMicros(5_000_000).build()) .setCampaignBudget(budgetResourceName) // Optional: Sets the start and end dates for the campaign, beginning one day from // now and ending a month from now. .setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd")) .setEndDate(new DateTime().plusMonths(1).toString("yyyyMMdd")) .build(); // Creates the operation. CampaignOperation operation = CampaignOperation.newBuilder().setCreate(campaign).build(); // Creates the campaign service client. try (CampaignServiceClient campaignServiceClient = googleAdsClient.getLatestVersion().createCampaignServiceClient()) { // Adds the campaign. MutateCampaignsResponse response = campaignServiceClient.mutateCampaigns( Long.toString(customerId), ImmutableList.of(operation)); String campaignResourceName = response.getResults(0).getResourceName(); // Displays the results. System.out.printf( "Added Smart Display campaign with resource name '%s'.%n", campaignResourceName); return campaignResourceName; } }
C#
private string CreateSmartDisplayCampaign(GoogleAdsClient client, long customerId, string budgetResourceName) { // Get the CampaignService. CampaignServiceClient campaignService = client.GetService(Services.V10.CampaignService); // Create the campaign. Campaign campaign = new Campaign() { Name = "Smart Display Campaign #" + ExampleUtilities.GetRandomString(), // Smart Display campaign requires the advertising_channel_type as 'DISPLAY'. AdvertisingChannelType = AdvertisingChannelType.Display, // Smart Display campaign requires the advertising_channel_sub_type as // 'DISPLAY_SMART_CAMPAIGN'. AdvertisingChannelSubType = AdvertisingChannelSubType.DisplaySmartCampaign, // 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 Status = CampaignStatus.Paused, // Smart Display campaign requires the TargetCpa bidding strategy. TargetCpa = new TargetCpa() { TargetCpaMicros = 5000000 }, CampaignBudget = budgetResourceName, // Optional: Set the start date. StartDate = DateTime.Now.AddDays(1).ToString("yyyyMMdd"), // Optional: Set the end date. EndDate = DateTime.Now.AddYears(1).ToString("yyyyMMdd"), }; // Create the operation. CampaignOperation operation = new CampaignOperation() { Create = campaign }; // Add the campaign. MutateCampaignsResponse response = campaignService.MutateCampaigns( customerId.ToString(), new[] { operation }); string campaignResourceName = response.Results.First().ResourceName; // Print out some information about the added campaign. Console.WriteLine($"Added campaign with resource name = '{campaignResourceName}'."); return campaignResourceName; }
PHP
private static function createSmartDisplayCampaign( GoogleAdsClient $googleAdsClient, int $customerId, string $campaignBudgetResourceName ) { $campaign = new Campaign([ 'name' => 'Smart Display Campaign #' . Helper::getPrintableDatetime(), // Smart Display campaign requires the advertising_channel_type as 'DISPLAY'. 'advertising_channel_type' => AdvertisingChannelType::DISPLAY, // Smart Display campaign requires the advertising_channel_sub_type as // 'DISPLAY_SMART_CAMPAIGN'. 'advertising_channel_sub_type' => AdvertisingChannelSubType::DISPLAY_SMART_CAMPAIGN, // Smart Display campaign requires the TargetCpa bidding strategy. 'target_cpa' => new TargetCpa(['target_cpa_micros' => 5000000]), 'campaign_budget' => $campaignBudgetResourceName, // Optional: Sets the start and end dates for the campaign, beginning one day from // now and ending a month from now. 'start_date' => date('Ymd', strtotime('+1 day')), 'end_date' => date('Ymd', strtotime('+1 month')) ]); // Creates a campaign operation. $campaignOperation = new CampaignOperation(); $campaignOperation->setCreate($campaign); // Issues a mutate request to add the campaign. $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); /** @var MutateCampaignsResponse $campaignResponse */ $campaignResponse = $campaignServiceClient->mutateCampaigns( $customerId, [$campaignOperation] ); $campaignResourceName = $campaignResponse->getResults()[0]->getResourceName(); printf("Added a Smart Display campaign named '%s'.%s", $campaignResourceName, PHP_EOL); return $campaignResourceName; }
Python
def _create_smart_display_campaign(client, customer_id, budget_resource_name): campaign_service = client.get_service("CampaignService") campaign_operation = client.get_type("CampaignOperation") campaign = campaign_operation.create campaign.name = f"Smart Display Campaign #{uuid4()}" advertising_channel_type_enum = client.enums.AdvertisingChannelTypeEnum campaign.advertising_channel_type = advertising_channel_type_enum.DISPLAY advertising_channel_sub_type_enum = ( client.enums.AdvertisingChannelSubTypeEnum ) # Smart Display campaign requires the advertising_channel_sub_type as # "DISPLAY_SMART_CAMPAIGN". campaign.advertising_channel_sub_type = ( advertising_channel_sub_type_enum.DISPLAY_SMART_CAMPAIGN ) campaign_status_enum = client.enums.CampaignStatusEnum campaign.status = campaign_status_enum.PAUSED # Smart Display campaign requires the TargetCpa bidding strategy. campaign.target_cpa.target_cpa_micros = 5000000 campaign.campaign_budget = budget_resource_name # Optional: Set the start and end date. start_date = datetime.date.today() + datetime.timedelta(days=1) campaign.start_date = start_date.strftime(_DATE_FORMAT) end_date = start_date + datetime.timedelta(days=365) campaign.end_date = end_date.strftime(_DATE_FORMAT) try: campaign_response = campaign_service.mutate_campaigns( customer_id=customer_id, operations=[campaign_operation] ) except GoogleAdsException as ex: _handle_googleads_exception(ex) return campaign_response.results[0].resource_name
Ruby
def create_smart_display_campaign(client, customer_id, budget) # Creates a campaign operation. operation = client.operation.create_resource.campaign do |c| c.name = "Smart Display Campaign #{(Time.new.to_f * 1000).to_i}" # Smart Display campaign requires the advertising_channel_type as 'DISPLAY'. c.advertising_channel_type = :DISPLAY # Smart Display campaign requires the advertising_channel_sub_type as # 'DISPLAY_SMART_CAMPAIGN'. c.advertising_channel_sub_type = :DISPLAY_SMART_CAMPAIGN # Smart Display campaign requires the TargetCpa bidding strategy. c.target_cpa = client.resource.target_cpa do |tcpa| tcpa.target_cpa_micros = 5_000_000 end c.campaign_budget = budget # Optional: Sets the start and end dates for the campaign, beginning one day # from now and ending a month from now. c.start_date = DateTime.parse((Date.today + 1).to_s).strftime('%Y%m%d') c.end_date = DateTime.parse(Date.today.next_month.to_s).strftime('%Y%m%d') end # Issues a mutate request to add the campaign. response = client.service.campaign.mutate_campaigns( customer_id: customer_id, operations: [operation], ) # Prints out some information about the created campaign. resource_name = response.results.first.resource_name puts "Added a smart display campaign named #{resource_name}" resource_name end
Perl
sub create_smart_display_campaign { my ($api_client, $customer_id, $campaign_budget_resource_name) = @_; my $campaign = Google::Ads::GoogleAds::V10::Resources::Campaign->new({ name => "Smart Display Campaign #" . uniqid(), # Smart Display campaign requires the advertising_channel_type as 'DISPLAY'. advertisingChannelType => DISPLAY, # Smart Display campaign requires the advertising_channel_sub_type as # 'DISPLAY_SMART_CAMPAIGN'. advertisingChannelSubType => DISPLAY_SMART_CAMPAIGN, # Smart Display campaign requires the TargetCpa bidding strategy. targetCpa => Google::Ads::GoogleAds::V10::Common::TargetCpa->new({ targetCpaMicros => 5000000 } ), campaignBudget => $campaign_budget_resource_name, # Optional: Set the start and end dates for the campaign, beginning one day # from now and ending a month from now. startDate => strftime("%Y%m%d", localtime(time + 60 * 60 * 24)), endDate => strftime("%Y%m%d", localtime(time + 60 * 60 * 24 * 30)), }); # Create a campaign operation. my $campaign_operation = Google::Ads::GoogleAds::V10::Services::CampaignService::CampaignOperation-> new({ create => $campaign }); # Issue a mutate request to add the campaign. my $campaigns_response = $api_client->CampaignService()->mutate({ customerId => $customer_id, operations => [$campaign_operation]}); # Print out some information about the added campaign. my $campaign_resource_name = $campaigns_response->{results}[0]{resourceName}; printf "Added a Smart Display campaign named '%s'.\n", $campaign_resource_name; return $campaign_resource_name; }