מסכות שדה

ב-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 שכולל 3: 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.