← 返回目录
来源:Google Ads API 官方文档 · 中文版 · 本地归档

为关键字申请豁免

如需申请关键字豁免,请执行以下操作:

  1. 存储 PolicyViolationDetails 中包含的所有可豁免的政策违规键,这些键会在您首次尝试创建关键字失败时返回。
  2. 发送 mutate 请求,通过添加存储的可豁免政策违规键来再次创建关键字。

存储所有可豁免的政策违规键

关键字的错误详情包含在 PolicyViolationDetails中,而 又存储了key字段,该字段的值是 PolicyViolationKey的对象。

收集所有可豁免的政策违规键,以便在下一步中使用:

Java

private List<PolicyViolationKey> extractExemptiblePolicyViolationKeys(
    GoogleAdsException googleAdsException) {
  List<PolicyViolationKey> exemptibleKeys = new ArrayList<>();
  System.out.println("Google Ads failure details:");
  for (GoogleAdsError googleAdsError : googleAdsException.getGoogleAdsFailure().getErrorsList()) {
    System.out.printf("\t%s: %s%n", googleAdsError.getErrorCode(), googleAdsError.getMessage());
    if (googleAdsError.hasDetails() && googleAdsError.getDetails().hasPolicyViolationDetails()) {
      PolicyViolationDetails policyViolationDetails =
          googleAdsError.getDetails().getPolicyViolationDetails();
      System.out.println("\tPolicy violation details:");
      System.out.printf(
          "\t\tExternal policy name: '%s'%n", policyViolationDetails.getExternalPolicyName());
      System.out.printf(
          "\t\tExternal policy description: '%s'%n",
          policyViolationDetails.getExternalPolicyDescription());
      System.out.printf("\t\tIs exemptible? '%s'%n", policyViolationDetails.getIsExemptible());
      if (policyViolationDetails.getIsExemptible() && policyViolationDetails.hasKey()) {
        PolicyViolationKey policyViolationKey = policyViolationDetails.getKey();
        exemptibleKeys.add(policyViolationKey);
        System.out.println("\t\tPolicy violation key:");
        System.out.printf("\t\t\tName: '%s'%n", policyViolationKey.getPolicyName());
        System.out.printf("\t\t\tViolating text: '%s'%n", policyViolationKey.getViolatingText());
      }
    }
  }
  return exemptibleKeys;
}
      

C#

private static PolicyViolationKey[] FetchExemptPolicyViolationKeys(GoogleAdsException ex)
{
    bool isFullyExemptable = true;
    List<PolicyViolationKey> exemptPolicyViolationKeys = new List<PolicyViolationKey>();

    Console.WriteLine("Google Ads failure details:");
    foreach (GoogleAdsError error in ex.Failure.Errors)
    {
        if (error.ErrorCode.ErrorCodeCase !=
            ErrorCode.ErrorCodeOneofCase.PolicyViolationError)
        {
            Console.WriteLine("No exemption request is sent because there are other " +
                "non-policy related errors thrown.");
            throw ex;
        }
        if (error.Details != null && error.Details.PolicyViolationDetails != null)
        {
            PolicyViolationDetails details = error.Details.PolicyViolationDetails;
            Console.WriteLine($"- Policy violation details:");

            Console.WriteLine(" - Policy violation details:");
            Console.WriteLine($"   - External policy name: '{details.ExternalPolicyName}'");
            Console.WriteLine($"   - External policy description: " +
                $"'{details.ExternalPolicyDescription}'");
            Console.WriteLine($"   - Is exemptable: '{details.IsExemptible}'");

            if (details.IsExemptible && details.Key != null)
            {
                PolicyViolationKey key = details.Key;
                Console.WriteLine($"   - Policy violation key:");
                Console.WriteLine($"     - Name: {key.PolicyName}");
                Console.WriteLine($"     - Violating Text: {key.ViolatingText}");
                exemptPolicyViolationKeys.Add(key);
            }
            else
            {
                isFullyExemptable = false;
            }
        }
    }

    if (!isFullyExemptable)
    {
        Console.WriteLine("No exemption request is sent because your keyword " +
            "contained some non-exemptible policy violations.");
        throw ex;
    }
    return exemptPolicyViolationKeys.ToArray();
}
      

PHP

private static function fetchExemptPolicyViolationKeys(GoogleAdsException $googleAdsException)
{
    $exemptPolicyViolationKeys = [];

    printf("Google Ads failure details:%s", PHP_EOL);
    foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
        /** @var GoogleAdsError $error */
        printf(
            "\t%s: %s%s",
            $error->getErrorCode()->getErrorCode(),
            $error->getMessage(),
            PHP_EOL
        );
        if (
            !is_null($error->getDetails())
            && !is_null($error->getDetails()->getPolicyViolationDetails())
        ) {
            $policyViolationDetails = $error->getDetails()->getPolicyViolationDetails();
            printf("\tPolicy violation details:%s", PHP_EOL);
            printf(
                "\t\tExternal policy name: '%s'%s",
                $policyViolationDetails->getExternalPolicyName(),
                PHP_EOL
            );
            printf(
                "\t\tExternal policy description: '%s'%s",
                $policyViolationDetails->getExternalPolicyDescription(),
                PHP_EOL
            );
            printf(
                "\t\tIs exemptible? '%s'%s",
                $policyViolationDetails->getIsExemptible() ? 'yes' : 'no',
                PHP_EOL
            );

            if (
                $policyViolationDetails->getIsExemptible() &&
                !is_null($policyViolationDetails->getKey())
            ) {
                $policyViolationDetailsKey = $policyViolationDetails->getKey();
                $exemptPolicyViolationKeys[] = $policyViolationDetailsKey;
                printf("\t\tPolicy violation key:%s", PHP_EOL);
                printf(
                    "\t\t\tName: '%s'%s",
                    $policyViolationDetailsKey->getPolicyName(),
                    PHP_EOL
                );
                printf(
                    "\t\t\tViolating text: '%s'%s",
                    $policyViolationDetailsKey->getViolatingText(),
                    PHP_EOL
                );
            } else {
                print "No exemption request is sent because your keyword contained some "
                    . "non-exemptible policy violations." . PHP_EOL;
                throw $googleAdsException;
            }
        } else {
            print "No exemption request is sent because there are other non-policy related "
                . "errors thrown." . PHP_EOL;
            throw $googleAdsException;
        }
    }
    return $exemptPolicyViolationKeys;
}
在接这个接口时卡住了?
Stuck wiring this API into production? We build ad-delivery tooling on these platforms — campaign automation, reporting pipelines, error-handling and migration between API versions.
联系我们 · Talk to us 每日平台更新简报 →