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();