כדי לשנות כמה סוגי משאבים בבקשה אחת או לשלוח את כל השינויים לנקודת קצה מאוחדת, משתמשים ב-GoogleAdsService.Mutate. לרשימה המלאה של הפעולות הנתמכות, עיינו בחומר העזר בנושא MutateOperation.
פעולות שינוי (mutate)
כל MutateGoogleAdsRequest מקבל MutateOperation חוזר בשדה mutate_operations:
- פעולות שינוי של משאבים: בכל רשומה אפשר לציין פעולה אחת מסוג
create,updateאוremoveלסוג משאב אחד. - פעולות: בכל רשומה אפשר גם לציין פעולה, כמו
book_campaigns_operationאוquote_campaigns_operation.
כדי ליצור ישויות תלויות (כמו קמפיין וקבוצת מודעות) בקריאה אחת ל-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
(
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.