-
Notifications
You must be signed in to change notification settings - Fork 479
/
handle_responsive_search_ad_policy_violations.py
executable file
·243 lines (210 loc) · 8.8 KB
/
handle_responsive_search_ad_policy_violations.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# 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
#
# https://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.
"""Requests an exemption for policy violations of a responsive search ad.
If the request somehow fails with exceptions that are not policy finding
errors, the example will stop instead of trying sending an exemption request.
"""
import argparse
import sys
import uuid
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
def main(client, customer_id, ad_group_id):
"""Uses Customer Match to create and add users to a new user list.
Args:
client: The Google Ads client.
customer_id: The customer ID for which to add the responsive search ad.
ad_group_id: The ad group ID to which to add a responsive search ad.
"""
ad_group_ad_service_client = client.get_service("AdGroupAdService")
ad_group_ad_operation = create_responsive_search_ad(
client, ad_group_ad_service_client, customer_id, ad_group_id
)
ignorable_policy_topics = []
try:
# Try sending a mutate request to add the ad group ad.
ad_group_ad_service_client.mutate_ad_group_ads(
customer_id=customer_id, operations=[ad_group_ad_operation]
)
except GoogleAdsException as googleads_exception:
# The request will always fail due to the policy violation in the
# ad's description.
ignorable_policy_topics = fetch_ignorable_policy_topics(
client, googleads_exception
)
request_exemption(
customer_id,
ad_group_ad_service_client,
ad_group_ad_operation,
ignorable_policy_topics,
)
def create_responsive_search_ad(
client, ad_group_ad_service_client, customer_id, ad_group_id
):
"""Create a responsive search ad that includes a policy violation.
Args:
client: The GoogleAds client instance.
ad_group_ad_service_client: The AdGroupAdService client instance.
customer_id: The customer ID for which to add the responsive search ad.
ad_group_id: The ad group ID to which to add a responsive search ad.
Returns:
The attempted AdGroupAdOperation instance.
"""
ad_group_resource_name = client.get_service("AdGroupService").ad_group_path(
customer_id, ad_group_id
)
# Creates an operation and ad group ad to create and hold the above ad.
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
ad_group_ad = ad_group_ad_operation.create
ad_group_ad.ad_group = ad_group_resource_name
# Set the ad group ad to PAUSED to prevent it from immediately serving.
# Set to ENABLED once you've added targeting and the ad are ready to serve.
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.PAUSED
# Sets the responsive search ad info on an ad.
responsive_search_ad_info = ad_group_ad.ad.responsive_search_ad
headline_1 = client.get_type("AdTextAsset")
headline_1.text = f"Cruise to Mars #{str(uuid.uuid4())[0:13]}"
headline_2 = client.get_type("AdTextAsset")
headline_2.text = "Best Space Cruise Line"
headline_3 = client.get_type("AdTextAsset")
headline_3.text = "Experience the Stars"
responsive_search_ad_info.headlines.extend(
[headline_1, headline_2, headline_3]
)
# Intentionally use an ad text that violates policy by having too many
# exclamation marks.
description_1 = client.get_type("AdTextAsset")
description_1.text = "Buy your tickets now!!!!!!!"
description_2 = client.get_type("AdTextAsset")
description_2.text = "Visit the Red Planet"
responsive_search_ad_info.descriptions.extend(
[description_1, description_2]
)
ad_group_ad.ad.final_urls.append("https://www.example.com")
return ad_group_ad_operation
# [START handle_responsive_search_ad_policy_violations]
def fetch_ignorable_policy_topics(client, googleads_exception):
"""Collects all ignorable policy topics to be sent for exemption request.
Args:
client: The GoogleAds client instance.
googleads_exception: The exception that contains the policy
violation(s).
Returns:
A list of ignorable policy topics.
"""
ignorable_policy_topics = []
print("Google Ads failure details:")
for error in googleads_exception.failure.errors:
if (
error.error_code.policy_finding_error
!= client.get_type(
"PolicyFindingErrorEnum"
).PolicyFindingError.POLICY_FINDING
):
print(
"This example supports sending exemption request for the "
"policy finding error only."
)
raise googleads_exception
print(f"\t{error.error_code.policy_finding_error}: {error.message}")
if (
error.details is not None
and error.details.policy_finding_details is not None
):
policy_finding_details = error.details.policy_finding_details
print("\tPolicy finding details:")
for (
policy_topic_entry
) in policy_finding_details.policy_topic_entries:
ignorable_policy_topics.append(policy_topic_entry.topic)
print(f"\t\tPolicy topic name: '{policy_topic_entry.topic}'")
print(
f"\t\tPolicy topic entry type: '{policy_topic_entry.type_}'"
)
# For the sake of brevity, we exclude printing "policy topic
# evidences" and "policy topic constraints" here. You can fetch
# those data by calling:
# - policy_topic_entry.evidences
# - policy_topic_entry.constraints
return ignorable_policy_topics
# [END handle_responsive_search_ad_policy_violations]
# [START handle_responsive_search_ad_policy_violations_1]
def request_exemption(
customer_id,
ad_group_ad_service_client,
ad_group_ad_operation,
ignorable_policy_topics,
):
"""Sends exemption requests for creating a responsive search ad.
Args:
customer_id: The customer ID for which to add the responsive search ad.
ad_group_ad_service_client: The AdGroupAdService client instance.
ad_group_ad_operation: The AdGroupAdOperation that returned policy
violation(s).
ignorable_policy_topics: The extracted list of policy topic entries.
"""
print(
"Attempting to add a responsive search ad again by requesting "
"exemption for its policy violations."
)
ad_group_ad_operation.policy_validation_parameter.ignorable_policy_topics.extend(
ignorable_policy_topics
)
response = ad_group_ad_service_client.mutate_ad_group_ads(
customer_id=customer_id, operations=[ad_group_ad_operation]
)
print(
"Successfully added a responsive search ad with resource name "
f"'{response.results[0].resource_name}' for policy violation "
"exemption."
)
# [END handle_responsive_search_ad_policy_violations_1]
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(version="v13")
parser = argparse.ArgumentParser(
description="Requests an exemption for responsive search ad policy "
"violations."
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-a",
"--ad_group_id",
type=str,
required=True,
help="The ad group ID to which to add a responsive search ad.",
)
args = parser.parse_args()
try:
main(googleads_client, args.customer_id, args.ad_group_id)
except GoogleAdsException as ex:
print(
f"Request with ID '{ex.request_id}' failed with status "
f"'{ex.error.code().name}' and includes the following errors:"
)
for error in ex.failure.errors:
print(f"\tError with message '{error.message}'.")
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)