Create a budget

Below is an example showing how to build a MutateOperation that creates a new CampaignBudget for a Smart campaign. This operation will be used in a single mutate request along with the other entities required to create a Smart campaign.

Key requirements for Smart campaign budgets:

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.get_type(
        "BudgetTypeEnum"
    ).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.
    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

Ruby

# 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