-
Notifications
You must be signed in to change notification settings - Fork 475
/
add_prices.py
214 lines (185 loc) · 7.24 KB
/
add_prices.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
#!/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.
"""This example adds a price asset and associates it with an account."""
import argparse
import sys
from uuid import uuid4
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
def main(client, customer_id):
"""The main method that creates all necessary entities for the example.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
"""
# Create a new price asset.
price_asset_resource_name = create_price_asset(client, customer_id)
# Add the new price asset to the account.
add_asset_to_account(client, customer_id, price_asset_resource_name)
def create_price_asset(client, customer_id):
"""Creates a price asset and returns its resource name.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
Returns:
a PriceAsset resource name.
"""
# Create an asset operation.
asset_operation = client.get_type("AssetOperation")
# Create an asset.
asset = asset_operation.create
asset.name = f"Price Asset #{uuid4()}"
asset.tracking_url_template = "http://tracker.example.com/?u={lpurl}"
# Create the price asset.
price_asset = asset.price_asset
price_asset.type_ = client.enums.PriceExtensionTypeEnum.SERVICES
# Price qualifier is optional.
price_asset.price_qualifier = (
client.enums.PriceExtensionPriceQualifierEnum.FROM
)
price_asset.language_code = "en"
price_asset.price_offerings.extend(
[
create_price_offering(
client,
"Scrubs",
"Body Scrub, Salt Scrub",
"http://www.example.com/scrubs",
"http://m.example.com/scrubs",
60000000, # 60 USD
"USD",
client.enums.PriceExtensionPriceUnitEnum.PER_HOUR,
),
create_price_offering(
client,
"Hair Cuts",
"Once a month",
"http://www.example.com/haircuts",
"http://m.example.com/haircuts",
75000000, # 75 USD
"USD",
client.enums.PriceExtensionPriceUnitEnum.PER_MONTH,
),
create_price_offering(
client,
"Skin Care Package",
"Four times a month",
"http://www.example.com/skincarepackage",
None,
250000000, # 250 USD
"USD",
client.enums.PriceExtensionPriceUnitEnum.PER_MONTH,
),
]
)
# Issue a mutate request to create the price asset.
asset_service = client.get_service("AssetService")
response = asset_service.mutate_assets(
customer_id=customer_id, operations=[asset_operation]
)
resource_name = response.results[0].resource_name
print(f"Created a price asset with resource name '{resource_name}'.")
return resource_name
def create_price_offering(
client,
header,
description,
final_url,
final_mobile_url,
price_in_micros,
currency_code,
unit,
):
"""Creates a PriceOffering instance and returns it.
Args:
client: an initialized GoogleAdsClient instance.
header: The header of the price offering.
description: The description of the price offering.
final_url: The final_url of the price offering.
final_mobile_url: The final_mobile_url of the price offering.
price_in_micros: The price of the price offering.
currency_code: The currency_code of the price offering.
unit: The price unit of the price offering.
Returns:
A PriceOffering instance.
"""
price_offering = client.get_type("PriceOffering")
price_offering.header = header
price_offering.description = description
price_offering.final_url = final_url
# Check if this exists, since we pass None for one of the PriceOfferings
# in the _create_price_asset method and assigning None to this field
# raises an error.
if final_mobile_url:
price_offering.final_mobile_url = final_mobile_url
price_offering.price.amount_micros = price_in_micros
price_offering.price.currency_code = currency_code
price_offering.unit = unit
return price_offering
def add_asset_to_account(client, customer_id, price_asset_resource_name):
"""Adds a new Asset to the given user account.
Adding the Asset to an account allows it to serve in all campaigns under
that account.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
price_asset_resource_name: a resource name for an Asset containing
a PriceAsset.
"""
# Create a customer asset operation.
customer_asset_operation = client.get_type("CustomerAssetOperation")
# Create a customer asset, set its type to PRICE and attach price asset.
asset = customer_asset_operation.create
asset.field_type = client.enums.AssetFieldTypeEnum.PRICE
asset.asset = price_asset_resource_name
# Issue a mutate request to create the customer asset.
customer_asset_service = client.get_service("CustomerAssetService")
response = customer_asset_service.mutate_customer_assets(
customer_id=customer_id, operations=[customer_asset_operation]
)
resource_name = response.results[0].resource_name
print(
"Created customer asset with resource name "
f"'{response.results[0].resource_name}'."
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Add price asset for the specified customer id."
)
# 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",
)
args = parser.parse_args()
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(version="v17")
try:
main(googleads_client, args.customer_id)
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)