Skip to content

Commit 5cd2a01

Browse files
Add treatment campaign ID and shorten the asset text.
Change-Id: I39ac87bcd26a9bbb346937a7f42e86668224b97d
1 parent e26e566 commit 5cd2a01

1 file changed

Lines changed: 80 additions & 114 deletions

File tree

examples/experiments/create_asset_optimization_experiment.py

Lines changed: 80 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,6 @@
2525
from examples.utils.example_helpers import get_image_bytes_from_url
2626
from google.ads.googleads.client import GoogleAdsClient
2727
from google.ads.googleads.errors import GoogleAdsException
28-
from google.ads.googleads.v24.services.types.experiment_service import (
29-
ExperimentOperation,
30-
MutateExperimentsResponse,
31-
)
32-
from google.ads.googleads.v24.services.types.experiment_arm_service import (
33-
ExperimentArmOperation,
34-
MutateExperimentArmsRequest,
35-
MutateExperimentArmsResponse,
36-
)
37-
from google.ads.googleads.v24.resources.types.experiment import Experiment
38-
from google.ads.googleads.v24.resources.types.experiment_arm import (
39-
ExperimentArm,
40-
)
41-
from google.ads.googleads.v24.services.services.experiment_service import (
42-
ExperimentServiceClient,
43-
)
44-
from google.ads.googleads.v24.services.services.experiment_arm_service import (
45-
ExperimentArmServiceClient,
46-
)
4728
from google.ads.googleads.v24.services.types.google_ads_service import (
4829
MutateOperation,
4930
)
@@ -79,19 +60,19 @@ def main(
7960
print(f"Asset group with ID {asset_group_id} not found.")
8061
sys.exit(1)
8162

82-
# Temp IDs for asset creation
63+
# Temp IDs
8364
ASSET_1_TEMP_ID = "-1"
84-
ASSET_2_TEMP_ID = "-2"
65+
EXPERIMENT_TEMP_ID = "-2"
66+
ASSET_2_TEMP_ID = "-3"
8567

8668
# [START create_asset_optimization_experiment_1]
87-
# 1. Create Assets.
69+
# 1. Create Assets with temporary resource names.
8870
# We create a text asset and an image asset to showcase different types.
89-
# We execute the asset creation first to get real resource names.
9071
asset_operation_1 = create_text_asset_operation(
9172
client,
9273
customer_id,
9374
ASSET_1_TEMP_ID,
94-
"Fly to Mars with Interplanetary Cruises!",
75+
"Fly to Mars!",
9576
)
9677
asset_operation_2 = create_image_asset_operation(
9778
client,
@@ -101,114 +82,94 @@ def main(
10182
"Mars Landscape View",
10283
)
10384

104-
asset_response = googleads_service.mutate(
105-
customer_id=customer_id,
106-
mutate_operations=[asset_operation_1, asset_operation_2],
107-
)
108-
headline_asset_resource_name = (
109-
asset_response.mutate_operation_responses[0].asset_result.resource_name
110-
)
111-
image_asset_resource_name = (
112-
asset_response.mutate_operation_responses[1].asset_result.resource_name
113-
)
114-
115-
# 2. Create an Experiment using ExperimentService.
116-
experiment_operation: ExperimentOperation = client.get_type(
117-
"ExperimentOperation"
85+
# 2. Create an Experiment with a temporary resource name.
86+
experiment_operation = client.get_type("MutateOperation")
87+
experiment = experiment_operation.experiment_operation.create
88+
experiment.resource_name = googleads_service.experiment_path(
89+
customer_id, EXPERIMENT_TEMP_ID
11890
)
119-
experiment: Experiment = experiment_operation.create
12091
experiment.name = f"Interstellar Asset Experiment #{uuid4()}"
12192
experiment.type_ = client.enums.ExperimentTypeEnum.OPTIMIZE_ASSETS
12293
# Set the optimize assets experiment subtype to COMPARE_ASSETS.
12394
experiment.optimize_assets_experiment.optimize_assets_experiment_subtype = (
12495
client.enums.OptimizeAssetsExperimentSubtypeEnum.COMPARE_ASSETS
12596
)
12697

127-
experiment_service: ExperimentServiceClient = client.get_service(
128-
"ExperimentService"
129-
)
130-
experiment_response: MutateExperimentsResponse = (
131-
experiment_service.mutate_experiments(
132-
customer_id=customer_id, operations=[experiment_operation]
133-
)
134-
)
135-
experiment_resource_name: str = (
136-
experiment_response.results[0].resource_name
137-
)
138-
139-
# 3. Create two ExperimentArm resources using ExperimentArmService.
98+
# 3. Create two ExperimentArm resources.
14099
treatment_assets = [
141-
(headline_asset_resource_name, client.enums.AssetFieldTypeEnum.HEADLINE),
142-
(image_asset_resource_name, client.enums.AssetFieldTypeEnum.MARKETING_IMAGE),
100+
(ASSET_1_TEMP_ID, client.enums.AssetFieldTypeEnum.HEADLINE),
101+
(ASSET_2_TEMP_ID, client.enums.AssetFieldTypeEnum.MARKETING_IMAGE),
143102
]
144103
arm_operations = create_arms_operations(
145104
client,
146105
customer_id,
147-
experiment_resource_name,
106+
EXPERIMENT_TEMP_ID,
148107
campaign_resource_name,
149108
asset_group_id,
150109
treatment_assets,
151110
)
152111

153-
experiment_arm_service: ExperimentArmServiceClient = client.get_service(
154-
"ExperimentArmService"
155-
)
156-
request: MutateExperimentArmsRequest = client.get_type(
157-
"MutateExperimentArmsRequest"
158-
)
159-
request.customer_id = customer_id
160-
request.operations = arm_operations
161-
# We want to fetch the generated asset group IDs from the treatment arm, so the
162-
# easiest way to do that is to have the response return the newly created entities.
163-
request.response_content_type = (
164-
client.enums.ResponseContentTypeEnum.MUTABLE_RESOURCE
165-
)
166-
arm_response: MutateExperimentArmsResponse = (
167-
experiment_arm_service.mutate_experiment_arms(request=request)
168-
)
169-
170-
control_arm_result = arm_response.results[0]
171-
treatment_arm_result = arm_response.results[1]
172-
treatment_asset_group_resource_name = (
173-
treatment_arm_result.experiment_arm.asset_groups[0].asset_group
174-
)
175-
176-
# 4. Create AssetGroupAssets linking the assets to the treatment experiment arm's asset group.
112+
# 4. Create AssetGroupAssets linking the assets to the asset group.
177113
asset_group_asset_operation_1 = create_asset_group_asset_operation(
178114
client,
179-
treatment_asset_group_resource_name,
180-
headline_asset_resource_name,
115+
customer_id,
116+
asset_group_id,
117+
ASSET_1_TEMP_ID,
181118
client.enums.AssetFieldTypeEnum.HEADLINE,
182119
)
183120
asset_group_asset_operation_2 = create_asset_group_asset_operation(
184121
client,
185-
treatment_asset_group_resource_name,
186-
image_asset_resource_name,
122+
customer_id,
123+
asset_group_id,
124+
ASSET_2_TEMP_ID,
187125
client.enums.AssetFieldTypeEnum.MARKETING_IMAGE,
188126
)
189127

190-
aga_response = googleads_service.mutate(
128+
# Send all operations in a single Mutate request.
129+
# The operations must be in this specific order.
130+
mutate_operations = [
131+
asset_operation_1,
132+
asset_operation_2,
133+
experiment_operation,
134+
*arm_operations,
135+
asset_group_asset_operation_1,
136+
asset_group_asset_operation_2,
137+
]
138+
139+
response = googleads_service.mutate(
191140
customer_id=customer_id,
192-
mutate_operations=[
193-
asset_group_asset_operation_1,
194-
asset_group_asset_operation_2,
195-
],
141+
mutate_operations=mutate_operations,
196142
)
197143
# [END create_asset_optimization_experiment_1]
198144

199145
# Print the results.
200-
print(f"Created headline asset: {headline_asset_resource_name}")
201-
print(f"Created image asset: {image_asset_resource_name}")
202-
print(f"Created experiment: {experiment_resource_name}")
203-
print(f"Created control arm: {control_arm_result.resource_name}")
204-
print(f"Created treatment arm: {treatment_arm_result.resource_name}")
146+
print(
147+
"Created headline asset:"
148+
f" {response.mutate_operation_responses[0].asset_result.resource_name}"
149+
)
150+
print(
151+
"Created image asset:"
152+
f" {response.mutate_operation_responses[1].asset_result.resource_name}"
153+
)
154+
print(
155+
"Created experiment:"
156+
f" {response.mutate_operation_responses[2].experiment_result.resource_name}"
157+
)
158+
print(
159+
"Created control arm:"
160+
f" {response.mutate_operation_responses[3].experiment_arm_result.resource_name}"
161+
)
162+
print(
163+
"Created treatment arm:"
164+
f" {response.mutate_operation_responses[4].experiment_arm_result.resource_name}"
165+
)
205166
print(
206167
"Created asset group asset for headline:"
207-
f" {aga_response.mutate_operation_responses[0].asset_group_asset_result.resource_name}"
168+
f" {response.mutate_operation_responses[5].asset_group_asset_result.resource_name}"
208169
)
209170
print(
210171
"Created asset group asset for image:"
211-
f" {aga_response.mutate_operation_responses[1].asset_group_asset_result.resource_name}"
172+
f" {response.mutate_operation_responses[6].asset_group_asset_result.resource_name}"
212173
)
213174

214175

@@ -245,22 +206,22 @@ def create_image_asset_operation(
245206
def create_arms_operations(
246207
client: GoogleAdsClient,
247208
customer_id: str,
248-
experiment_resource_name: str,
209+
experiment_temp_id: str,
249210
campaign_resource_name: str,
250211
asset_group_id: str,
251212
treatment_assets: List[Tuple[str, Any]],
252-
) -> List[ExperimentArmOperation]:
213+
) -> List[MutateOperation]:
253214
"""Creates mutate operations for control and treatment arms."""
254215
googleads_service = client.get_service("GoogleAdsService")
255216
experiment_arm_type = client.get_type("ExperimentArm")
256-
operations: List[ExperimentArmOperation] = []
217+
operations = []
257218

258219
# Control arm
259-
control_operation: ExperimentArmOperation = client.get_type(
260-
"ExperimentArmOperation"
220+
control_operation = client.get_type("MutateOperation")
221+
control = control_operation.experiment_arm_operation.create
222+
control.experiment = googleads_service.experiment_path(
223+
customer_id, experiment_temp_id
261224
)
262-
control: ExperimentArm = control_operation.create
263-
control.experiment = experiment_resource_name
264225
control.name = "Base Assets (Control)"
265226
control.control = True
266227
control.traffic_split = 50
@@ -274,25 +235,26 @@ def create_arms_operations(
274235
operations.append(control_operation)
275236

276237
# Treatment arm
277-
treatment_operation: ExperimentArmOperation = client.get_type(
278-
"ExperimentArmOperation"
238+
treatment_operation = client.get_type("MutateOperation")
239+
treatment = treatment_operation.experiment_arm_operation.create
240+
treatment.experiment = googleads_service.experiment_path(
241+
customer_id, experiment_temp_id
279242
)
280-
treatment: ExperimentArm = treatment_operation.create
281-
treatment.experiment = experiment_resource_name
282243
treatment.name = "New Assets (Treatment)"
283244
treatment.control = False
284245
treatment.traffic_split = 50
285-
# NOTE: Do not set treatment.campaigns, as the backend automatically creates
286-
# the treatment campaign for Performance Max / OPTIMIZE_ASSETS experiments.
246+
treatment.campaigns.append(campaign_resource_name)
287247

288248
asset_group_info_treatment = experiment_arm_type.AssetGroupInfo()
289249
asset_group_info_treatment.asset_group = googleads_service.asset_group_path(
290250
customer_id, asset_group_id
291251
)
292252

293-
for asset_resource_name, field_type in treatment_assets:
253+
for asset_temp_id, field_type in treatment_assets:
294254
asset_group_asset_info = experiment_arm_type.AssetGroupAssetInfo()
295-
asset_group_asset_info.asset = asset_resource_name
255+
asset_group_asset_info.asset = googleads_service.asset_path(
256+
customer_id, asset_temp_id
257+
)
296258
asset_group_asset_info.field_type = field_type
297259
asset_group_info_treatment.asset_group_assets.append(
298260
asset_group_asset_info
@@ -306,15 +268,19 @@ def create_arms_operations(
306268

307269
def create_asset_group_asset_operation(
308270
client: GoogleAdsClient,
309-
asset_group_resource_name: str,
310-
asset_resource_name: str,
271+
customer_id: str,
272+
asset_group_id: str,
273+
asset_temp_id: str,
311274
field_type: Any,
312275
) -> MutateOperation:
313276
"""Creates a mutate operation for an asset group asset."""
277+
googleads_service = client.get_service("GoogleAdsService")
314278
operation = client.get_type("MutateOperation")
315279
aga = operation.asset_group_asset_operation.create
316-
aga.asset_group = asset_group_resource_name
317-
aga.asset = asset_resource_name
280+
aga.asset_group = googleads_service.asset_group_path(
281+
customer_id, asset_group_id
282+
)
283+
aga.asset = googleads_service.asset_path(customer_id, asset_temp_id)
318284
aga.field_type = field_type
319285
return operation
320286

0 commit comments

Comments
 (0)