Skip to content

Commit 43b3144

Browse files
githaruoBenRKarl
authored andcommitted
Add an ad group asset in advanced_operations.
1 parent 1d01a80 commit 43b3144

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
# Copyright 2018 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 adds an ad group asset.
16+
17+
To upload image assets for this campaign, run misc/upload_image_asset.py.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
24+
from google.ads.googleads.client import GoogleAdsClient
25+
from google.ads.googleads.errors import GoogleAdsException
26+
from google.api_core import protobuf_helpers
27+
28+
29+
def main(client, customer_id, ad_group_id, asset_id):
30+
ad_group_asset_service = client.get_service("AdGroupAssetService")
31+
ad_group_asset_resource_name = ad_group_asset_service.asset_path(
32+
customer_id, asset_id
33+
)
34+
35+
ad_group_operation = client.get_type("AdGroupAssetOperation")
36+
ad_group_asset_set = ad_group_operation.create
37+
ad_group_asset_set.asset = ad_group_asset_resource_name
38+
ad_group_asset_set.field_type = client.enums.AssetFieldTypeEnum.AD_IMAGE
39+
ad_group_asset_set.ad_group = ad_group_asset_service.ad_group_path(
40+
customer_id, ad_group_id
41+
)
42+
response = ad_group_asset_service.mutate_ad_group_assets(
43+
customer_id=customer_id, operations=[ad_group_operation]
44+
)
45+
46+
for result in response.results:
47+
print(
48+
"Created ad group asset with resource name: "
49+
f"'{result.resource_name}'"
50+
)
51+
52+
53+
if __name__ == "__main__":
54+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
55+
# home directory if none is specified.
56+
googleads_client = GoogleAdsClient.load_from_storage(version="v14")
57+
58+
parser = argparse.ArgumentParser(
59+
description=(
60+
"Updates an ad group for specified customer and ad group "
61+
"id with the given asset id."
62+
)
63+
)
64+
# The following argument(s) should be provided to run the example.
65+
parser.add_argument(
66+
"-c",
67+
"--customer_id",
68+
type=str,
69+
required=True,
70+
help="The Google Ads customer ID.",
71+
)
72+
parser.add_argument(
73+
"-a", "--ad_group_id", type=str, required=True, help="The ad group ID."
74+
)
75+
parser.add_argument(
76+
"-s",
77+
"--asset_id",
78+
type=str,
79+
required=True,
80+
help="The asset ID.",
81+
)
82+
83+
try:
84+
main(
85+
googleads_client, args.customer_id, args.ad_group_id, args.asset_id
86+
)
87+
except GoogleAdsException as ex:
88+
print(
89+
f'Request with ID "{ex.request_id}" failed with status '
90+
f'"{ex.error.code().name}" and includes the following errors:'
91+
)
92+
for error in ex.failure.errors:
93+
print(f'\tError with message "{error.message}".')
94+
if error.location:
95+
for field_path_element in error.location.field_path_elements:
96+
print(f"\t\tOn field: {field_path_element.field_name}")
97+
sys.exit(1)

0 commit comments

Comments
 (0)