Google Ads API では、フィールド マスクを使って更新を行います。フィールド マスクには、更新で変更するすべてのフィールドがリストされます。フィールド マスクに含まれていない指定のフィールドは、サーバーに送信された場合でも無視されます。フィールド マスクを手動で作成するには、Google\Protobuf\FieldMask
を作成し、変更するすべてのフィールドの名前が入力された配列を作成して、それをフィールド マスクのパス フィールドに割り当てます。
また、組み込みのフィールド マスク ユーティリティ(FieldMasks)を使用して、特定の詳細情報を隠し、エンティティのフィールドに対する変更をモニタリングすることで、フィールド マスクを自動的に生成できます。
以下は、キャンペーンを更新する場合の例です。
$campaign = new Campaign([
'resource_name' => ResourceNames::forCampaign($customerId, $campaignId),
'status' => CampaignStatus::PAUSED
]);
$campaignOperation = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
このコードは、まず Campaign オブジェクトを作成し、次に ResourceNames
を使用してリソース名を設定することで、更新対象のキャンペーンを API に認識できるようにします。status
も PAUSED
に設定されます。
次に、CampaignOperation
オブジェクトを作成し、前に作成したキャンペーンをそのオブジェクトに設定します。その後、FieldMasks::allSetFieldsOf()
を使って、変更されたすべてのフィールドからキャンペーンのフィールド マスクを作成します。最後に、返されたマスクをキャンペーンのオペレーション オブジェクトに渡します。
なお、FieldMasks::allSetFieldsOf
は FieldMasks::compare()
にとって便利なメソッドです。渡されたオブジェクトと、同じクラスの空のオブジェクトを比較します。たとえば、前のコードでは、allSetFieldsOf()
ではなく FieldMasks::compare(new
Campaign(), $campaign)
を使用していました。
メッセージ フィールドとそのサブフィールドの更新
MESSAGE
フィールドには、サブフィールドを含めることも(target_cpa_micros
、cpc_bid_ceiling_micros
、cpc_bid_floor_micros
の 3 つの値を持つ MaximizeConversions
など)、または何も指定しないことも可能です(例: ManualCpm
)。
サブフィールドが定義されていないメッセージ フィールド
どのサブフィールドでも定義されていない MESSAGE
フィールドを更新する場合は、前述のように FieldMasks
を使用してフィールド マスクを生成します。
サブフィールドが定義されたメッセージ フィールド
メッセージのサブフィールドを明示的に設定せずにサブフィールドで定義された MESSAGE
フィールドを更新する場合は、フィールド マスクをゼロから作成した前の例と同様に、変更可能な各 MESSAGE
サブフィールドを FieldMask
に手動で追加する必要があります。
一般的な例としては、新しい入札戦略のフィールドを設定せずにキャンペーンの入札戦略を更新する場合があります。以下のコードは、入札戦略のサブフィールドを設定せずに、MaximizeConversions
入札戦略を使用するようにキャンペーンを更新する方法を示しています。
この場合、FieldMasks
の allSetFieldsOf()
メソッドと compare()
メソッドを使用しては、意図した目標を達成できません。
次のコードは、maximize_conversions
を含むフィールド マスクを生成します。ただし、誤ってフィールドをクリアして FieldMaskError.FIELD_HAS_SUBFIELDS
エラーが生成されるのを防ぐため、Google Ads API ではこの動作は許可されていません。
// 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 will include 'maximize_conversions`,
// which will produce 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 will result in a
// FieldMaskError.FIELD_HAS_SUBFIELDS error because empty MESSAGE fields cannot
// be included in a field mask.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($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 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 = 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 here:
// https://developers.google.com/google-ads/api/docs/campaigns/bidding/assign-strategies
$fieldMask->setPaths(array_merge(
iterator_to_array($fieldMask->getPaths()->getIterator()),
['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);
フィールドをクリアする
一部のフィールドは明示的にクリアできます。前の例と同様に、これらのフィールドをフィールド マスクに明示的に追加する必要があります。たとえば、キャンペーンで 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' => 0]),
'status' => CampaignStatus::PAUSED
]);
// 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 = new CampaignOperation();
$campaignOperation->setUpdate($campaign);
$campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign));
// 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.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns($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 will
// clear this field from the bidding strategy without impacting any other fields
// on the bidding strategy.
$fieldMask = FieldMasks::allSetFieldsOf($campaign);
$fieldMask->setPaths(array_merge(
iterator_to_array($fieldMask->getPaths()->getIterator()),
['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 の protocol buffers
で optional
として定義されているフィールドには意図したとおりに機能します。しかし、target_cpa_micros
は optional
フィールドではないため、「不適切な」コードによって入札戦略が更新されて target_cpa
フィールドが削除されることはありません。