PolicyViolationError.POLICY_ERROR - How is this possible?

95 views
Skip to first unread message

de...@ubc-corp.com

unread,
Jun 22, 2017, 4:22:03 AM6/22/17
to AdWords API Forum
Hi. Please, help me.

I'm using a check. As written in the documentation.

However, the ad is not added anyway! I do not understand why.

Look at my code, please.

What could be the reason?

$session->setValidateOnly(true);
$adWordsServices = new AdWordsServices();
$adGroupAdService = $adWordsServices->get($session, AdGroupAdService::class);
$operations = [];
$expandedTextAd = new ExpandedTextAd();
$expandedTextAd->setHeadlinePart1($HeadlinePart1);
$expandedTextAd->setHeadlinePart2($HeadlinePart2);
$expandedTextAd->setDescription($sourceAdText); 
$expandedTextAd->setFinalUrls([$sourceAdHref]);

$adGroupAd = new AdGroupAd();
$adGroupAd->setAdGroupId($adsGroupsSync[$sourceAdGroupId]);
$adGroupAd->setAd($expandedTextAd);
$adGroupAd->setStatus(AdGroupAdStatus::PAUSED);

 $operation = new AdGroupAdOperation();
 operation->setOperand($adGroupAd);
 $operation->setOperator(Operator::ADD);
 $operations[] = $operation;
 
 
            try {
                // Try creating an ad group ad on the server.
                $result = $adGroupAdService->mutate($operations);
            } catch (ApiException $apiException) {
                $operationIndicesToRemove = [];
                foreach ($apiException->getErrors() as $error) {
                    // Get the index of the failed operation from the error's field path
                    // elements.
                    $fieldPathElements = $error->getFieldPathElements();
                    $firstFieldPathElement = null;
                    if ($fieldPathElements !== null && count($fieldPathElements) > 0) {
                        $firstFieldPathElement = $fieldPathElements[0];
                    }
                    if ($firstFieldPathElement === null
                        || $firstFieldPathElement->getField() !== 'operations'
                        || $firstFieldPathElement->getIndex() === null) {
                        // If the operation index is not present on the first error field
                        // path element, then there's no way to determine which operation to
                        // remove, so simply throw the exception.
                        throw $apiException;
                    }
                    $operationIndex = $firstFieldPathElement->getIndex();
                    $operation = $operations[$operationIndex];
                    if ($error instanceof PolicyViolationError) {
                        if ($error->getIsExemptable() === true) {
                            $operation->setExemptionRequests(
                                [new ExemptionRequest($error->getKey())]);
                        } else {
                            $operationIndicesToRemove[] = $operationIndex;
                        }
                    } else {
                        $operationIndicesToRemove[] = $operationIndex;
                    }
                }
                $operationIndicesToRemove = array_unique($operationIndicesToRemove);
                rsort($operationIndicesToRemove, SORT_NUMERIC);
                foreach ($operationIndicesToRemove as $operationIndex) {
                    unset($operations[$operationIndex]);
                }
            }

            if (count($operations) > 0) {
                // Make the mutate request to really add an ad group ad.
                $session->setValidateOnly(false);

                try {
                    $result = $adGroupAdService->mutate($operations);
                } catch (ApiException $e) {
                    $errors = $e->getErrors();
                    if (count($errors) > 0) {
                        foreach ($errors as $error) {
                            // todo
                        }
                    }
                }
            }

Peter Oliquino

unread,
Jun 22, 2017, 5:11:39 AM6/22/17
to AdWords API Forum
Hi,

To better identify the cause of the issue, could you provide the generated SOAP request and response? If you have not enabled SOAP logging, please refer to this guide as to how. Please reply using Reply privately to author.

Best regards,
Peter
AdWords API Team
Reply all
Reply to author
Forward
0 new messages