欄位遮罩

在 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::compare(new Campaign(), $campaign),而非 FieldMasks::allSetFieldsOf($campaign)。

更新訊息欄位及其子欄位

MESSAGE 欄位可以有子欄位 (例如 MaximizeConversions,其中包含 target_cpa_micros、cpc_bid_ceiling_micros 和 cpc_bid_floor_micros 等子欄位),也可以完全沒有子欄位 (例如 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 會忽略設為預設值的 Proto3 純量欄位 (例如 0、false 或 "")。舉例來說,假設您有一個廣告活動採用 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。