Skip to content

Commit f018027

Browse files
bobhancockgBenRKarl
authored andcommitted
Change v19 to v20
1 parent eae2acd commit f018027

4 files changed

Lines changed: 346 additions & 342 deletions

File tree

examples/misc/add_ad_group_image_asset.py

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,69 @@ def main(
3232
ad_group_id: str,
3333
asset_id: str,
3434
) -> None:
35-
ad_group_asset_service = client.get_service("AdGroupAssetService")
36-
ad_group_asset_resource_name: str = ad_group_asset_service.asset_path(
37-
customer_id, asset_id
38-
)
35+
ad_group_asset_service = client.get_service("AdGroupAssetService")
36+
ad_group_asset_resource_name: str = ad_group_asset_service.asset_path(
37+
customer_id, asset_id
38+
)
3939

40-
ad_group_asset_operation = client.get_type("AdGroupAssetOperation")
41-
ad_group_asset_set = ad_group_asset_operation.create
42-
ad_group_asset_set.asset = ad_group_asset_resource_name
43-
ad_group_asset_set.field_type = client.enums.AssetFieldTypeEnum.AD_IMAGE
44-
ad_group_asset_set.ad_group = ad_group_asset_service.ad_group_path(
45-
customer_id, ad_group_id
46-
)
47-
response = ad_group_asset_service.mutate_ad_group_assets(
48-
customer_id=customer_id, operations=[ad_group_asset_operation]
49-
)
40+
ad_group_asset_operation = client.get_type("AdGroupAssetOperation")
41+
ad_group_asset_set = ad_group_asset_operation.create
42+
ad_group_asset_set.asset = ad_group_asset_resource_name
43+
ad_group_asset_set.field_type = client.enums.AssetFieldTypeEnum.AD_IMAGE
44+
ad_group_asset_set.ad_group = ad_group_asset_service.ad_group_path(
45+
customer_id, ad_group_id
46+
)
47+
response = ad_group_asset_service.mutate_ad_group_assets(
48+
customer_id=customer_id, operations=[ad_group_asset_operation]
49+
)
5050

51-
for result in response.results:
52-
print(
53-
"Created ad group asset with resource name: "
54-
f"'{result.resource_name}'"
55-
)
51+
for result in response.results:
52+
print(
53+
f"Created ad group asset with resource name: '{result.resource_name}'"
54+
)
5655

5756

5857
if __name__ == "__main__":
59-
parser = argparse.ArgumentParser(
60-
description=(
61-
"Updates an ad group for specified customer and ad group "
62-
"id with the given image asset id."
63-
)
64-
)
65-
# The following argument(s) should be provided to run the example.
66-
parser.add_argument(
67-
"-c",
68-
"--customer_id",
69-
type=str,
70-
required=True,
71-
help="The Google Ads customer ID.",
72-
)
73-
parser.add_argument(
74-
"-a", "--ad_group_id", type=str, required=True, help="The ad group ID."
75-
)
76-
parser.add_argument(
77-
"-s",
78-
"--asset_id",
79-
type=str,
80-
required=True,
81-
help="The asset ID.",
82-
)
83-
args = parser.parse_args()
58+
parser = argparse.ArgumentParser(
59+
description=(
60+
"Updates an ad group for specified customer and ad group "
61+
"id with the given image asset id."
62+
)
63+
)
64+
# The following argument(s) should be provided to run the example.
65+
parser.add_argument(
66+
"-c",
67+
"--customer_id",
68+
type=str,
69+
required=True,
70+
help="The Google Ads customer ID.",
71+
)
72+
parser.add_argument(
73+
"-a", "--ad_group_id", type=str, required=True, help="The ad group ID."
74+
)
75+
parser.add_argument(
76+
"-s",
77+
"--asset_id",
78+
type=str,
79+
required=True,
80+
help="The asset ID.",
81+
)
82+
args = parser.parse_args()
8483

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

