設定預算

以下範例說明如何建立 MutateOperation,可建立新的 CampaignBudget:智慧廣告活動。這個 作業會與其他實體一起用於單一 Change 請求 才能建立智慧廣告活動。

智慧廣告活動預算的重要規定:

Java

private MutateOperation createCampaignBudgetOperation(long customerId, long dailyBudgetMicros) {
  MutateOperation.Builder builder = MutateOperation.newBuilder();
  builder
      .getCampaignBudgetOperationBuilder()
      .getCreateBuilder()
      .setName("Smart campaign budget " + CodeSampleHelper.getShortPrintableDateTime())
      .setDeliveryMethod(BudgetDeliveryMethod.STANDARD)
      // A budget used for Smart campaigns must have the type SMART_CAMPAIGN.
      .setType(BudgetType.SMART_CAMPAIGN)
      // The suggested budget amount from the SmartCampaignSuggestService is for a _daily_ budget.
      // We don't need to specify that here, because the budget period already defaults to DAILY.
      .setAmountMicros(dailyBudgetMicros)
      // Sets a temporary ID in the budget's resource name so it can be referenced by the campaign
      // in later steps.
      .setResourceName(ResourceNames.campaignBudget(customerId, BUDGET_TEMPORARY_ID));
  return builder.build();
}
      

C#

/// <summary>
/// Creates a MutateOperation that creates a new CampaignBudget.
/// A temporary ID will be assigned to this campaign budget so that it can be referenced by
/// other objects being created in the same Mutate request.
/// </summary>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <param name="suggestedBudgetAmount">A daily amount budget in micros.</param>
/// <returns>A MutateOperation that creates a CampaignBudget</returns>
private MutateOperation CreateCampaignBudgetOperation(long customerId,
    long suggestedBudgetAmount)
{
    return new MutateOperation
    {
        CampaignBudgetOperation = new CampaignBudgetOperation
        {
            Create = new CampaignBudget
            {
                Name = $"Smart campaign budget #{ExampleUtilities.GetRandomString()}",
                // A budget used for Smart campaigns must have the type SMART_CAMPAIGN.
                Type = BudgetType.SmartCampaign,
                // The suggested budget amount from the SmartCampaignSuggestService is a
                // daily budget. We don't need to specify that here, because the budget
                // period already defaults to DAILY.
                AmountMicros = suggestedBudgetAmount,
                // Set a temporary ID in the budget's resource name so it can be referenced
                // by the campaign in later steps.
                ResourceName = ResourceNames.CampaignBudget(
                    customerId, BUDGET_TEMPORARY_ID)
            }
        }
    };
}
      

PHP

private static function createCampaignBudgetOperation(
    int $customerId,
    int $suggestedBudgetAmount
): MutateOperation {
    // Creates the campaign budget object.
    $campaignBudget = new CampaignBudget([
        'name' => "Smart campaign budget #" . Helper::getPrintableDatetime(),
        // A budget used for Smart campaigns must have the type SMART_CAMPAIGN.
        'type' => BudgetType::SMART_CAMPAIGN,
        // The suggested budget amount from the SmartCampaignSuggestService is a daily budget.
        // We don't need to specify that here, because the budget period already defaults to
        // DAILY.
        'amount_micros' => $suggestedBudgetAmount,
        // Sets a temporary ID in the budget's resource name so it can be referenced by the
        // campaign in later steps.
        'resource_name' =>
            ResourceNames::forCampaignBudget($customerId, self::BUDGET_TEMPORARY_ID)
    ]);

    // Creates the MutateOperation that creates the campaign budget.
    return new MutateOperation([
        'campaign_budget_operation' => new CampaignBudgetOperation([
            'create' => $campaignBudget
        ])
    ]);
}
      

Python

def create_campaign_budget_operation(
    client, customer_id, suggested_budget_amount
):
    """Creates a MutateOperation that creates a new CampaignBudget.

    A temporary ID will be assigned to this campaign budget so that it can be
    referenced by other objects being created in the same Mutate request.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        suggested_budget_amount: a numeric daily budget amount in micros.

    Returns:
        a MutateOperation that creates a CampaignBudget.
    """
    mutate_operation = client.get_type("MutateOperation")
    campaign_budget_operation = mutate_operation.campaign_budget_operation
    campaign_budget = campaign_budget_operation.create
    campaign_budget.name = f"Smart campaign budget #{uuid4()}"
    # A budget used for Smart campaigns must have the type SMART_CAMPAIGN.
    # Note that the field name "type_" is an implementation detail in Python,
    # the field's actual name is "type".
    campaign_budget.type_ = client.enums.BudgetTypeEnum.SMART_CAMPAIGN
    # The suggested budget amount from the SmartCampaignSuggestService is
    # a daily budget. We don't need to specify that here, because the budget
    # period already defaults to DAILY.
    campaign_budget.amount_micros = suggested_budget_amount
    # Set a temporary ID in the budget's resource name so it can be referenced
    # by the campaign in later steps.
    campaign_budget.resource_name = client.get_service(
        "CampaignBudgetService"
    ).campaign_budget_path(customer_id, _BUDGET_TEMPORARY_ID)

    return mutate_operation
      

小茹

# Creates a mutate_operation that creates a new campaign_budget.
# A temporary ID will be assigned to this campaign budget so that it can be
# referenced by other objects being created in the same mutate request.
def create_campaign_budget_operation(
  client,
  customer_id,
  suggested_budget_amount)
  mutate_operation = client.operation.mutate do |m|
    m.campaign_budget_operation = client.operation.create_resource.campaign_budget do |cb|
      cb.name = "Smart campaign budget ##{(Time.new.to_f * 1000).to_i}"
      # A budget used for Smart campaigns must have the type SMART_CAMPAIGN.
      cb.type = :SMART_CAMPAIGN
      # The suggested budget amount from the smart_campaign_suggest_service is
      # a daily budget. We don't need to specify that here, because the budget
      # period already defaults to DAILY.
      cb.amount_micros = suggested_budget_amount
      # Sets a temporary ID in the budget's resource name so it can be referenced
      # by the campaign in later steps.
      cb.resource_name = client.path.campaign_budget(customer_id, BUDGET_TEMPORARY_ID)
    end
  end

  mutate_operation
end
      

Perl

# Creates a MutateOperation that creates a new CampaignBudget.
# A temporary ID will be assigned to this campaign budget so that it can be
# referenced by other objects being created in the same Mutate request.
sub _create_campaign_budget_operation {
  my ($customer_id, $suggested_budget_amount) = @_;

  return
    Google::Ads::GoogleAds::V17::Services::GoogleAdsService::MutateOperation->
    new({
      campaignBudgetOperation =>
        Google::Ads::GoogleAds::V17::Services::CampaignBudgetService::CampaignBudgetOperation
        ->new({
          create =>
            Google::Ads::GoogleAds::V17::Resources::CampaignBudget->new({
              name => "Smart campaign budget #" . uniqid(),
              # A budget used for Smart campaigns must have the type SMART_CAMPAIGN.
              type =>
                Google::Ads::GoogleAds::V17::Enums::BudgetTypeEnum::SMART_CAMPAIGN,
              # The suggested budget amount from the SmartCampaignSuggestService is
              # a daily budget. We don't need to specify that here, because the
              # budget period already defaults to DAILY.
              amountMicros => $suggested_budget_amount,
              # Set a temporary ID in the budget's resource name so it can be
              # referenced by the campaign in later steps.
              resourceName =>
                Google::Ads::GoogleAds::V17::Utils::ResourceNames::campaign_budget(
                $customer_id, BUDGET_TEMPORARY_ID
                )})})});
}