Skip to content

Commit 84f2366

Browse files
Added create_feed_item_set example
1 parent d342b72 commit 84f2366

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 = "Feed Item Set #%s" % 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+
"""
60+
dynamic_location_set_filter = feed_item_set.dynamic_location_set_filter
61+
business_name_filter = dynamic_location_set_filter.business_name_filter
62+
business_name_filter.business_name = 'INSERT_YOUR_BUSINESS_NAME_HERE'
63+
business_name_filter.filter_type = client.get_type(
64+
"FeedItemSetStringFilterTypeEnum", version="v6"
65+
).EXACT
66+
"""
67+
"""
68+
# 2) Affiliate Extension
69+
dynamic_affiliate_location_set_filter = feed_item_set.dynamic_affiliate_location_set_filter
70+
dynamic_affiliate_location_set_filter.chain_ids.extend([INSERT_CHAIN_IDS_HERE])
71+
"""
72+
73+
try:
74+
response = feed_item_set_service.mutate_feed_item_sets(
75+
customer_id, [feed_item_set_operation]
76+
)
77+
print(
78+
"Created a feed item set with resource name: "
79+
f"'{response.results[0].resource_name}'"
80+
)
81+
except GoogleAdsException as ex:
82+
print(
83+
f'Request with ID "{ex.request_id}" failed with status '
84+
f'"{ex.error.code().name}" and includes the following errors:'
85+
)
86+
for error in ex.failure.errors:
87+
print(f'\tError with message "{error.message}".')
88+
if error.location:
89+
for field_path_element in error.location.field_path_elements:
90+
print(f"\t\tOn field: {field_path_element.field_name}")
91+
sys.exit(1)
92+
93+
94+
if __name__ == "__main__":
95+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
96+
# home directory if none is specified.
97+
google_ads_client = GoogleAdsClient.load_from_storage()
98+
99+
parser = argparse.ArgumentParser(
100+
description="Creates a new feed item set for a specified feed."
101+
)
102+
# The following argument(s) should be provided to run the example.
103+
parser.add_argument(
104+
"-c",
105+
"--customer_id",
106+
type=str,
107+
required=True,
108+
help="The Google Ads customer ID.",
109+
)
110+
parser.add_argument(
111+
"-f", "--feed_id", type=str, required=True, help="The feed ID.",
112+
)
113+
args = parser.parse_args()
114+
115+
main(
116+
google_ads_client, args.customer_id, args.feed_id,
117+
)

0 commit comments

Comments
 (0)