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
の 3 つがあります)がある場合と、まったくない場合(ManualCpm
など)があります。
サブフィールドが定義されていないメッセージ フィールド
サブフィールドが定義されていない MESSAGE
フィールドを更新する場合は、上記のように FieldMaskUtil を使用してフィールド マスクを生成します。
サブフィールドが定義されたメッセージ フィールド
サブフィールドで定義された MESSAGE
フィールドを更新するときに、そのメッセージのサブフィールドを明示的に設定しない場合は、上記の例のように、変更可能な MESSAGE
サブフィールドをそれぞれ FieldMask
に手動で追加する必要があります。
一般的な例としては、新しい入札戦略のフィールドを何も設定せずに、キャンペーンの入札戦略を更新する場合などがあります。次の例は、入札戦略のサブフィールドを設定せずに、MaximizeConversions
入札戦略を使用するようにキャンペーンを更新する方法を示しています。
この場合、FieldMaskUtil の 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 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));
次の例は、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 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();
上記の「誤った」例は、Google Ads API の protocol buffers
で optional
として定義されているフィールドでは意図したとおりに動作します。ただし、target_cpa_micros
は optional
フィールドではないため、「不正解」の例では、入札戦略を更新して target_cpa
フィールドをクリアすることはできません。