フィールド マスク

Google Ads API では、フィールド マスクを使用して更新が行われます。フィールド マスクには、更新で変更するすべてのフィールドがリストされます。フィールド マスクに含まれていない指定されたフィールドは、サーバーに送信されても無視されます。FieldMask(Google\Protobuf\FieldMask)を作成し、変更するすべてのフィールドの名前を入力した配列を作成して、その配列をフィールド マスクの paths フィールドに割り当てることで、フィールド マスクを手動で作成できます。

組み込みのフィールド マスク ユーティリティ(FieldMasks)を使用することもできます。このユーティリティは、多くの詳細を非表示にし、エンティティのフィールドに対する変更を検査してフィールド マスクを自動的に生成します。

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

$campaign = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
    'status' => CampaignStatusEnum\CampaignStatus::PAUSED
]);

$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));

このコードは、まず Campaign オブジェクトを作成し、ResourceNames を使用してリソース名を設定します。これにより、API はどのキャンペーンが更新されているかを認識します。status も CampaignStatus::PAUSED に設定されます。

次に、コードは CampaignOperation オブジェクトを作成し、そのオブジェクトに前に作成したキャンペーンを設定します。その後、FieldMasks::allSetFieldsOf() を使用して、変更されたすべてのフィールドを列挙し、キャンペーンのフィールド マスクを作成します。最後に、返されたマスクをキャンペーン オペレーション オブジェクトに渡します。

FieldMasks::allSetFieldsOf() は FieldMasks::compare() のコンビニエンス メソッドです。渡されたオブジェクトを同じクラスの空のオブジェクトと比較します。たとえば、前のコードでは、FieldMasks::allSetFieldsOf($campaign) の代わりに FieldMasks::compare(new Campaign(), $campaign) を使用できます。

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

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

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

サブフィールドが定義されていない MESSAGE フィールドを更新する場合は、前述のように FieldMasks を使用してフィールド マスクを生成します。

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

サブフィールドで定義された MESSAGE フィールドを更新するときに、そのメッセージのサブフィールドを明示的に設定しない場合は、前の例のように、変更可能な MESSAGE サブフィールドをそれぞれ FieldMask に手動で追加する必要があります。

一般的な例としては、新しい入札戦略のフィールドを何も設定せずに、キャンペーンの入札戦略を更新することがあります。次のコードは、入札戦略のサブフィールドを設定せずに 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 = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
    'maximize_conversions' => new MaximizeConversions()
]);

// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. The field mask includes 'maximize_conversions',
// which produces a FieldMaskError.FIELD_HAS_SUBFIELDS error.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));

// Sends the operation in a mutate request that results in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build($customerId, [$campaignOperation])
);

次のコードは、サブフィールドを設定せずに MaximizeConversions 入札戦略を使用するようにキャンペーンを適切に更新する方法を示しています。

// Creates a Campaign object with the proper resource name.
$campaign = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId)
]);

// Creates a field mask from the existing campaign and adds the mutable
// subfield 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 sets the campaign's bidding strategy
// to a MaximizeConversions object without any of its subfields set.
$fieldMask = FieldMasks::allSetFieldsOf($campaign);
// 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:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
$fieldMask->getPaths()[] = 'maximize_conversions.target_cpa_micros';

// Creates an operation to update the campaign with the specified fields.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask($fieldMask);

フィールドをクリア

一部のフィールドは明示的にクリアできます。前の例と同様に、これらのフィールドをフィールド マスクに明示的に追加する必要があります。これは、FieldMasks がデフォルト値(0、false、"" など)に設定された Proto3 スカラー フィールドを無視するためです。たとえば、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 = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
    'maximize_conversions' => new MaximizeConversions([
        'target_cpa_micros' => 0
    ]),
    'status' => CampaignStatusEnum\CampaignStatus::PAUSED
]);

// Constructs an operation, using the FieldMasks' allSetFieldsOf utility to
// derive the update mask. However, the field mask does NOT include
// 'maximize_conversions.target_cpa_micros'.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));

// Sends the operation in a mutate request that succeeds, but does NOT update
// the 'target_cpa_micros' field because
// 'maximize_conversions.target_cpa_micros' was not included in the field mask.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
    MutateCampaignsRequest::build($customerId, [$campaignOperation])
);

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

// Creates a Campaign object with the proper resource name.
$campaign = new Campaign([
    'resource_name' => ResourceNames::forCampaign($customerId, $campaignId)
]);

// Constructs a field mask from the existing 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 = FieldMasks::allSetFieldsOf($campaign);
$fieldMask->getPaths()[] = 'maximize_conversions.target_cpa_micros';

// Creates an operation to update the campaign with the specified field.
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask($fieldMask);

デフォルト値を設定すると、Google Ads API のプロトコル バッファで optional として定義されているフィールドで意図したとおりに動作します。ただし、target_cpa_micros は optional フィールドではないため、フィールド マスクにパスを明示的に追加せずに 0 に設定しても、入札戦略は更新されず、target_cpa_micros はクリアされません。