|
| 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 | +"""Creates a new feed item set for a specified feed. |
| 16 | +
|
| 17 | +The feed must belong to either a Google Ads location extension or an affiliate |
| 18 | +extension. This is equivalent to a "location group" in the Google Ads UI. See |
| 19 | +https://support.google.com/google-ads/answer/9288588 for more detail. |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import sys |
| 24 | +import uuid |
| 25 | + |
| 26 | +from google.ads.google_ads.client import GoogleAdsClient |
| 27 | +from google.ads.google_ads.errors import GoogleAdsException |
| 28 | + |
| 29 | + |
| 30 | +def main(client, customer_id, feed_id): |
| 31 | + """The main method that creates all necessary entities for the example. |
| 32 | +
|
| 33 | + Args: |
| 34 | + client: an initialized GoogleAdsClient instance. |
| 35 | + customer_id: a client customer ID. |
| 36 | + feed_id: the ID for a Feed belonging to the given customer. |
| 37 | + """ |
| 38 | + feed_item_set_service = client.get_service( |
| 39 | + "FeedItemSetService", version="v6" |
| 40 | + ) |
| 41 | + feed_item_set_operation = client.get_type( |
| 42 | + "FeedItemSetOperation", version="v6" |
| 43 | + ) |
| 44 | + |
| 45 | + feed_item_set = feed_item_set_operation.create |
| 46 | + feed_resource_name = client.get_service( |
| 47 | + "FeedService", version="v6" |
| 48 | + ).feed_path(customer_id, feed_id) |
| 49 | + feed_item_set.feed = feed_resource_name |
| 50 | + feed_item_set.display_name = f"Feed Item Set #{uuid.uuid4()}" |
| 51 | + |
| 52 | + # A feed item set can be created as a dynamic set by setting an optional |
| 53 | + # filter field below. If your feed is a location extension, uncomment the code |
| 54 | + # that sets dynamic_location_set_filter. If your feed is an affiliate |
| 55 | + # extension, set dynamic_affiliate_location_set_filter instead. |
| 56 | + |
| 57 | + # 1) Location Extension |
| 58 | + # NOTE: Does not support Curated location extensions. |
| 59 | + # dynamic_location_set_filter = feed_item_set.dynamic_location_set_filter |
| 60 | + # business_name_filter = dynamic_location_set_filter.business_name_filter |
| 61 | + # business_name_filter.business_name = 'INSERT_YOUR_BUSINESS_NAME_HERE' |
| 62 | + # business_name_filter.filter_type = client.get_type( |
| 63 | + # "FeedItemSetStringFilterTypeEnum", version="v6" |
| 64 | + # ).EXACT |
| 65 | + |
| 66 | + # 2) Affiliate Extension |
| 67 | + # dynamic_affiliate_location_set_filter = feed_item_set.dynamic_affiliate_location_set_filter |
| 68 | + # dynamic_affiliate_location_set_filter.chain_ids.extend([INSERT_CHAIN_IDS_HERE]) |
| 69 | + |
| 70 | + try: |
| 71 | + response = feed_item_set_service.mutate_feed_item_sets( |
| 72 | + customer_id, [feed_item_set_operation] |
| 73 | + ) |
| 74 | + print( |
| 75 | + "Created a feed item set with resource name: " |
| 76 | + f"'{response.results[0].resource_name}'" |
| 77 | + ) |
| 78 | + except GoogleAdsException as ex: |
| 79 | + print( |
| 80 | + f'Request with ID "{ex.request_id}" failed with status ' |
| 81 | + f'"{ex.error.code().name}" and includes the following errors:' |
| 82 | + ) |
| 83 | + for error in ex.failure.errors: |
| 84 | + print(f'\tError with message "{error.message}".') |
| 85 | + if error.location: |
| 86 | + for field_path_element in error.location.field_path_elements: |
| 87 | + print(f"\t\tOn field: {field_path_element.field_name}") |
| 88 | + sys.exit(1) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 93 | + # home directory if none is specified. |
| 94 | + google_ads_client = GoogleAdsClient.load_from_storage() |
| 95 | + |
| 96 | + parser = argparse.ArgumentParser( |
| 97 | + description="Creates a new feed item set for a specified feed." |
| 98 | + ) |
| 99 | + # The following argument(s) should be provided to run the example. |
| 100 | + parser.add_argument( |
| 101 | + "-c", |
| 102 | + "--customer_id", |
| 103 | + type=str, |
| 104 | + required=True, |
| 105 | + help="The Google Ads customer ID.", |
| 106 | + ) |
| 107 | + parser.add_argument( |
| 108 | + "-f", |
| 109 | + "--feed_id", |
| 110 | + type=str, |
| 111 | + required=True, |
| 112 | + help="The feed ID.", |
| 113 | + ) |
| 114 | + args = parser.parse_args() |
| 115 | + |
| 116 | + main( |
| 117 | + google_ads_client, args.customer_id, args.feed_id, |
| 118 | + ) |
0 commit comments