# [START add_shopping_product_listing_group_tree]
def create_listing_group(client, customer_id, ad_group_id, replace_existing_tree):
"""Adds a shopping listing group tree to a shopping ad group.
Args:
client: An initialized Google Ads client.
customer_id: The Google Ads customer ID.
ad_group_id: The ad group ID to which the node will be added.
replace_existing_tree: Boolean, whether to replace the existing listing
group tree on the ad group. Defaults to false.
"""
# Get the AdGroupCriterionService client.
ad_group_criterion_service = client.get_service("AdGroupCriterionService")
# Optional: Remove the existing listing group tree, if it already exists
# on the ad group. The example will throw a LISTING_GROUP_ALREADY_EXISTS
# error if a listing group tree already exists and this option is not
# set to true.
if replace_existing_tree:
remove_listing_group_tree(client, customer_id, ad_group_id)
# Create a list of ad group criteria operations.
operations = []
# Construct the listing group tree "root" node.
# Subdivision node: (Root node)
ad_group_criterion_root_operation = create_listing_group_subdivision(
client, customer_id, ad_group_id
)
# Get the resource name that will be used for the root node.
# This resource has not been created yet and will include the temporary
# ID as part of the criterion ID.
ad_group_criterion_root_resource_name = (
ad_group_criterion_root_operation.create.resource_name
)
operations.append(ad_group_criterion_root_operation)
# Construct the listing group unit nodes for NEW, USED, and other.
condition_dimension_info = client.get_type("ListingDimensionInfo")
enum = client.enums.ListingGroupFilterCustomAttributeIndexEnum
pca_1 = client.get_type("ProductCustomAttributeInfo")
pca_1.index = enum.INDEX1
pca_1.value = "test1"
pca_2 = client.get_type("ProductCustomAttributeInfo")
pca_2.index = enum.INDEX2
pca_2.value = "test2"
condition_dimension_info.product_custom_attribute.CopyFrom(pca_1)
operations.append(
create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_root_resource_name,
condition_dimension_info,
200_000,
)
)
condition_dimension_info.product_custom_attribute.CopyFrom(pca_2)
operations.append(
create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_root_resource_name,
condition_dimension_info,
200_000,
)
)
client.copy_from(
condition_dimension_info.product_custom_attribute,
client.get_type("ProductCustomAttributeInfo"),
)
operations.append(
create_listing_group_unit_biddable(
client, customer_id, ad_group_id, ad_group_criterion_root_resource_name, condition_dimension_info, 100_000
)
)
# Add the ad group criteria.
mutate_ad_group_criteria_response = (
ad_group_criterion_service.mutate_ad_group_criteria(
customer_id=customer_id, operations=operations
)
)
# Print the results of the successful mutates.
print(
"Added ad group criteria for the listing group tree with the "
"following resource names:"
)
for result in mutate_ad_group_criteria_response.results:
print(f"\t{result.resource_name}")
print(f"{len(mutate_ad_group_criteria_response.results)} criteria added.")
# [END add_shopping_product_listing_group_tree]