欄位遮罩

在 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() 的便利方法。可比較傳遞的物件與相同類別的空白物件。舉例來說,在先前的程式碼中,您可能使用了 FieldMasks::compare(new Campaign(), $campaign) 而不是 allSetFieldsOf()

更新訊息欄位及其子欄位

MESSAGE 欄位可以有子欄位 (例如 MaximizeConversions,其中包含三個:target_cpa_microscpc_bid_ceiling_microscpc_bid_floor_micros),或者也可以完全沒有子欄位 (例如 ManualCpm)。

未定義子欄位的訊息欄位

更新未以任何子欄位定義的 MESSAGE 欄位時,請按照先前所述,使用 FieldMasks 產生欄位遮罩。

含有已定義子欄位的訊息欄位

更新使用子欄位定義的 MESSAGE 欄位時,並未明確設定該訊息中的任何子欄位,您必須手動將每個「可變動」MESSAGE 子欄位新增至 FieldMask,類似之前從頭開始建立欄位遮罩的範例。

其中一個常見例子是在不設定新出價策略的任何欄位的情況下,更新廣告活動的出價策略。以下程式碼示範如何在不為出價策略設定任何子欄位的情況下,將廣告活動更新為使用 MaximizeConversions 出價策略。

在此情況下,使用 FieldMasksallSetFieldsOf()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 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 欄位。