หากต้องการเปลี่ยนแปลงทรัพยากรหลายประเภทในคำขอเดียวหรือส่งการเปลี่ยนแปลงทั้งหมดไปยังปลายทางแบบรวม ให้ใช้ GoogleAdsService.Mutate ดูรายการการดำเนินการที่รองรับทั้งหมดได้ในข้อมูลอ้างอิง MutateOperation
การดำเนินการเปลี่ยนแปลง
MutateGoogleAdsRequest แต่ละรายการยอมรับ MutateOperation ที่ซ้ำกันในช่อง mutate_operations ดังนี้
- การดำเนินการเปลี่ยนแปลงทรัพยากร: แต่ละรายการสามารถระบุการดำเนินการ
create,updateหรือremoveรายการเดียวสำหรับทรัพยากรประเภทเดียว - การดำเนินการ: แต่ละรายการยังระบุการดำเนินการได้ด้วย เช่น
book_campaigns_operationหรือquote_campaigns_operation
หากต้องการสร้างเอนทิตีที่ขึ้นต่อกัน (เช่น แคมเปญและกลุ่มโฆษณา) ในการเรียก GoogleAdsService.Mutate ครั้งเดียว ให้สร้างรายการ MutateOperation 2 รายการ (รายการหนึ่งมี CampaignOperation และอีกรายการมี AdGroupOperation) กำหนดชื่อทรัพยากรชั่วคราวที่มีรหัสเป็นค่าลบ (เช่น 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}")
Ruby
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): ตรวจสอบการดำเนินการเพื่อหาข้อผิดพลาดโดยไม่ยืนยันการเปลี่ยนแปลงในบัญชี:- การดำเนินการเปลี่ยนแปลงทรัพยากร: แสดงเฉพาะข้อผิดพลาดในการตรวจสอบและละเว้นผลลัพธ์
- การดำเนินการ: แสดงผลลัพธ์การดำเนินการ (เช่น
quote_campaigns_resultหรือbook_campaigns_result) เมื่อการตรวจสอบความถูกต้องสำเร็จ หากเกิดข้อผิดพลาดในการตรวจสอบความถูกต้อง ระบบจะไม่แสดงผลลัพธ์การดำเนินการจากMutateOperationResponse(แม้ว่าอาจยังคงแสดงใบเสนอราคาในErrorDetails.reservation_error_details) โปรดทราบว่าquote_campaigns_operationต้องใช้validate_only = trueและpartial_failure = false