フィールド マスク

Google Ads API では、更新はフィールド マスクを使用して行われます。フィールド マスクには、更新で変更するすべてのフィールドが一覧表示されます。フィールド マスクにない指定されたフィールドは、サーバーに送信されても無視されます。

FieldMaskUtil

フィールド マスクを生成するには、組み込みのフィールド マスク ユーティリティを使用することをおすすめします。このユーティリティを使用すると、ゼロから構築するのではなく、変更されたオブジェクトからフィールド マスクを生成できます。

キャンペーンを更新する例を次に示します。

// Update campaign by setting its status to paused, and "Search network" to false.
Campaign campaignToUpdate = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    Status = CampaignStatus.Paused,
    NetworkSettings = new NetworkSettings()
    {
        TargetSearchNetwork = false
    }
};

// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaignToUpdate,
    UpdateMask = FieldMasks.AllSetFieldsOf(campaignToUpdate)
};

// Update the campaign.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

まず、空の Campaign オブジェクトを作成します。次に、更新するキャンペーンを API が正確に認識できるように、リソース名を設定します。

この例では、キャンペーンの FieldMasks.AllSetFieldsOf メソッドを使用して、設定されたすべてのフィールドを列挙するフィールド マスクを自動的に生成します。返されたマスクを更新呼び出しに直接渡すことができます。

既存のオブジェクトを操作して、いくつかのフィールドを更新する必要がある場合があります。このような場合は、コードを次のように変更します。

Campaign existingCampaign;

// Obtain existingCampaign from elsewhere.
...

// Create a new campaign based off the existing campaign for update.
Campaign campaignToUpdate = new Campaign(existingCampaign);

// Update campaign by setting its status to paused, and "Search network" to
// false.
campaignToUpdate.Status = CampaignStatus.Paused;
campaignToUpdate.NetworkSettings = new NetworkSettings()
{
    TargetSearchNetwork = false
}

// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaignToUpdate,
    UpdateMask = FieldMasks.FromChanges(existingCampaign, campaignToUpdate)
};

フィールドマスクを最初から作成するには、まず FieldMask オブジェクトを作成し、変更するすべてのフィールドの名前を含む配列を作成し、最後に配列の内容をフィールドマスクの Path フィールドに追加します。

FieldMask fieldMask = new FieldMask();
fieldMask.Paths.AddRange(new string[] { "status", "name" });

メッセージ フィールドとそのサブフィールドの更新

MESSAGE フィールドにはサブフィールドを含めることができます(target_cpa_microscpc_bid_ceiling_microscpc_bid_floor_micros の 3 つを含む MaximizeConversions など)。サブフィールドを含めない場合もあります(ManualCpm など)。

サブフィールドが定義されていないメッセージ フィールド

サブフィールドで定義されていない MESSAGE フィールドを更新する場合は、上記のように FieldMasks を使用してフィールド マスクを生成します。

サブフィールドが定義されたメッセージ フィールド

サブフィールドで定義された MESSAGE フィールドを更新するときに、そのメッセージのサブフィールドを明示的に設定しない場合、上記の例でフィールド マスクを最初から作成する場合と同様に、変更可能MESSAGE サブフィールドを FieldMask に手動で追加する必要があります。

よくある例としては、新しい入札戦略のフィールドを設定せずに、キャンペーンの入札戦略を更新することがあります。次の例は、入札戦略のサブフィールドを設定せずに MaximizeConversions 入札戦略を使用するようにキャンペーンを更新する方法を示しています。

この場合、FieldMasksAllSetFieldsOf() メソッドと FromChanges() メソッドを使用しても、目的の目標は達成されません。

次の例では、maximize_conversions を含むフィールド マスクを生成します。ただし、Google Ads API では、フィールドが誤って消去されないようにするために、この動作を許可していません。このため、FieldMaskError.FIELD_HAS_SUBFIELDS エラーが発生します。

// Creates a campaign with the proper resource name and an empty
// MaximizeConversions field.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    MaximizeConversions = 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 operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = 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.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

次の例は、MaximizeConversions 入札戦略を使用するようにキャンペーンを適切に更新し、サブフィールドを設定しない方法を示しています。

// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
};

// Creates a field mask from the existing campaign and adds all of the fields
// on the MaximizeConversions bidding strategy to the field mask. Because these
// fields are 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 with none of its subfields set.
FieldMask 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.Paths.AddRange(new string[] {
    "maximize_conversions.target_cpa_micros",
});

// Creates an operation to update the campaign with the specified fields.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = 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 campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(customerId, campaignId),
    MaximizeConversions = new MaximizeConversions()
    {
        TargetCpaMicros = 0
    }
};

// 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 operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = 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.
MutateCampaignsResponse response = campaignService.MutateCampaigns(
    customerId.ToString(), new CampaignOperation[] { operation });

次の例は、MaximizeConversions 入札戦略の target_cpa_micros フィールドを適切に消去する方法を示しています。

// Creates a Campaign object with the proper resource name.
Campaign campaign = new Campaign()
{
    ResourceName = ResourceNames.Campaign(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 fieldMask = FieldMasks.AllSetFieldsOf(campaign);
fieldMask.Paths.AddRange(new string[] {
    "maximize_conversions.target_cpa_micros",
});

// Creates an operation to update the campaign with the specified field.
CampaignOperation operation = new CampaignOperation()
{
    Update = campaign,
    UpdateMask = fieldMask
};

なお、上記の「正しくない」例は、Google 広告 API の protocol buffersoptional として定義されているフィールドに対しては、意図したとおりに機能します。ただし、target_cpa_microsoptional フィールドではないため、「正しくない」例では、入札戦略が更新されず、target_cpa フィールドはクリアされません。