Skip to content

Commit 5e04e04

Browse files
authored
code example for update expanded text ads (#247)
* code example for update expanded text ads * fixed argument wording * wording tweaks, use f-string * add punctuation * quote around resource name, and end sentence with a period * filename change
1 parent 7c81bc8 commit 5e04e04

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 updates an expanded text ad.
16+
17+
To get expanded text ads, run get_expanded_text_ads.py.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
import uuid
24+
25+
from google.ads.google_ads.client import GoogleAdsClient
26+
from google.ads.google_ads.errors import GoogleAdsException
27+
from google.api_core import protobuf_helpers
28+
29+
30+
def main(client, customer_id, ad_id):
31+
ad_service = client.get_service('AdService', version='v3')
32+
33+
ad_operation = client.get_type('AdOperation', version='v3')
34+
35+
# Update ad operation.
36+
ad = ad_operation.update
37+
ad.resource_name = ad_service.ad_path(customer_id, ad_id)
38+
ad.expanded_text_ad.headline_part1.value = (
39+
f'Cruise to Pluto {str(uuid.uuid4())[:8]}')
40+
ad.expanded_text_ad.headline_part2.value = 'Tickets on sale now'
41+
final_url = ad.final_urls.add()
42+
final_url.value = 'http://www.example.com'
43+
final_mobile_url = ad.final_mobile_urls.add()
44+
final_mobile_url.value = 'http://www.example.com/mobile'
45+
46+
fm = protobuf_helpers.field_mask(None, ad)
47+
ad_operation.update_mask.CopyFrom(fm)
48+
49+
# Updates the ad.
50+
try:
51+
ad_response = ad_service.mutate_ads(customer_id, [ad_operation])
52+
except GoogleAdsException as ex:
53+
print(f'Request with ID "{ex.request_id}" failed with status '
54+
f'"{ex.error.code().name}" and includes the following errors:')
55+
for error in ex.failure.errors:
56+
print(f'\tError with message "{error.message}".')
57+
if error.location:
58+
for field_path_element in error.location.field_path_elements:
59+
print(f'\t\tOn field: {field_path_element.field_name}')
60+
sys.exit(1)
61+
62+
print(f'Ad with resource name "{ad_response.results[0].resource_name}" '
63+
'was updated.')
64+
65+
66+
if __name__ == '__main__':
67+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
68+
# home directory if none is specified.
69+
google_ads_client = GoogleAdsClient.load_from_storage()
70+
71+
parser = argparse.ArgumentParser(
72+
description=('Updates the specified expanded text ad, '
73+
'for the given customer ID.'))
74+
# The following argument(s) should be provided to run the example.
75+
parser.add_argument('-c', '--customer_id', type=str,
76+
required=True, help='The Google Ads customer ID.')
77+
parser.add_argument('-i', '--ad_id', type=str, required=True,
78+
help='The ad ID.')
79+
args = parser.parse_args()
80+
81+
main(google_ads_client, args.customer_id, args.ad_id)

0 commit comments

Comments
 (0)