שינויים רבים

כדי לשנות כמה סוגים של משאבים בבקשה אחת או לשלוח את כל השינויים לנקודת קצה מאוחדת, משתמשים ב-GoogleAdsService.Mutate. רשימה מלאה של הפעולות הנתמכות זמינה במאמר MutateOperation.

פעולות שינוי

כל MutateGoogleAdsRequest מקבל MutateOperation חוזר בשדה mutate_operations שלו:

  • פעולות לשינוי משאבים (כל הגרסאות הנתמכות): כל רשומה יכולה לציין פעולה אחת מסוג create, update או remove עבור סוג משאב אחד.
  • פעולות על משאבים (גרסה 23 ואילך): כל רשומה יכולה לציין גם פעולה על משאב, כמו book_campaigns_operation או quote_campaigns_operation (בגרסה 22, MutateOperation תומך רק בפעולות שינוי של משאבים).

כדי ליצור ישויות תלויות (כמו קמפיין וקבוצת מודעות) בקריאה אחת של GoogleAdsService.Mutate, צריך ליצור שתי רשומות של 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): בדיקת פעולות לאיתור שגיאות בלי לבצע שינויים בחשבון:
    • פעולות שינוי של משאבים: מחזירה רק שגיאות אימות ומשמיטה תוצאות.
    • פעולות של פעולות (גרסה 23 ואילך): הפונקציה מחזירה את תוצאת הפעולה (למשל quote_campaigns_result או book_campaigns_result) אם האימות מצליח. אם מתרחשות שגיאות באימות, תוצאת הפעולה מושמטת מ-MutateOperationResponse (אבל יכול להיות שעדיין יוחזר ציטוט ב-ErrorDetails.reservation_error_details). שימו לב: הפונקציה quote_campaigns_operation דורשת את הפונקציות validate_only = true ו-partial_failure = false.