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
}
}
}
}
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+:
https://googleadsdeveloper.blogspot.com/
https://plus.google.com/+GoogleAdsDevelopers/posts
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/ad539448-5053-4012-a0d6-85d06b2aa5df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.