大量變動

如要在單一要求中變動多個資源類型,或將所有變動傳送至統一端點,請使用 GoogleAdsService.Mutate。如需支援作業的完整清單,請參閱 MutateOperation 參考資料。

變動作業

每個 MutateGoogleAdsRequest 都會在 mutate_operations 欄位中接受重複的 MutateOperation:

  • 資源變動作業 (所有支援版本):每個項目可為一個資源類型指定單一 create、update 或 remove 作業。
  • 動作作業 (第 23 版以上):每個項目也可以指定動作作業,例如 book_campaigns_operation 或 quote_campaigns_operation (在第 22 版中,MutateOperation 僅支援資源變動作業)。

如要在單一 GoogleAdsService.Mutate呼叫中建立相依實體 (例如廣告活動和廣告群組),請建立兩個 MutateOperation項目 (一個包含 CampaignOperation,另一個包含 AdGroupOperation)。將含有負 ID (例如 customers/CUSTOMER_ID/campaigns/-1) 的暫時資源名稱指派給廣告活動,這樣廣告群組作業就能在同一個要求中參照該名稱:

Python

import uuid

campaign_budget_service = client.get_service("CampaignBudgetService")
campaign_service = client.get_service("CampaignService")
googleads_service = client.get_service("GoogleAdsService")

campaign_budget_resource_name = campaign_budget_service.campaign_budget_path(
    customer_id, budget_id
)
temp_campaign_resource_name = campaign_service.campaign_path(customer_id, "-1")

mutate_operation1 = client.get_type("MutateOperation")
campaign = mutate_operation1.campaign_operation.create
campaign.resource_name = temp_campaign_resource_name
campaign.name = f"Bulk Mutate Campaign #{uuid.uuid4()}"
campaign.advertising_channel_type = (
    client.enums.AdvertisingChannelTypeEnum.SEARCH
)
campaign.status = client.enums.CampaignStatusEnum.PAUSED
campaign.manual_cpc = client.get_type("ManualCpc")
campaign.campaign_budget = campaign_budget_resource_name
campaign.contains_eu_political_advertising = (
    client.enums.EuPoliticalAdvertisingStatusEnum.DOES_NOT_CONTAIN_EU_POLITICAL_ADVERTISING
)

mutate_operation2 = client.get_type("MutateOperation")
ad_group = mutate_operation2.ad_group_operation.create
ad_group.name = "Bulk Mutate Ad Group"
ad_group.campaign = temp_campaign_resource_name
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED

response = googleads_service.mutate(
    customer_id=customer_id,
    mutate_operations=[mutate_operation1, mutate_operation2],
)

for op_response in response.mutate_operation_responses:
    result_type = op_response._pb.WhichOneof("response")
    result = getattr(op_response, result_type)
    print(f"Created {result_type}: {result.resource_name}")

小茹

campaign_budget_resource_name =
  client.path.campaign_budget(customer_id, budget_id)
temp_campaign_resource_name =
  client.path.campaign(customer_id, -1)

mutate_operation1 = client.operation.mutate do |m|
  m.campaign_operation = client.operation.create_resource.campaign do |c|
    c.resource_name = temp_campaign_resource_name
    c.name = "Bulk Mutate Campaign #{(Time.new.to_f * 1000).to_i}"
    c.advertising_channel_type = :SEARCH
    c.status = :PAUSED
    c.manual_cpc = client.resource.manual_cpc
    c.campaign_budget = campaign_budget_resource_name
    c.contains_eu_political_advertising = :DOES_NOT_CONTAIN_EU_POLITICAL_ADVERTISING
  end
end

mutate_operation2 = client.operation.mutate do |m|
  m.ad_group_operation = client.operation.create_resource.ad_group do |ag|
    ag.name = "Bulk Mutate Ad Group"
    ag.campaign = temp_campaign_resource_name
    ag.status = :ENABLED
  end
end

response = client.service.google_ads.mutate(
  customer_id: customer_id,
  mutate_operations: [mutate_operation1, mutate_operation2]
)

response.mutate_operation_responses.each do |op_response|
  result = op_response.send(op_response.response)
  puts "Created #{op_response.response}: #{result.resource_name}"
end

完整性和部分失敗

根據預設,GoogleAdsService.Mutate 要求中的作業會以原子方式執行:所有作業會一併成功,或如果任何作業包含錯誤,整個要求就會失敗。

與個別資源服務相同,這個端點支援下列選項:

  • 部分失敗 (partial_failure = true):提交有效作業,並針對 partial_failure_error 中的失敗作業傳回錯誤。
  • 驗證模式 (validate_only = true):檢查作業是否有錯誤,但不會將變更提交至帳戶:
    • 資源變動作業:只會傳回驗證錯誤,並省略結果。
    • 動作作業 (第 23 版以上):驗證成功時,會傳回動作結果 (例如 quote_campaigns_result 或 book_campaigns_result);如果發生驗證錯誤,MutateOperationResponse 中會省略動作結果 (但 ErrorDetails.reservation_error_details 中可能仍會傳回報價)。請注意,quote_campaigns_operation 需要 validate_only = true 和 partial_failure = false。