ב-Google Ads API, העדכונים מתבצעים באמצעות מסכת שדה. במסכת השדות מפורטים כל השדות שאתם מתכוונים לשנות באמצעות העדכון, וכל שדה שצוין ולא נכלל במסכת השדות יידחה, גם אם הוא נשלח לשרת.
FieldMaskUtil
הדרך המומלצת ליצור מסכות שדות היא באמצעות הכלי המובנה שלנו למסכות שדות, שמאפשר ליצור מסכות שדות מאובייקט שעבר שינוי במקום ליצור אותן מאפס.
דוגמה לעדכון קמפיין:
// Creates a Campaign object with the proper resource name and any other changes.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation that will update the campaign, using the FieldMasks'
// allSetFieldsOf utility to derive the update mask. This mask tells the Google
// Ads API which attributes of the campaign you want to change.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
בדוגמה הזו, קודם יוצרים אובייקט Campaign
ריק ואז מגדירים את שם המשאב שלו כדי ש-API ידע איזה קמפיין מתעדכן.
בדוגמה הזו נעשה שימוש בשיטה FieldMasks.allSetFieldsOf()
בקמפיין כדי ליצור באופן אוטומטי מסכת שדות שמפרטת את כל השדות שהוגדרו. לאחר מכן תוכלו להעביר את המסכה שהוחזרה ישירות לקריאה לעדכון.
אם אתם צריכים לעבוד עם אובייקט קיים כדי לעדכן כמה שדות, תוכלו לשנות את הקוד באופן הבא:
Campaign existingCampaign;
// Obtains existingCampaign from elsewhere.
...
// Creates a new campaign based off the existing campaign and updates the
// campaign by setting its status to paused.
Campaign campaignToUpdate =
existingCampaign.toBuilder()
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation that will update the campaign, using the FieldMasks'
// compare utility to derive the update mask. This mask tells the Google Ads API
// which attributes of the campaign you want to change.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaignToUpdate)
.setUpdateMask(FieldMasks.compare(existingCampaign, campaignToUpdate))
.build();
// Sends the operation in a mutate request.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
כדי ליצור מסכת שדה מאפס, קודם יוצרים אובייקט FieldMask
ואז מוסיפים לאובייקט את השם של כל אחד מהשדות שרוצים לשנות.
FieldMask fieldMask =
FieldMask.newBuilder()
.addPaths("status")
.addPaths("name")
.build();
עדכון שדות ההודעה ושדות המשנה שלהם
לשדות MESSAGE
יכולים להיות שדות משנה (למשל, MaximizeConversions
שיש לו שלושה: target_cpa_micros
, cpc_bid_ceiling_micros
ו-cpc_bid_floor_micros
), או שאין להם שדות משנה בכלל (למשל, ManualCpm
).
שדות של הודעות ללא שדות משנה מוגדרים
כשמעדכנים שדה MESSAGE
שלא מוגדר עם שדות משנה, משתמשים ב-FieldMaskUtil כדי ליצור אנונימיזציה של שדה, כפי שמתואר למעלה.
שדות של הודעות עם שדות משנה מוגדרים
כשמעדכנים שדה MESSAGE
שמוגדר עם שדות משנה בלי להגדיר באופן מפורש אף אחד משדות המשנה בהודעה, צריך להוסיף ידנית את כל שדות המשנה הניתנים לשינוי של MESSAGE
אל FieldMask
, בדומה לדוגמה שלמעלה שבה נוצר מסכת שדה מאפס.
דוגמה נפוצה לכך היא עדכון שיטת הבידינג של קמפיין בלי להגדיר אף אחד מהשדות בשיטת הבידינג החדשה. בדוגמה הבאה מוסבר איך לעדכן קמפיין כך שישתמש בשיטת הבידינג MaximizeConversions
בלי להגדיר אף אחד משדות המשנה של שיטת הבידינג.
במקרה כזה, השימוש בשיטות allSetFieldsOf()
ו-compare()
של FieldMaskUtil לא מאפשר להשיג את היעד הרצוי.
בדוגמה הבאה נוצרת מסכת שדה שכוללת את maximize_conversions
. עם זאת, Google Ads API לא מאפשר את ההתנהגות הזו כדי למנוע ניקוי שדות בטעות, ומציג הודעת שגיאה מסוג FieldMaskError.FIELD_HAS_SUBFIELDS
.
// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(MaximizeConversions.newBuilder().build())
.build();
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. The field mask will include 'maximize_conversions`,
// which will produce a FieldMaskError.FIELD_HAS_SUBFIELDS error.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that will result in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
בדוגמה הבאה מוסבר איך לעדכן כראוי קמפיין כך שישתמש בשיטת הבידינג MaximizeConversions
בלי להגדיר אף אחד משדות המשנה שלה.
// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.build();
// Creates a field mask from the existing campaign and adds all of the mutable
// fields (only one in this case) on the MaximizeConversions bidding strategy to
// the field mask. Because this field is included in the field mask but
// excluded from the campaign object, the Google Ads API will set the campaign's
// bidding strategy to a MaximizeConversions object without any of its subfields
// set.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
.toBuilder()
// Only include 'maximize_conversions.target_cpa_micros' in the field mask
// as it is the only mutable subfield on MaximizeConversions when used as a
// standard bidding strategy.
//
// Learn more about standard and portfolio bidding strategies here:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
.addPaths("maximize_conversions.target_cpa_micros")
.build();
// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(fieldMask)
.build();
ניקוי שדות
אפשר לנקות שדות מסוימים באופן מפורש. בדומה לדוגמה שלמעלה, צריך להוסיף את השדות האלה באופן מפורש למסכת השדות. לדוגמה, נניח שיש לכם קמפיין שבו מוגדרת שיטת בידינג מסוג MaximizeConversions
וששדה target_cpa_micros
מוגדר לערך גבוה מ-0.
הקוד הבא פועל, אבל השדה maximize_conversions.target_cpa_micros
לא יתווסף למסיכת השדה, ולכן לא יבוצעו שינויים בשדה target_cpa_micros
:
// Creates a campaign with the proper resource name and a MaximizeConversions
// object with target_cpa_micros set to 0.
Campaign campaign =
Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.setMaximizeConversions(
MaximizeConversions.newBuilder().setTargetCpa(0).build())
.setStatus(CampaignStatus.PAUSED)
.build();
// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. However, the field mask will NOT include
// 'maximize_conversions.target_cpa_micros'.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
.build();
// Sends the operation in a mutate request that will succeed but will NOT update
// the 'target_cpa_micros' field because
// 'maximize_conversions.target_cpa_micros' was not included in the field mask.
MutateCampaignsResponse response =
campaignServiceClient.mutateCampaigns(
customerId.toString(), Collections.singletonList(operation));
בדוגמה הבאה מוסבר איך למחוק בצורה נכונה את השדה target_cpa_micros
בשיטת הבידינג MaximizeConversions
.
// Creates a Campaign object with the proper resource name.
Campaign campaign = Campaign.newBuilder()
.setResourceName(ResourceNames.campaign(customerId, campaignId))
.build();
// Constructs a field mask from the existing campaign and adds the
// 'maximize_conversions.target_cpa_micros' field to the field mask, which will
// clear this field from the bidding strategy without impacting any other fields
// on the bidding strategy.
FieldMask fieldMask = FieldMasks.allSetFieldsOf(campaign)
.toBuilder()
.addPaths("maximize_conversions.target_cpa_micros")
.build();
// Creates an operation to update the campaign with the specified field.
CampaignOperation operation =
CampaignOperation.newBuilder()
.setUpdate(campaign)
.setUpdateMask(fieldMask))
.build();
שימו לב שהדוגמה 'השגויה' שלמעלה פועלת כמצופה בשדות שמוגדרים בתור optional
ב-Google Ads API protocol buffers
.
עם זאת, מכיוון ש-target_cpa_micros
הוא לא שדה optional
, הדוגמה 'שגויה' לא מעדכנת את שיטת הבידינג כדי לנקות את השדה target_cpa
.