-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathcreate_asset_optimization_experiment.py
More file actions
319 lines (285 loc) · 10.7 KB
/
create_asset_optimization_experiment.py
File metadata and controls
319 lines (285 loc) · 10.7 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Copyright 2026 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 shows how to create an OPTIMIZE_ASSETS experiment.
Asset optimization experiments are used to test different asset combinations
within Performance Max campaigns.
"""
import argparse
import sys
from typing import List, Tuple, Any
from uuid import uuid4
from examples.utils.example_helpers import get_image_bytes_from_url
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
from google.ads.googleads.v24.services.types.google_ads_service import (
MutateOperation,
)
def main(
client: GoogleAdsClient, customer_id: str, asset_group_id: str
) -> None:
"""Creates an OPTIMIZE_ASSETS experiment.
Args:
client: An initialized GoogleAdsClient instance.
customer_id: The Google Ads customer ID.
asset_group_id: The base asset group ID to run the experiment on.
"""
googleads_service = client.get_service("GoogleAdsService")
# Query the asset group to find the associated campaign resource name.
query = f"""
SELECT asset_group.campaign
FROM asset_group
WHERE asset_group.id = {asset_group_id}
"""
search_response = googleads_service.search(
customer_id=customer_id, query=query
)
if len(search_response):
campaign_resource_name = search_response[0].asset_group.campaign
else:
print(f"Asset group with ID {asset_group_id} not found.")
sys.exit(1)
# Temp IDs
ASSET_1_TEMP_ID = "-1"
EXPERIMENT_TEMP_ID = "-2"
ASSET_2_TEMP_ID = "-3"
# [START create_asset_optimization_experiment_1]
# 1. Create Assets with temporary resource names.
# We create a text asset and an image asset to showcase different types.
asset_operation_1 = create_text_asset_operation(
client,
customer_id,
ASSET_1_TEMP_ID,
"Fly to Mars!",
)
asset_operation_2 = create_image_asset_operation(
client,
customer_id,
ASSET_2_TEMP_ID,
"https://gaagl.page.link/Eit5",
"Mars Landscape View",
)
# 2. Create an Experiment with a temporary resource name.
experiment_operation = client.get_type("MutateOperation")
experiment = experiment_operation.experiment_operation.create
experiment.resource_name = googleads_service.experiment_path(
customer_id, EXPERIMENT_TEMP_ID
)
experiment.name = f"Interstellar Asset Experiment #{uuid4()}"
experiment.type_ = client.enums.ExperimentTypeEnum.OPTIMIZE_ASSETS
# Set the optimize assets experiment subtype to COMPARE_ASSETS.
experiment.optimize_assets_experiment.optimize_assets_experiment_subtype = (
client.enums.OptimizeAssetsExperimentSubtypeEnum.COMPARE_ASSETS
)
# 3. Create two ExperimentArm resources.
treatment_assets = [
(ASSET_1_TEMP_ID, client.enums.AssetFieldTypeEnum.HEADLINE),
(ASSET_2_TEMP_ID, client.enums.AssetFieldTypeEnum.MARKETING_IMAGE),
]
arm_operations = create_arms_operations(
client,
customer_id,
EXPERIMENT_TEMP_ID,
campaign_resource_name,
asset_group_id,
treatment_assets,
)
# 4. Create AssetGroupAssets linking the assets to the asset group.
asset_group_asset_operation_1 = create_asset_group_asset_operation(
client,
customer_id,
asset_group_id,
ASSET_1_TEMP_ID,
client.enums.AssetFieldTypeEnum.HEADLINE,
)
asset_group_asset_operation_2 = create_asset_group_asset_operation(
client,
customer_id,
asset_group_id,
ASSET_2_TEMP_ID,
client.enums.AssetFieldTypeEnum.MARKETING_IMAGE,
)
# Send all operations in a single Mutate request.
# The operations must be in this specific order.
mutate_operations = [
asset_operation_1,
asset_operation_2,
experiment_operation,
*arm_operations,
asset_group_asset_operation_1,
asset_group_asset_operation_2,
]
response = googleads_service.mutate(
customer_id=customer_id,
mutate_operations=mutate_operations,
)
# [END create_asset_optimization_experiment_1]
# Print the results.
print(
"Created headline asset:"
f" {response.mutate_operation_responses[0].asset_result.resource_name}"
)
print(
"Created image asset:"
f" {response.mutate_operation_responses[1].asset_result.resource_name}"
)
print(
"Created experiment:"
f" {response.mutate_operation_responses[2].experiment_result.resource_name}"
)
print(
"Created control arm:"
f" {response.mutate_operation_responses[3].experiment_arm_result.resource_name}"
)
print(
"Created treatment arm:"
f" {response.mutate_operation_responses[4].experiment_arm_result.resource_name}"
)
print(
"Created asset group asset for headline:"
f" {response.mutate_operation_responses[5].asset_group_asset_result.resource_name}"
)
print(
"Created asset group asset for image:"
f" {response.mutate_operation_responses[6].asset_group_asset_result.resource_name}"
)
def create_text_asset_operation(
client: GoogleAdsClient, customer_id: str, temp_id: str, text: str
) -> MutateOperation:
"""Creates a mutate operation for a text asset."""
googleads_service = client.get_service("GoogleAdsService")
operation = client.get_type("MutateOperation")
asset = operation.asset_operation.create
asset.resource_name = googleads_service.asset_path(customer_id, temp_id)
asset.text_asset.text = text
return operation
def create_image_asset_operation(
client: GoogleAdsClient,
customer_id: str,
temp_id: str,
url: str,
name: str,
) -> MutateOperation:
"""Creates a mutate operation for an image asset."""
googleads_service = client.get_service("GoogleAdsService")
operation = client.get_type("MutateOperation")
asset = operation.asset_operation.create
asset.resource_name = googleads_service.asset_path(customer_id, temp_id)
asset.name = name
asset.type_ = client.enums.AssetTypeEnum.IMAGE
asset.image_asset.data = get_image_bytes_from_url(url)
return operation
def create_arms_operations(
client: GoogleAdsClient,
customer_id: str,
experiment_temp_id: str,
campaign_resource_name: str,
asset_group_id: str,
treatment_assets: List[Tuple[str, Any]],
) -> List[MutateOperation]:
"""Creates mutate operations for control and treatment arms."""
googleads_service = client.get_service("GoogleAdsService")
experiment_arm_type = client.get_type("ExperimentArm")
operations = []
# Control arm
control_operation = client.get_type("MutateOperation")
control = control_operation.experiment_arm_operation.create
control.experiment = googleads_service.experiment_path(
customer_id, experiment_temp_id
)
control.name = "Base Assets (Control)"
control.control = True
control.traffic_split = 50
control.campaigns.append(campaign_resource_name)
asset_group_info_control = experiment_arm_type.AssetGroupInfo()
asset_group_info_control.asset_group = googleads_service.asset_group_path(
customer_id, asset_group_id
)
control.asset_groups.append(asset_group_info_control)
operations.append(control_operation)
# Treatment arm
treatment_operation = client.get_type("MutateOperation")
treatment = treatment_operation.experiment_arm_operation.create
treatment.experiment = googleads_service.experiment_path(
customer_id, experiment_temp_id
)
treatment.name = "New Assets (Treatment)"
treatment.control = False
treatment.traffic_split = 50
treatment.campaigns.append(campaign_resource_name)
asset_group_info_treatment = experiment_arm_type.AssetGroupInfo()
asset_group_info_treatment.asset_group = googleads_service.asset_group_path(
customer_id, asset_group_id
)
for asset_temp_id, field_type in treatment_assets:
asset_group_asset_info = experiment_arm_type.AssetGroupAssetInfo()
asset_group_asset_info.asset = googleads_service.asset_path(
customer_id, asset_temp_id
)
asset_group_asset_info.field_type = field_type
asset_group_info_treatment.asset_group_assets.append(
asset_group_asset_info
)
treatment.asset_groups.append(asset_group_info_treatment)
operations.append(treatment_operation)
return operations
def create_asset_group_asset_operation(
client: GoogleAdsClient,
customer_id: str,
asset_group_id: str,
asset_temp_id: str,
field_type: Any,
) -> MutateOperation:
"""Creates a mutate operation for an asset group asset."""
googleads_service = client.get_service("GoogleAdsService")
operation = client.get_type("MutateOperation")
aga = operation.asset_group_asset_operation.create
aga.asset_group = googleads_service.asset_group_path(
customer_id, asset_group_id
)
aga.asset = googleads_service.asset_path(customer_id, asset_temp_id)
aga.field_type = field_type
return operation
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Creates an OPTIMIZE_ASSETS experiment."
)
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-a",
"--asset_group_id",
type=str,
required=True,
help="The base asset group ID to run the experiment on.",
)
args = parser.parse_args()
googleads_client = GoogleAdsClient.load_from_storage(version="v24")
try:
main(googleads_client, args.customer_id, args.asset_group_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)