字段掩码

在 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 字段。