在 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 会忽略设置为默认值(例如 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。