欄位遮罩

在 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_micros, cpc_bid_ceiling_microscpc_bid_floor_micros),或者沒有 (例如 ManualCpm)。

未定義子欄位的訊息欄位

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

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

更新不含子欄位定義的 MESSAGE 欄位時 明確設定該訊息的任何子欄位,您必須手動 將每個可變動 MESSAGE 子欄位新增至 FieldMask,如下所示: 上述範例是從頭建立欄位遮罩

有一個常見的例子,就是更新廣告活動的出價策略時,並未設定任何 新出價策略欄位的部分。以下程式碼示範如何 更新廣告活動 MaximizeConversions 出價策略 不必在出價策略中設定任何子欄位

在這種情況下,請使用 allSetFieldsOf()compare() FieldMasks 未達成預期目標,

以下程式碼會產生包含 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]);

下列程式碼示範如何正確清除 target_cpa_micros ] 欄位。MaximizeConversions

// 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」欄位。