การกลายพันธุ์เป็นกลุ่ม

หากต้องการเปลี่ยนแปลงประเภททรัพยากรหลายรายการในคำขอเดียวหรือส่งการเปลี่ยนแปลงทั้งหมดไปยัง ปลายทางแบบรวม ให้ใช้ GoogleAdsService.Mutate ดูรายการการดำเนินการที่รองรับทั้งหมดได้ในข้อมูลอ้างอิงของ MutateOperation

การดำเนินการเปลี่ยนแปลง

MutateGoogleAdsRequest แต่ละรายการยอมรับ MutateOperation ที่ซ้ำกันในช่อง mutate_operations

  • การดำเนินการเปลี่ยนแปลงทรัพยากร (ทุกเวอร์ชันที่รองรับ): แต่ละรายการสามารถ ระบุการดำเนินการ create, update หรือ remove รายการเดียวสำหรับทรัพยากรประเภทหนึ่ง ได้
  • การดำเนินการของ Action (v23 ขึ้นไป): แต่ละรายการยังระบุการดำเนินการของ Action ได้ด้วย เช่น book_campaigns_operation หรือ quote_campaigns_operation (ใน v22 MutateOperation รองรับเฉพาะการดำเนินการเปลี่ยนแปลงทรัพยากร)

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