89-
try:
90-
main(
91-
googleads_client, args.customer_id, args.ad_group_id, args.asset_id
92-
)
93-
except GoogleAdsException as ex:
94-
print(
95-
f'Request with ID "{ex.request_id}" failed with status '
96-
f'"{ex.error.code().name}" and includes the following errors:'
97-
)
98-
for error in ex.failure.errors:
99-
print(f'\tError with message "{error.message}".')
100-
if error.location:
101-
for field_path_element in error.location.field_path_elements:
102-
print(f"\t\tOn field: {field_path_element.field_name}")
103-
sys.exit(1)
88+
try:
89+
main(googleads_client, args.customer_id, args.ad_group_id, args.asset_id)
90+
except GoogleAdsException as ex:
91+
print(
92+
f'Request with ID "{ex.request_id}" failed with status '
93+
f'"{ex.error.code().name}" and includes the following errors:'
94+
)
95+
for error in ex.failure.errors:
96+
print(f'\tError with message "{error.message}".')
97+
if error.location:
98+
for field_path_element in error.location.field_path_elements:
99+
print(f"\t\tOn field: {field_path_element.field_name}")
100+
sys.exit(1)

examples/misc/campaign_report_to_csv.py

Lines changed: 107 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"""
2929
import argparse
3030
import csv
31-
import sys
3231
import os
32+
import sys
3333

3434
from google.ads.googleads.client import GoogleAdsClient
3535
from google.ads.googleads.errors import GoogleAdsException
@@ -57,110 +57,114 @@ def main(
5757
output_file: str,
5858
write_headers: bool,
5959
) -> None:
60-
"""Writes rows returned from a search_stream request to a CSV file.
61-
Args:
62-
client: An initialized GoogleAdsClient instance.
63-
customer_id (str): The client customer ID string.
64-
output_file (str): Filename of the file to write the report data to.
65-
write_headers (bool): From argparse, True if arg is provided.
66-
"""
67-
file_dir: str = os.path.dirname(os.path.abspath(__file__))
68-
file_path: str = os.path.join(file_dir, output_file)
69-
ga_service = client.get_service("GoogleAdsService")
70-
71-
# Issues a search request using streaming.
72-
search_request = client.get_type("SearchGoogleAdsStreamRequest")
73-
search_request.customer_id = customer_id
74-
search_request.query = _QUERY
75-
stream = ga_service.search_stream(search_request)
76-
77-
with open(file_path, "w", newline="") as f:
78-
writer = csv.writer(f)
79-
80-
# Define a list of headers for the first row.
81-
headers: list[str] = [
82-
"Account",
83-
"Date",
84-
"Campaign",
85-
"Impressions",
86-
"Clicks",
87-
"Cost",
88-
]
89-
90-
# If the write_headers flag was passed, write header row to the CSV
91-
if write_headers:
92-
writer.writerow(headers)
93-
94-
for batch in stream:
95-
for row in batch.results:
96-
# Use the CSV writer to write the individual GoogleAdsRow
97-
# fields returned in the SearchGoogleAdsStreamResponse.
98-
writer.writerow(
99-
[
100-
row.customer.descriptive_name,
101-
row.segments.date,
102-
row.campaign.name,
103-
row.metrics.impressions,
104-
row.metrics.clicks,
105-
row.metrics.cost_micros,
106-
]
107-
)
108-
109-
print(f"Customer {customer_id} report written to {output_file}")
60+
"""Writes rows returned from a search_stream request to a CSV file.
61+
Args:
62+
client: An initialized GoogleAdsClient instance.
63+
customer_id (str): The client customer ID string.
64+
output_file (str): Filename of the file to write the report data to.
65+
write_headers (bool): From argparse, True if arg is provided.
66+
"""
67+
file_dir: str = os.path.dirname(os.path.abspath(__file__))
68+
file_path: str = os.path.join(file_dir, output_file)
69+
ga_service = client.get_service("GoogleAdsService")
70+
71+
# Issues a search request using streaming.
72+
search_request = client.get_type("SearchGoogleAdsStreamRequest")
73+
search_request.customer_id = customer_id
74+
search_request.query = _QUERY
75+
stream = ga_service.search_stream(search_request)
76+
77+
with open(file_path, "w", newline="") as f:
78+
writer = csv.writer(f)
79+
80+
# Define a list of headers for the first row.
81+
headers: list[str] = [
82+
"Account",
83+
"Date",
84+
"Campaign",
85+
"Impressions",
86+
"Clicks",
87+
"Cost",
88+
]
89+
90+
# If the write_headers flag was passed, write header row to the CSV
91+
if write_headers:
92+
writer.writerow(headers)
93+
94+
for batch in stream:
95+
for row in batch.results:
96+
# Use the CSV writer to write the individual GoogleAdsRow
97+
# fields returned in the SearchGoogleAdsStreamResponse.
98+
writer.writerow([
99+
row.customer.descriptive_name,
100+
row.segments.date,
101+
row.campaign.name,
102+
row.metrics.impressions,
103+
row.metrics.clicks,
104+
row.metrics.cost_micros,
105+
])
106+
107+
print(f"Customer {customer_id} report written to {output_file}")
110108

111109

112110
if __name__ == "__main__":
113-
parser = argparse.ArgumentParser(
114-
description="Retrieves a campaign stats and writes to CSV file."
115-
)
116-
# The following argument(s) should be provided to run the example.
117-
parser.add_argument(
118-
"-c",
119-
"--customer_id",
120-
type=str,
121-
required=True,
122-
help="The Google Ads customer ID of the account you would like to get "
123-
"the report for to write to CSV.",
124-
)
125-
parser.add_argument(
126-
"-o",
127-
"--output_file",
128-
type=str,
129-
required=False,
130-
default=_DEFAULT_FILE_NAME,
131-
help="Name of the local CSV file to save the report to. File will be "
132-
"saved in the same directory as the script.",
111+
parser = argparse.ArgumentParser(
112+
description="Retrieves a campaign stats and writes to CSV file."
113+
)
114+
# The following argument(s) should be provided to run the example.
115+
parser.add_argument(
116+
"-c",
117+
"--customer_id",
118+
type=str,
119+
required=True,
120+
help=(
121+
"The Google Ads customer ID of the account you would like to get "
122+
"the report for to write to CSV."
123+
),
124+
)
125+
parser.add_argument(
126+
"-o",
127+
"--output_file",
128+
type=str,
129+
required=False,
130+
default=_DEFAULT_FILE_NAME,
131+
help=(
132+
"Name of the local CSV file to save the report to. File will be "
133+
"saved in the same directory as the script."
134+
),
135+
)
136+
# Optional boolean argument for writing headers.
137+
parser.add_argument(
138+
"-w",
139+
"--write_headers",
140+
action="store_true",
141+
help=(
142+
"Writes headers to the CSV file if argument is supplied. Simply "
143+
"add -w if you want the headers defined in the script to be "
144+
"added as the first row in the CSV file."
145+
),
146+
)
147+
args = parser.parse_args()
148+
149+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
150+
# home directory if none is specified.
151+
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
152+
153+
try:
154+
main(
155+
googleads_client,
156+
args.customer_id,
157+
args.output_file,
158+
args.write_headers,
133159
)
134-
# Optional boolean argument for writing headers.
135-
parser.add_argument(
136-
"-w",
137-
"--write_headers",
138-
action="store_true",
139-
help="Writes headers to the CSV file if argument is supplied. Simply "
140-
"add -w if you want the headers defined in the script to be "
141-
"added as the first row in the CSV file.",
160+
except GoogleAdsException as ex:
161+
print(
162+
f'Request with ID "{ex.request_id}" failed with status '
163+
f'"{ex.error.code().name}" and includes the following errors:'
142164
)
143-
args = parser.parse_args()
144-
145-
# GoogleAdsClient will read the google-ads.yaml configuration file in the
146-
# home directory if none is specified.
147-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
148-
149-
try:
150-
main(
151-
googleads_client,
152-
args.customer_id,
153-
args.output_file,
154-
args.write_headers,
155-
)
156-
except GoogleAdsException as ex:
157-
print(
158-
f'Request with ID "{ex.request_id}" failed with status '
159-
f'"{ex.error.code().name}" and includes the following errors:'
160-
)
161-
for error in ex.failure.errors:
162-
print(f'\tError with message "{error.message}".')
163-
if error.location:
164-
for field_path_element in error.location.field_path_elements:
165-
print(f"\t\tOn field: {field_path_element.field_name}")
166-
sys.exit(1)
165+
for error in ex.failure.errors:
166+
print(f'\tError with message "{error.message}".')
167+
if error.location:
168+
for field_path_element in error.location.field_path_elements:
169+
print(f"\t\tOn field: {field_path_element.field_name}")
170+
sys.exit(1)

0 commit comments

Comments
 (0)