부분 실패

Google Ads API에서는 유효한 작업이 커밋되고 실패한 작업이 오류를 반환하도록 요청할 수 있습니다. 부분 실패라고 하는 이 기능을 사용하면 실패한 작업을 마지막에 별도로 처리할 수 있습니다. 이 값은 여러 Google Ads API 서비스의 get이 아닌 메서드에서 사용할 수 있습니다.

기술 세부정보

이 기능을 활용하려면 사용하려는 메서드의 partialFailure 매개변수를 설정하세요. 이는 클라이언트 라이브러리마다 약간 다를 수 있습니다. 예를 들어 MutateAdGroups을 사용하여 요청을 보낼 때 이 기능을 사용 설정하려면 다음을 실행하세요.

자바

mutateAdGroups(String.valueOf(customerId), operations, true)

C#

MutateAdGroups(customerId.ToString(), operations, true, false)

PHP

mutateAdGroups($customerId, $operations, ['partialFailure' => true])

Python

mutate_ad_groups(customer_id, operations, partial_failure=True)

Ruby

mutate_ad_groups(customer_id, operations, partial_failure: true)

Perl

mutate({customerId => $customer_id, operations => $operations, partialFailure => 'true'})

서비스는 오류가 없는 작업을 실행합니다. 성공한 작업의 결과와 실패한 작업의 오류를 반환합니다.

사용

계정에 광고 그룹을 추가해야 하는데 일부 광고 그룹에 문제가 있을 수 있다고 가정해 보겠습니다. 유효한 광고 그룹을 만들고 싶지만 오류와 함께 실패한 광고 그룹이 반환됩니다.

부분 실패를 방지해야 하는 경우

상호 종속적인 작업이 있는 요청에는 부분 실패를 사용하면 안 됩니다.

예를 들어 실적 최대화 캠페인과 같이 단일 요청에서 여러 리소스를 만들어야 하는 복잡한 항목을 만드는 경우 부분 오류를 사용하면 안 됩니다. 실적 최대화 캠페인을 처음 만들 때는 동일한 요청에서 CampaignCampaignAsset 리소스를 만들어야 합니다. Campaign 생성이 실패하면 CampaignAsset 생성도 실패하므로 요청을 단일 원자적 실패로 취급해야 합니다.

또 다른 예로, AssetGroup에 필수 애셋을 처음 추가할 때는 부분 실패를 사용하지 마세요. 애셋 그룹은 애셋 없이 또는 모든 필수 애셋을 포함하여 만들 수 있습니다. 애셋 그룹에 애셋을 처음 추가할 때는 별도의 요청에서 한 번에 하나씩 추가할 수 없습니다. 모든 항목을 하나의 요청에 포함해야 하며 부분 실패를 사용해서는 안 됩니다. 애셋 그룹에 필요한 애셋이 모두 포함된 후에는 애셋을 한 번에 하나씩 추가하거나 삭제할 수 있으며 이러한 요청은 부분 실패를 사용할 수 있습니다.

부분 실패가 적절한지 확인하려면 동일한 요청 내에서 교차 작업 리소스 참조에 임시 ID가 필요한지 확인하세요. 이 경우 두 번째 작업의 성공 여부가 첫 번째 작업의 성공 여부에 따라 달라지므로 부분 실패를 사용하면 안 됩니다.

작업을 만들고 API 호출하기

변이 작업을 만들고 평소와 같이 API를 호출합니다.

자바

