Skip to content

Commit cd31bf4

Browse files
authored
Add annotations to examples/recommendations (#949)
1 parent 5b11e15 commit cd31bf4

File tree

4 files changed

+142
-55
lines changed

4 files changed

+142
-55
lines changed

examples/recommendations/detect_and_apply_recommendations.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@
3131

3232
import argparse
3333
import sys
34+
from typing import List, Iterable
3435

3536
from google.ads.googleads.client import GoogleAdsClient
3637
from google.ads.googleads.errors import GoogleAdsException
38+
from google.ads.googleads.v20.services.types.google_ads_service import (
39+
GoogleAdsRow,
40+
)
41+
from google.ads.googleads.v20.services.types.recommendation_service import (
42+
ApplyRecommendationOperation,
43+
ApplyRecommendationResult,
44+
)
3745

3846

39-
def main(client, customer_id):
47+
def main(client: GoogleAdsClient, customer_id: str) -> None:
4048
"""The main method that creates all necessary entities for the example.
4149
4250
Args:
@@ -46,7 +54,9 @@ def main(client, customer_id):
4654
detect_and_apply_recommendations(client, customer_id)
4755

4856

49-
def detect_and_apply_recommendations(client, customer_id):
57+
def detect_and_apply_recommendations(
58+
client: GoogleAdsClient, customer_id: str
59+
) -> None:
5060
"""Detects recommendations and applies them.
5161
5262
Args:
@@ -56,7 +66,7 @@ def detect_and_apply_recommendations(client, customer_id):
5666

5767
# [START detect_keyword_recommendations]
5868
googleads_service = client.get_service("GoogleAdsService")
59-
query = f"""
69+
query: str = f"""
6070
SELECT
6171
recommendation.campaign,
6272
recommendation.keyword_recommendation
@@ -65,10 +75,12 @@ def detect_and_apply_recommendations(client, customer_id):
6575
recommendation.type = KEYWORD"""
6676

6777
# Detects keyword recommendations that exist for the customer account.
68-
response = googleads_service.search(customer_id=customer_id, query=query)
78+
response: Iterable[GoogleAdsRow] = googleads_service.search(
79+
customer_id=customer_id, query=query
80+
)
6981

70-
operations = []
71-
for row in response.results:
82+
operations: List[ApplyRecommendationOperation] = []
83+
for row in response:
7284
recommendation = row.recommendation
7385
print(
7486
f"Keyword recommendation ('{recommendation.resource_name}') "
@@ -94,12 +106,13 @@ def detect_and_apply_recommendations(client, customer_id):
94106

95107

96108
# [START build_apply_recommendation_operation]
97-
def build_recommendation_operation(client, recommendation):
109+
def build_recommendation_operation(
110+
client: GoogleAdsClient, recommendation: str
111+
) -> ApplyRecommendationOperation:
98112
"""Creates a ApplyRecommendationOperation to apply the given recommendation.
99113
100114
Args:
101115
client: an initialized GoogleAdsClient instance.
102-
customer_id: a client customer ID.
103116
recommendation: a resource name for the recommendation to be applied.
104117
"""
105118
# If you have a recommendation ID instead of a resource name, you can create
@@ -110,7 +123,9 @@ def build_recommendation_operation(client, recommendation):
110123
# customer_id, recommendation.id
111124
# )
112125

113-
operation = client.get_type("ApplyRecommendationOperation")
126+
operation: ApplyRecommendationOperation = client.get_type(
127+
"ApplyRecommendationOperation"
128+
)
114129

115130
# Each recommendation type has optional parameters to override the
116131
# recommended values. Below is an example showing how to override a
@@ -127,7 +142,11 @@ def build_recommendation_operation(client, recommendation):
127142

128143

129144
# [START apply_recommendation]
130-
def apply_recommendations(client, customer_id, operations):
145+
def apply_recommendations(
146+
client: GoogleAdsClient,
147+
customer_id: str,
148+
operations: List[ApplyRecommendationOperation],
149+
) -> None:
131150
"""Applies a batch of recommendations.
132151
133152
Args:
@@ -137,20 +156,22 @@ def apply_recommendations(client, customer_id, operations):
137156
"""
138157
# Issues a mutate request to apply the recommendations.
139158
recommendation_service = client.get_service("RecommendationService")
140-
response = recommendation_service.apply_recommendation(
141-
customer_id=customer_id, operations=operations
159+
response: ApplyRecommendationResult = (
160+
recommendation_service.apply_recommendation(
161+
customer_id=customer_id, operations=operations
162+
)
142163
)
143164

144165
for result in response.results:
145166
print(
146167
"Applied a recommendation with resource name: "
147-
f"'{result[0].resource_name}'."
168+
f"'{result.resource_name}'."
148169
)
149170
# [END apply_recommendation]
150171

151172

152173
if __name__ == "__main__":
153-
parser = argparse.ArgumentParser(
174+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
154175
description=(
155176
"Retrieves keyword recommendations for specified customer and "
156177
"applies them."
@@ -164,11 +185,13 @@ def apply_recommendations(client, customer_id, operations):
164185
required=True,
165186
help="The Google Ads customer ID.",
166187
)
167-
args = parser.parse_args()
188+
args: argparse.Namespace = parser.parse_args()
168189

169190
# GoogleAdsClient will read the google-ads.yaml configuration file in the
170191
# home directory if none is specified.
171-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
192+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
193+
version="v20"
194+
)
172195

173196
try:
174197
main(googleads_client, args.customer_id)

examples/recommendations/dismiss_recommendation.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,34 @@
2323

2424
from google.ads.googleads.client import GoogleAdsClient
2525
from google.ads.googleads.errors import GoogleAdsException
26+
from google.ads.googleads.v20.services.services.recommendation_service import (
27+
RecommendationServiceClient,
28+
)
29+
from google.ads.googleads.v20.services.types.recommendation_service import (
30+
DismissRecommendationRequest,
31+
DismissRecommendationResponse,
32+
)
2633

2734

28-
def main(client, customer_id, recommendation_id):
29-
recommendation_service = client.get_service("RecommendationService")
30-
request = client.get_type("DismissRecommendationRequest")
35+
def main(
36+
client: GoogleAdsClient, customer_id: str, recommendation_id: str
37+
) -> None:
38+
recommendation_service: RecommendationServiceClient = client.get_service(
39+
"RecommendationService"
40+
)
41+
request: DismissRecommendationRequest = client.get_type(
42+
"DismissRecommendationRequest"
43+
)
3144
operation = request.DismissRecommendationOperation()
3245
operation.resource_name = recommendation_service.recommendation_path(
3346
customer_id, recommendation_id
3447
)
3548
request.customer_id = customer_id
3649
request.operations.append(operation)
3750

38-
response = recommendation_service.dismiss_recommendation(request=request)
51+
response: DismissRecommendationResponse = (
52+
recommendation_service.dismiss_recommendation(request=request)
53+
)
3954

4055
print(
4156
"Dismissed recommendation with resource name: "
@@ -44,7 +59,7 @@ def main(client, customer_id, recommendation_id):
4459

4560

4661
if __name__ == "__main__":
47-
parser = argparse.ArgumentParser(
62+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
4863
description=("Dismisses a recommendation with the given ID.")
4964
)
5065
# The following argument(s) should be provided to run the example.
@@ -62,11 +77,13 @@ def main(client, customer_id, recommendation_id):
6277
required=True,
6378
help="The recommendation ID.",
6479
)
65-
args = parser.parse_args()
80+
args: argparse.Namespace = parser.parse_args()
6681

6782
# GoogleAdsClient will read the google-ads.yaml configuration file in the
6883
# home directory if none is specified.
69-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
84+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
85+
version="v20"
86+
)
7087

7188
try:
7289
main(googleads_client, args.customer_id, args.recommendation_id)

examples/recommendations/generate_budget_recommendations.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,35 @@
2727

2828
import argparse
2929
import sys
30+
from typing import List, Dict, Any
3031

3132
from google.ads.googleads.client import GoogleAdsClient
3233
from google.ads.googleads.errors import GoogleAdsException
33-
34-
35-
def main(client, customer_id):
34+
from google.ads.googleads.v20.services.services.recommendation_service import (
35+
RecommendationServiceClient,
36+
)
37+
from google.ads.googleads.v20.services.types.recommendation_service import (
38+
GenerateRecommendationsRequest,
39+
GenerateRecommendationsResponse,
40+
)
41+
from google.ads.googleads.v20.resources.types.recommendation import (
42+
Recommendation,
43+
)
44+
45+
46+
def main(client: GoogleAdsClient, customer_id: str) -> None:
3647
"""The main method that creates all necessary entities for the example.
3748
3849
Args:
3950
client: an initialized GoogleAdsClient instance.
4051
customer_id: a client customer ID.
4152
"""
42-
recommendation_service = client.get_service("RecommendationService")
43-
request = client.get_type("GenerateRecommendationsRequest")
53+
recommendation_service: RecommendationServiceClient = client.get_service(
54+
"RecommendationService"
55+
)
56+
request: GenerateRecommendationsRequest = client.get_type(
57+
"GenerateRecommendationsRequest"
58+
)
4459

4560
request.customer_id = customer_id
4661
request.recommendation_types = ["CAMPAIGN_BUDGET"]
@@ -51,14 +66,16 @@ def main(client, customer_id):
5166
request.positive_locations_ids = [2840] # 2840 is for United States
5267
request.asset_group_info = [{"final_url": "https://www.your-company.com/"}]
5368

54-
results = recommendation_service.generate_recommendations(request)
69+
results: GenerateRecommendationsResponse = (
70+
recommendation_service.generate_recommendations(request)
71+
)
5572

56-
recommendations = results.recommendations
73+
recommendations: List[Recommendation] = results.recommendations
5774

5875
# Initialize a list to store all budget recommendations with impact metrics.
59-
budget_recommendations_list = []
76+
budget_recommendations_list: List[Dict[str, Any]] = []
6077
# Initialize a list to store budget recommendation amounts.
61-
budget_amounts = []
78+
budget_amounts: List[float] = []
6279

6380
# Get budget recommendations with their associated impact metrics.
6481
for rec in recommendations:
@@ -70,14 +87,16 @@ def main(client, customer_id):
7087
# budget amount best aligns with their goals.
7188
for budget_option in campaign_budget_rec.budget_options:
7289
impact = budget_option.impact
73-
budget_amount = budget_option.budget_amount_micros
74-
if budget_amount > 0:
75-
budget_data = {
76-
"budget_amount": round((budget_amount / 1000000), 2),
90+
budget_amount_micros = budget_option.budget_amount_micros
91+
if budget_amount_micros > 0:
92+
budget_data: Dict[str, Any] = {
93+
"budget_amount": round((budget_amount_micros / 1000000), 2),
7794
"potential_metrics": impact.potential_metrics,
7895
}
7996
budget_recommendations_list.append(budget_data)
80-
budget_amounts.append(round((budget_amount / 1000000), 2))
97+
budget_amounts.append(
98+
round((budget_amount_micros / 1000000), 2)
99+
)
81100

82101
print(f"budget_recommendations_list:\n{budget_recommendations_list}")
83102
"""
@@ -96,7 +115,7 @@ def main(client, customer_id):
96115

97116

98117
if __name__ == "__main__":
99-
parser = argparse.ArgumentParser(
118+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
100119
description=(
101120
"Generate budget recommendations for a Performance Max campaign."
102121
)
@@ -110,11 +129,13 @@ def main(client, customer_id):
110129
help="The Google Ads customer ID.",
111130
)
112131

113-
args = parser.parse_args()
132+
args: argparse.Namespace = parser.parse_args()
114133

115134
# GoogleAdsClient will read the google-ads.yaml configuration file in the
116135
# home directory if none is specified.
117-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
136+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
137+
version="v20"
138+
)
118139

119140
try:
120141
main(googleads_client, args.customer_id)

0 commit comments

Comments
 (0)