-
Notifications
You must be signed in to change notification settings - Fork 477
/
add_hotel_listing_group_tree.py
executable file
·515 lines (446 loc) · 17.8 KB
/
add_hotel_listing_group_tree.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/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.
"""Shows how to add a hotel listing group tree with two levels.
The first level is partitioned by the hotel class. The second level is
partitioned by the country region.
Each level is composed of two types of nodes: UNIT and SUBDIVISION.
UNIT nodes serve as a leaf node in a tree and can have bid amount set.
SUBDIVISION nodes serve as an internal node where a subtree will be built. The
SUBDIVISION node can't have bid amount set.
See https://developers.google.com/google-ads/api/docs/hotel-ads/create-listing-groups
for more information.
Note: Only one listing group tree can be added. Attempting to add another
listing group tree to an ad group that already has one will fail.
"""
import argparse
import sys
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
# The next temporary criterion ID to be used, which is a negative integer.
#
# When creating a tree, we need to specify the parent-child relationships
# between nodes. However, until a criterion has been created on the server we do
# not have a criterion ID with which to refer to it.
#
# Instead, we can specify temporary IDs that are specific to a single mutate
# request. Once a criterion is created, it is assigned an ID as normal and the
# temporary ID will no longer refer to it.
#
# See https://developers.google.com/google-ads/api/docs/mutating/best-practices
# for further details.
next_temp_id = -1
def main(client, customer_id, ad_group_id, percent_cpc_bid_micro_amount):
"""Shows how to add a hotel listing group tree with two levels.
Args:
client: An initialized GoogleAdsClient instance.
customer_id: The client customer ID string.
ad_group_id: The ad group ID for which to the hotel listing will be
added.
percent_cpc_bid_micro_amount: The CPC bid micro amount to be set on a
created ad group criterion.
"""
# Get the AdGroupCriterionService client.
ad_group_criterion_service = client.get_service("AdGroupCriterionService")
operations = []
# Creates the root of the tree as a SUBDIVISION node.
root_resource_name = _add_root_node(
client,
customer_id,
ad_group_id,
operations,
percent_cpc_bid_micro_amount,
)
# Creates child nodes of level 1, partitioned by the hotel class info.
other_hotel_resource_name = _add_level1_nodes(
client,
customer_id,
ad_group_id,
root_resource_name,
operations,
percent_cpc_bid_micro_amount,
)
# Creates child nodes of level 2, partitioned by the hotel country
# region info.
_add_level2_nodes(
client,
customer_id,
ad_group_id,
other_hotel_resource_name,
operations,
percent_cpc_bid_micro_amount,
)
# Adds the listing group and prints the resulting node resource names.
mutate_ad_group_criteria_response = ad_group_criterion_service.mutate_ad_group_criteria(
customer_id=customer_id, operations=operations
)
results = mutate_ad_group_criteria_response.results
print(
f"Added {len(results)} listing group info entities with resource "
"names:"
)
for ad_group_criterion_result in mutate_ad_group_criteria_response.results:
print(f"\t'{ad_group_criterion_result.resource_name}'")
def _add_root_node(
client, customer_id, ad_group_id, operations, percent_cpc_bid_micro_amount
):
"""Creates the root node of the listing group tree.
Also adds its create operation to the operations list.
Args:
client: The Google Ads API client.
customer_id: The Google Ads customer ID.
ad_group_id: The ad group ID to which the hotel listing group will be
added.
operations: A list of AdGroupCriterionOperations.
percent_cpc_bid_micro_amount: The CPC bid micro amount to be set on
created ad group criteria.
Returns:
The string resource name of the root of the tree.
"""
global next_temp_id
# Create the root of the tree as a SUBDIVISION node.
root_listing_group_info = _create_listing_group_info(
client,
client.get_type("ListingGroupTypeEnum").ListingGroupType.SUBDIVISION,
)
root_ad_group_criterion = _create_ad_group_criterion(
client,
customer_id,
ad_group_id,
root_listing_group_info,
percent_cpc_bid_micro_amount,
)
# Create an operation and add it to the list of operations.
root_ad_group_criterion_operation = client.get_type(
"AdGroupCriterionOperation"
)
client.copy_from(
root_ad_group_criterion_operation.create, root_ad_group_criterion
)
operations.append(root_ad_group_criterion_operation)
# Decrement the temp ID for the next ad group criterion.
next_temp_id -= 1
return root_ad_group_criterion.resource_name
# [START add_hotel_listing_group_tree]
def _add_level1_nodes(
client,
customer_id,
ad_group_id,
root_resource_name,
operations,
percent_cpc_bid_micro_amount,
):
"""Creates child nodes on level 1, partitioned by the hotel class info.
Args:
client: The Google Ads API client.
customer_id: The Google Ads customer ID.
ad_group_id: The ad group ID to which the hotel listing group will be
added.
root_resource_name: The string resource name of the listing group's root
node.
operations: A list of AdGroupCriterionOperations.
percent_cpc_bid_micro_amount: The CPC bid micro amount to be set on
created ad group criteria.
Returns:
The string resource name of the "other hotel classes" node, which serves
as the parent node for the next level of the listing tree.
"""
global next_temp_id
# Create listing dimension info for 5-star class hotels.
five_starred_listing_dimension_info = client.get_type(
"ListingDimensionInfo"
)
five_starred_listing_dimension_info.hotel_class.value = 5
# Create a listing group info for 5-star hotels as a UNIT node.
five_starred_unit = _create_listing_group_info(
client,
client.get_type("ListingGroupTypeEnum").ListingGroupType.UNIT,
root_resource_name,
five_starred_listing_dimension_info,
)
# Create an ad group criterion for 5-star hotels.
five_starred_ad_group_criterion = _create_ad_group_criterion(
client,
customer_id,
ad_group_id,
five_starred_unit,
percent_cpc_bid_micro_amount,
)
# Create an operation and add it to the list of operations.
five_starred_ad_group_criterion_operation = client.get_type(
"AdGroupCriterionOperation"
)
client.copy_from(
five_starred_ad_group_criterion_operation.create,
five_starred_ad_group_criterion,
)
operations.append(five_starred_ad_group_criterion_operation)
# Decrement the temp ID for the next ad group criterion.
next_temp_id -= 1
# You can also create more UNIT nodes for other hotel classes by copying the
# above code in this method and modifying the hotel class value.
# For instance, passing 4 instead of 5 in the above code will instead create
# a UNIT node of 4-star hotels.
# Create hotel class info and dimension info without any specifying
# attributes. This node will then represent hotel classes other than those
# already covered by UNIT nodes at this level.
other_hotels_listing_dimension_info = client.get_type(
"ListingDimensionInfo"
)
# Set "hotel_class" as the oneof field on the ListingDimentionInfo object
# without specifying the optional hotel_class field.
client.copy_from(
other_hotels_listing_dimension_info.hotel_class,
client.get_type("HotelClassInfo"),
)
# Create listing group info for other hotel classes as a SUBDIVISION node,
# which will be used as a parent node for children nodes of the next level.
other_hotels_subdivision_listing_group_info = _create_listing_group_info(
client,
client.get_type("ListingGroupTypeEnum").ListingGroupType.SUBDIVISION,
root_resource_name,
other_hotels_listing_dimension_info,
)
# Create an ad group criterion for other hotel classes.
other_hotels_ad_group_criterion = _create_ad_group_criterion(
client,
customer_id,
ad_group_id,
other_hotels_subdivision_listing_group_info,
percent_cpc_bid_micro_amount,
)
# Create an operation and add it to the list of operations.
other_hotels_ad_group_criterion_operation = client.get_type(
"AdGroupCriterionOperation"
)
client.copy_from(
other_hotels_ad_group_criterion_operation.create,
other_hotels_ad_group_criterion,
)
operations.append(other_hotels_ad_group_criterion_operation)
# Decrement the temp ID for the next ad group criterion.
next_temp_id -= 1
return other_hotels_ad_group_criterion.resource_name
# [END add_hotel_listing_group_tree]
def _add_level2_nodes(
client,
customer_id,
ad_group_id,
parent_resource_name,
operations,
percent_cpc_bid_micro_amount,
):
"""Creates child nodes on level 2, partitioned by the country region.
Args:
client: The Google Ads API client.
customer_id: The Google Ads customer ID.
ad_group_id: The ad group ID to which the hotel listing group will be
added.
parent_resource_name: The string resource name of the parent criterion
for the nodes to be added at this level.
operations: A list of AdGroupCriterionOperations.
percent_cpc_bid_micro_amount: The CPC bid micro amount to be set on
created ad group criteria.
Returns:
The string resource name of the "other hotel classes" node, which serves
as the parent node for the next level of the listing tree.
"""
global next_temp_id
# Create hotel dimension info for hotels in Japan. The criterion ID for
# Japan is 2392. See:
# https://developers.google.com/google-ads/api/reference/data/geotargets for
# criteria ID of other countries.
japan_listing_dimension_info = client.get_type("ListingDimensionInfo")
japan_listing_dimension_info.hotel_country_region.country_region_criterion = client.get_service(
"GeoTargetConstantService"
).geo_target_constant_path(
2392
)
# Create listing group info for hotels in Japan as a UNIT node.
japan_hotels_unit = _create_listing_group_info(
client,
client.get_type("ListingGroupTypeEnum").ListingGroupType.UNIT,
parent_resource_name,
japan_listing_dimension_info,
)
# Create an ad group criterion for hotels in Japan.
japan_hotels_ad_group_criterion = _create_ad_group_criterion(
client,
customer_id,
ad_group_id,
japan_hotels_unit,
percent_cpc_bid_micro_amount,
)
# Create an operation and add it to the list of operations.
japan_hotels_ad_group_criterion_operation = client.get_type(
"AdGroupCriterionOperation"
)
client.copy_from(
japan_hotels_ad_group_criterion_operation.create,
japan_hotels_ad_group_criterion,
)
operations.append(japan_hotels_ad_group_criterion_operation)
# Decrement the temp ID for the next ad group criterion.
next_temp_id -= 1
# Create hotel class info and dimension info for hotels in other regions.
other_hotel_regions_listing_dimension_info = client.get_type(
"ListingDimensionInfo"
)
# Set "hotel_country_region" as the oneof field on the ListingDimensionInfo
# object without specifying the optional
# hotel_country_region.country_region_criterion field.
client.copy_from(
other_hotel_regions_listing_dimension_info.hotel_country_region,
client.get_type("HotelCountryRegionInfo"),
)
# Create listing group info for hotels in other regions as a UNIT node.
# The "others" node is always required for every level of the tree.
other_hotel_regions_unit = _create_listing_group_info(
client,
client.get_type("ListingGroupTypeEnum").ListingGroupType.UNIT,
parent_resource_name,
other_hotel_regions_listing_dimension_info,
)
# Create an ad group criterion for other hotel country regions.
other_hotel_regions_ad_group_criterion = _create_ad_group_criterion(
client,
customer_id,
ad_group_id,
other_hotel_regions_unit,
percent_cpc_bid_micro_amount,
)
# Create an operation and add it to the list of operations.
other_hotel_regions_ad_group_criterion_operation = client.get_type(
"AdGroupCriterionOperation"
)
client.copy_from(
other_hotel_regions_ad_group_criterion_operation.create,
other_hotel_regions_ad_group_criterion,
)
operations.append(other_hotel_regions_ad_group_criterion_operation)
# Decrement the temp ID for the next ad group criterion.
next_temp_id -= 1
def _create_listing_group_info(
client,
listing_group_type,
parent_criterion_resource_name=None,
case_value=None,
):
"""Creates the listing group info with the provided parameters.
Args:
client: The Google Ads API client.
listing_group_type: The listing group type.
parent_criterion_resource_name: Optional resource name of the parent
criterion ID to set for this listing group info.
case_value: Optional dimension info for the listing group.
Returns:
A populated ListingGroupInfo object.
"""
listing_group_info = client.get_type("ListingGroupInfo")
listing_group_info.type_ = listing_group_type
if parent_criterion_resource_name is not None and case_value is not None:
listing_group_info.parent_ad_group_criterion = (
parent_criterion_resource_name
)
client.copy_from(listing_group_info.case_value, case_value)
return listing_group_info
def _create_ad_group_criterion(
client,
customer_id,
ad_group_id,
listing_group_info,
percent_cpc_bid_micro_amount,
):
"""Creates an ad group criterion from the provided listing group info.
Bid amount will be set on the created ad group criterion when listing group
info type is UNIT. Setting bid amount for SUBDIVISION types is not
allowed.
Args:
client: The Google Ads API client.
customer_id: The Google Ads customer ID.
ad_group_id: The ad group ID to which the hotel listing group will be
added.
listing_group_info: The listing group info to apply to the criterion.
percent_cpc_bid_micro_amount: The CPC bid micro amount to be set on
created ad group criteria.
Returns:
A populated AdGroupCriterion object.
"""
ad_group_criterion = client.get_type("AdGroupCriterion")
ad_group_criterion.status = client.get_type(
"AdGroupCriterionStatusEnum"
).AdGroupCriterionStatus.ENABLED
client.copy_from(ad_group_criterion.listing_group, listing_group_info)
ad_group_criterion.resource_name = client.get_service(
"AdGroupCriterionService"
).ad_group_criterion_path(customer_id, ad_group_id, next_temp_id)
# Bids are only valid for UNIT nodes.
if (
listing_group_info.type_
== client.get_type("ListingGroupTypeEnum").ListingGroupType.UNIT
):
ad_group_criterion.percent_cpc_bid_micros = percent_cpc_bid_micro_amount
return ad_group_criterion
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="v7")
parser = argparse.ArgumentParser(
description="Shows how to add a hotel listing group tree with two "
"levels."
)
# 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=int,
required=True,
help="The ad group ID to which the hotel listing will be added.",
)
parser.add_argument(
"-p",
"--percent_cpc_bid_micro_amount",
type=int,
required=True,
help="Specify the CPC bid micro amount to be set on a created ad group "
"criterion. For simplicity, each ad group criterion will use the below "
"amount equally. In practice, you probably want to use different "
"values for each ad group criterion.",
)
args = parser.parse_args()
try:
main(
googleads_client,
args.customer_id,
args.ad_group_id,
args.percent_cpc_bid_micro_amount,
)
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)