private MutateAdGroupsResponse createAdGroups(
    GoogleAdsClient googleAdsClient, long customerId, long campaignId) {
  // This AdGroup should be created successfully - assuming the campaign in the params exists.
  AdGroup group1 =
      AdGroup.newBuilder()
          .setCampaign(ResourceNames.campaign(customerId, campaignId))
          .setName("Valid AdGroup: " + getPrintableDateTime())
          .build();
  // This AdGroup will always fail - campaign ID 0 in resource names is never valid.
  AdGroup group2 =
      AdGroup.newBuilder()
          .setCampaign(ResourceNames.campaign(customerId, 0L))
          .setName("Broken AdGroup: " + getPrintableDateTime())
          .build();
  // This AdGroup will always fail - duplicate ad group names are not allowed.
  AdGroup group3 =
      AdGroup.newBuilder()
          .setCampaign(ResourceNames.campaign(customerId, campaignId))
          .setName(group1.getName())
          .build();

  AdGroupOperation op1 = AdGroupOperation.newBuilder().setCreate(group1).build();
  AdGroupOperation op2 = AdGroupOperation.newBuilder().setCreate(group2).build();
  AdGroupOperation op3 = AdGroupOperation.newBuilder().setCreate(group3).build();

  try (AdGroupServiceClient service =
      googleAdsClient.getLatestVersion().createAdGroupServiceClient()) {
    // Issues the mutate request, setting partialFailure=true.
    return service.mutateAdGroups(
        MutateAdGroupsRequest.newBuilder()
            .setCustomerId(String.valueOf(customerId))
            .setCustomerId(Long.toString(customerId))
            .addAllOperations(Arrays.asList(op1, op2, op3))
            .setPartialFailure(true)
            .build());
  }
}
      

C#

private static MutateAdGroupsResponse CreateAdGroups(GoogleAdsClient client,
    long customerId, long campaignId)
{
    // Get the AdGroupServiceClient.
    AdGroupServiceClient adGroupService = client.GetService(Services.V22.AdGroupService);

    string validAdGroupName = "Valid AdGroup: " + ExampleUtilities.GetRandomString();

    AdGroupOperation[] operations = new AdGroupOperation[]
    {
        // This operation will be successful, assuming the campaign specified in
        // campaignId parameter is correct.
        new AdGroupOperation()
        {
            Create = new AdGroup()
            {
                Campaign = ResourceNames.Campaign(customerId, campaignId),
                Name = validAdGroupName
            }
        },
        // This operation will fail since we are using campaign ID = 0, which results
        // in an invalid resource name.
        new AdGroupOperation()
        {
            Create = new AdGroup()
            {
                Campaign = ResourceNames.Campaign(customerId, 0),
                Name = "Broken AdGroup: " + ExampleUtilities.GetRandomString()
            },
        },
        // This operation will fail since the ad group is using the same name as the ad
        // group from the first operation. Duplicate ad group names are not allowed.
        new AdGroupOperation()
        {
            Create = new AdGroup()
            {
                Campaign = ResourceNames.Campaign(customerId, campaignId),
                Name = validAdGroupName
            }
        }
    };

    // Add the ad groups.
    MutateAdGroupsResponse response =
        adGroupService.MutateAdGroups(new MutateAdGroupsRequest()
        {
            CustomerId = customerId.ToString(),
            Operations = { operations },
            PartialFailure = true,
            ValidateOnly = false
        });
    return response;
}
      

PHP

private static function createAdGroups(
    GoogleAdsClient $googleAdsClient,
    int $customerId,
    int $campaignId
) {
    $campaignResourceName = ResourceNames::forCampaign($customerId, $campaignId);

    // This ad group should be created successfully - assuming the campaign in the params
    // exists.
    $adGroup1 = new AdGroup([
        'name' => 'Valid AdGroup #' . Helper::getPrintableDatetime(),
        'campaign' => $campaignResourceName
    ]);

    // This ad group will always fail - campaign ID 0 in the resource name is never valid.
    $adGroup2 = new AdGroup([
        'name' => 'Broken AdGroup #' . Helper::getPrintableDatetime(),
        'campaign' => ResourceNames::forCampaign($customerId, 0)
    ]);

    // This ad group will always fail - duplicate ad group names are not allowed.
    $adGroup3 = new AdGroup([
        'name' => $adGroup1->getName(),
        'campaign' => $campaignResourceName
    ]);

    $operations = [];

    $adGroupOperation1 = new AdGroupOperation();
    $adGroupOperation1->setCreate($adGroup1);
    $operations[] = $adGroupOperation1;

    $adGroupOperation2 = new AdGroupOperation();
    $adGroupOperation2->setCreate($adGroup2);
    $operations[] = $adGroupOperation2;

    $adGroupOperation3 = new AdGroupOperation();
    $adGroupOperation3->setCreate($adGroup3);
    $operations[] = $adGroupOperation3;

    // Issues the mutate request, enabling partial failure mode.
    $adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient();
    return $adGroupServiceClient->mutateAdGroups(
        MutateAdGroupsRequest::build($customerId, $operations)->setPartialFailure(true)
    );
}