← 返回目录
来源:Google Ads API 官方文档 · 中文版 · 本地归档

创建预算

以下示例展示了如何构建 MutateOperation,以针对智能广告系列创建新的 CampaignBudget。此操作将与创建智能广告系列所需的其他实体一起用于单个 mutate 请求中。

智能广告系列预算的关键要求:

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
        ])
    ]);
}
在接这个接口时卡住了?
Stuck wiring this API into production? We build ad-delivery tooling on these platforms — campaign automation, reporting pipelines, error-handling and migration between API versions.
联系我们 · Talk to us 每日平台更新简报 →