Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 346eeac

Browse files
authored
Validate Text Ad example (googleads#212)
* validate_text_ad: improved error handling for non policy violations
1 parent 04f310e commit 346eeac

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""This example shows use of the validateOnly header for an expanded text ad.
16+
17+
No objects will be created, but exceptions will still be thrown.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
24+
from google.ads.google_ads.client import GoogleAdsClient
25+
from google.ads.google_ads.errors import GoogleAdsException
26+
27+
28+
def main(client, customer_id, ad_group_id):
29+
ad_group_ad_operation = client.get_type('AdGroupAdOperation', version='v2')
30+
ad_group_ad = ad_group_ad_operation.create
31+
ad_group_service = client.get_service('AdGroupService', version='v2')
32+
ad_group_ad.ad_group.value = ad_group_service.ad_group_path(customer_id,
33+
ad_group_id)
34+
ad_group_ad.status = client.get_type('AdGroupAdStatusEnum',
35+
version='v2').PAUSED
36+
37+
# Create an expanded text ad.
38+
ad_group_ad.ad.expanded_text_ad.description.value = 'Luxury Cruise to Mars'
39+
ad_group_ad.ad.expanded_text_ad.headline_part1.value = (
40+
'Visit the Red Planet in style.')
41+
ad_group_ad.ad.expanded_text_ad.headline_part2.value = (
42+
'Low-gravity fun for everyone!!')
43+
final_url = ad_group_ad.ad.final_urls.add()
44+
final_url.value = 'http://www.example.com/'
45+
46+
ad_group_ad_service = client.get_service('AdGroupAdService', version='v2')
47+
# Attempt the mutate with validate_only=True.
48+
try:
49+
response = ad_group_ad_service.mutate_ad_group_ads(customer_id,
50+
[ad_group_ad_operation], partial_failure=False, validate_only=True)
51+
print('"Expanded text ad validated successfully.')
52+
except GoogleAdsException as ex:
53+
# This will be hit if there is a validation error from the server.
54+
print(f'Request with ID "{ex.request_id}" failed with status '
55+
f'"{ex.error.code().name}".')
56+
print('There may have been validation error(s) while adding expanded '
57+
'text ad.')
58+
for error in ex.failure.errors:
59+
# Note: Depending on the ad type, you may get back policy violation
60+
# errors as either PolicyFindingError or PolicyViolationError.
61+
# ExpandedTextAds return errors as PolicyFindingError, so only this
62+
# case is illustrated here. For additional details, see
63+
# https://developers.google.com/google-ads/api/docs/policy-exemption/overview
64+
if (error.error_code.policy_finding_error ==
65+
client.get_type('PolicyFindingErrorEnum',
66+
version='v2').POLICY_FINDING):
67+
if error.details.policy_finding_details:
68+
count = 1
69+
details = (error.details.policy_finding_details
70+
.policy_topic_entries)
71+
for entry in details:
72+
print(f'{count}) Policy topic entry: \n{entry}\n')
73+
count += 1
74+
else:
75+
print(f'\tNon-policy finding error with message '
76+
f'"{error.message}".')
77+
if error.location:
78+
for field_path_element in (
79+
error.location.field_path_elements):
80+
print(f'\t\tOn field: {field_path_element.field_name}')
81+
sys.exit(1)
82+
83+
if __name__ == '__main__':
84+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
85+
# home directory if none is specified.
86+
google_ads_client = GoogleAdsClient.load_from_storage()
87+
88+
parser = argparse.ArgumentParser(
89+
description='Shows how to use the ValidateOnly header.')
90+
91+
# The following argument(s) should be provided to run the example.
92+
parser.add_argument('-c', '--customer_id', type=str,
93+
required=True, help='The Google Ads customer ID.')
94+
parser.add_argument('-a', '--ad_group_id', type=str,
95+
required=True, help='The Ad Group ID.')
96+
args = parser.parse_args()
97+
98+
main(google_ads_client, args.customer_id, args.ad_group_id)

0 commit comments

Comments
 (0)