-
Notifications
You must be signed in to change notification settings - Fork 770
/
HandlePolicyViolationError.php
executable file
·167 lines (153 loc) · 6.5 KB
/
HandlePolicyViolationError.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AdsApi\Examples\AdWords\v201710\ErrorHandling;
require __DIR__ . '/../../../../vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201710\cm\AdGroupAd;
use Google\AdsApi\AdWords\v201710\cm\AdGroupAdOperation;
use Google\AdsApi\AdWords\v201710\cm\AdGroupAdService;
use Google\AdsApi\AdWords\v201710\cm\ApiException;
use Google\AdsApi\AdWords\v201710\cm\ExemptionRequest;
use Google\AdsApi\AdWords\v201710\cm\ExpandedTextAd;
use Google\AdsApi\AdWords\v201710\cm\Operator;
use Google\AdsApi\AdWords\v201710\cm\PolicyViolationError;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example demonstrates how to handle policy violation errors when creating
* text ads.
*/
class HandlePolicyViolationError {
const AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE';
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSession $session, $adGroupId) {
$session->setValidateOnly(true);
$adGroupAdService =
$adWordsServices->get($session, AdGroupAdService::class);
// Create text ad that violates an exemptable policy.
$expandedTextAd = new ExpandedTextAd();
$expandedTextAd->setHeadlinePart1('Mars Cruise !!!');
$expandedTextAd->setHeadlinePart2('Visit the Red Planet in style.');
$expandedTextAd->setDescription('Low-gravity fun for everyone!');
$expandedTextAd->setFinalUrls(['http://www.example.com']);
// Create ad group ad.
$adGroupAd = new AdGroupAd();
$adGroupAd->setAdGroupId($adGroupId);
$adGroupAd->setAd($expandedTextAd);
// Create ad group ad operation and add it to the list.
$operations = [];
$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) {
printf("Ad with headline part 1 '%s' violated %s policy '%s'.\n",
$operation->getOperand()->getAd()->getHeadlinePart1(),
$error->getIsExemptable() ? 'exemptable' : 'non-exemptable',
$error->getExternalPolicyName()
);
if ($error->getIsExemptable() === true) {
// Add exemption request to the operation.
printf(
"Adding exemption request for policy name '%s' on text "
."'%s'.\n",
$error->getKey()->getPolicyName(),
$error->getKey()->getViolatingText()
);
$operation->setExemptionRequests(
[new ExemptionRequest($error->getKey())]);
} else {
// Remove non-exemptable operation.
print "Removing non-exemptable operation from the request.\n";
$operationIndicesToRemove[] = $operationIndex;
}
} else {
// Non-policy error returned.
printf(
"Ad with headline part 1 '%s' created non-policy error '%s'.\n",
$operation->getOperand()->getAd()->getHeadlinePart1(),
$error->getErrorString()
);
print "Removing the operation causing non-policy error from the "
. "request.\n";
$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);
$result = $adGroupAdService->mutate($operations);
// Print out some information about the created ad group ad.
foreach ($result->getValue() as $adGroupAd) {
printf(
"Expanded text ad with headline part 1 '%s' and ID '%s' was added."
. "\n",
$adGroupAd->getAd()->getHeadlinePart1(),
$adGroupAd->getAd()->getId()
);
}
} else {
print "All the operations were invalid with non-exemptable errors.\n";
}
}
public static function main() {
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile()
->build();
// Construct an API session configured from a properties file and the OAuth2
// credentials above.
// Partial failure behavior is also enabled in this example.
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
self::runExample(
new AdWordsServices(), $session, intval(self::AD_GROUP_ID));
}
}
HandlePolicyViolationError::main();