Skip to content

Commit 65e1574

Browse files
Experiments Example Updates (#1076)
1 parent 2bc89d1 commit 65e1574

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

examples/experiments/create_asset_optimization_experiment.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ def main(
5151
search_response = googleads_service.search(
5252
customer_id=customer_id, query=query
5353
)
54-
if len(search_response):
55-
campaign_resource_name = search_response[0].asset_group.campaign
56-
else:
54+
campaign_resource_name = None
55+
for row in search_response:
56+
campaign_resource_name = row.asset_group.campaign
57+
break
58+
59+
if not campaign_resource_name:
5760
print(f"Asset group with ID {asset_group_id} not found.")
5861
sys.exit(1)
5962

examples/experiments/get_experiment_performance.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
"""This example illustrates how to retrieve performance metrics for an experiment.
1616
17-
It shows how to query statistical significance metrics for the experiment arms,
17+
It shows how to query statistical significance metrics for the experiment,
1818
and how to execute actions such as promoting, ending, or graduating an experiment.
1919
"""
2020

@@ -29,7 +29,8 @@
2929
GoogleAdsServiceClient,
3030
)
3131
from google.ads.googleads.v24.services.types.google_ads_service import (
32-
SearchGoogleAdsStreamResponse,
32+
SearchGoogleAdsRequest,
33+
SearchGoogleAdsResponse,
3334
GoogleAdsRow,
3435
)
3536
from google.ads.googleads.v24.services.services.experiment_service import (
@@ -62,15 +63,13 @@ def main(client: GoogleAdsClient, customer_id: str, experiment_id: str) -> None:
6263
"""
6364
ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")
6465

65-
# Query to retrieve both control and treatment arms under the parent experiment.
66+
# Query to retrieve the experiment.
6667
# Notice that we request the statistical metrics (e.g., p-value, point estimate,
67-
# margin of error) which are populated exclusively on the treatment arm row.
68+
# margin of error) which are populated based on the treatment arm.
6869
query = f"""
6970
SELECT
70-
experiment_arm.resource_name,
71-
experiment_arm.name,
72-
experiment_arm.control,
73-
experiment_arm.traffic_split,
71+
experiment.resource_name,
72+
experiment.name,
7473
experiment.resource_name,
7574
experiment.experiment_id,
7675
experiment.type,
@@ -80,41 +79,37 @@ def main(client: GoogleAdsClient, customer_id: str, experiment_id: str) -> None:
8079
metrics.clicks_p_value,
8180
metrics.clicks_point_estimate,
8281
metrics.clicks_margin_of_error
83-
FROM experiment_arm
82+
FROM experiment
8483
WHERE experiment.experiment_id = {experiment_id}
8584
"""
8685

87-
# Issues a search request using streaming.
88-
stream: Iterator[SearchGoogleAdsStreamResponse] = ga_service.search_stream(
89-
customer_id=customer_id, query=query
86+
# Issues a search request.
87+
search_request: SearchGoogleAdsRequest = client.get_type(
88+
"SearchGoogleAdsRequest"
9089
)
90+
search_request.customer_id = customer_id
91+
search_request.query = query
9192

92-
has_results = False
93-
for batch in stream:
94-
rows: List[GoogleAdsRow] = batch.results
95-
for row in rows:
96-
has_results = True
97-
print(f"Found experiment arm: {row.experiment_arm.name}")
98-
print(f" Resource Name: {row.experiment_arm.resource_name}")
99-
print(f" Control: {row.experiment_arm.control}")
100-
print(f" Traffic Split: {row.experiment_arm.traffic_split}%")
93+
results: SearchGoogleAdsResponse = ga_service.search(request=search_request)
10194

102-
# Statistical evaluation is only valid on the treatment (non-control) arm
103-
# because significance metrics are only populated relative to the baseline.
104-
# Note: For intra-campaign/in-campaign experiments, only a single treatment row is
105-
# returned (with control = False), since there is no separate control campaign.
106-
if not row.experiment_arm.control:
107-
evaluate_experiment(client, customer_id, row)
95+
experiment_found = False
96+
row: GoogleAdsRow
97+
# There should be at most one row.
98+
for row in results:
99+
experiment_found = True
100+
print(f"Found experiment: {row.experiment.name}")
101+
print(f" Resource Name: {row.experiment.resource_name}")
108102

109-
if not has_results:
110-
print(f"No experiment arms found for experiment ID: {experiment_id}")
103+
evaluate_experiment(client, customer_id, row)
104+
if not experiment_found:
105+
print(f"No experiment found for experiment ID: {experiment_id}")
111106

112107

113108
# [START get_experiment_performance_1]
114109
def evaluate_experiment(
115110
client: GoogleAdsClient, customer_id: str, row: GoogleAdsRow
116111
) -> None:
117-
"""Evaluates the performance of the treatment experiment arm.
112+
"""Evaluates the performance of the experiment.
118113
119114
Args:
120115
client: an initialized GoogleAdsClient instance.

0 commit comments

Comments
 (0)