フィールド マスク

Google Ads API では、更新はフィールド マスクを使用して行われます。フィールド マスク(google.protobuf.FieldMask)には、更新で変更する snake_case のフィールド パスのリストが含まれています。フィールド マスクに含まれていない指定されたフィールドは、サーバーに送信されても無視されます。

FieldMasks ユーティリティ

Java クライアント ライブラリでフィールド マスクを生成する推奨の方法は、組み込みの FieldMasks ユーティリティ クラスを使用することです。これにより、フィールド マスクをゼロから構築するのではなく、変更されたオブジェクトから生成できます。

キャンペーンを更新する例を次に示します。

// 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 updates 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() を呼び出し、設定されたすべてのフィールドを列挙するフィールド マスクを自動的に生成します。返されたマスクは、更新呼び出しに直接渡すことができます。

既存のオブジェクトを操作していくつかのフィールドを更新する必要がある場合は、次のように FieldMasks.compare() を使用します。

// Assumes existingCampaign was retrieved from a previous API call.

// Creates a new campaign based on 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 updates 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 ビルダーを作成し、変更する各フィールドの snake_case 名を追加します。

FieldMask fieldMask =
    FieldMask.newBuilder()
        .addPaths("status")
        .addPaths("name")
        .build();

メッセージ フィールドとそのサブフィールドを更新する

MESSAGE フィールドにはサブフィールド(target_cpa_micros、cpc_bid_ceiling_micros、cpc_bid_floor_micros を含む MaximizeConversions など)を含めることも、サブフィールドを含めない(ManualCpm など)こともできます。

サブフィールドが定義されていないメッセージ フィールド

サブフィールドで定義されていない MESSAGE フィールドを更新する場合は、前のセクションで説明したように、FieldMasks ユーティリティを使用してフィールド マスクを生成します。

サブフィールドが定義されたメッセージ フィールド

サブフィールドが定義されている MESSAGE フィールドを更新するときに、そのメッセージのサブフィールドを明示的に設定しない場合は、変更可能な MESSAGE サブフィールドをそれぞれ FieldMask に手動で追加する必要があります。これは、フィールド マスクをゼロから作成する場合と同様です。

一般的な例としては、新しい入札戦略のフィールドを設定せずに、キャンペーンの入札戦略(oneof フィールド campaign_bidding_strategy)を更新する場合などがあります。次の例は、入札戦略のサブフィールドを設定せずに、MaximizeConversions 入札戦略を使用するようにキャンペーンを更新する方法を示しています。

この場合、FieldMasks の allSetFieldsOf() メソッドと compare() メソッドを単独で使用しても、目的の目標を達成できません。

次の例では、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 FieldMasks.allSetFieldsOf to derive the update
// mask. The field mask includes 'maximize_conversions', which produces 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 results in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields with
// subfields cannot be included directly 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 campaign and adds the mutable subfield
// ('maximize_conversions.target_cpa_micros') on the MaximizeConversions
// bidding strategy to the field mask. Because this subfield is included in the
// field mask while excluded from the campaign object, the Google Ads API
// switches the campaign's bidding strategy oneof to MaximizeConversions with
// target_cpa_micros unset.
FieldMask fieldMask =
    FieldMasks.allSetFieldsOf(campaign).toBuilder()
        .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 は意図したとおりにクリアされません。

// Creates a campaign with the proper resource name and a MaximizeConversions
// object with target_cpa_micros set to 0L.
Campaign campaign =
    Campaign.newBuilder()
        .setResourceName(ResourceNames.campaign(customerId, campaignId))
        .setMaximizeConversions(
            MaximizeConversions.newBuilder().setTargetCpaMicros(0L).build())
        .setStatus(CampaignStatus.PAUSED)
        .build();

// Constructs an operation using FieldMasks.allSetFieldsOf to derive the
// update mask.
CampaignOperation operation =
    CampaignOperation.newBuilder()
        .setUpdate(campaign)
        .setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
        .build();

// Sends the operation in a mutate request that does not clear the field
// cleanly.
MutateCampaignsResponse response =
    campaignServiceClient.mutateCampaigns(
        customerId.toString(), Collections.singletonList(operation));

次の例は、MaximizeConversions 入札戦略の target_cpa_micros フィールドを適切にクリアする方法を示しています。

// 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 campaign and adds the
// 'maximize_conversions.target_cpa_micros' field to the field mask, which
// clears 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();