-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconversion_reports.py
More file actions
517 lines (461 loc) · 18.3 KB
/
conversion_reports.py
File metadata and controls
517 lines (461 loc) · 18.3 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# Copyright 2025 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 gets conversion reports."""
import argparse
import csv
import sys
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Tuple
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
def handle_googleads_exception(exception: GoogleAdsException) -> None:
"""Prints the details of a GoogleAdsException.
Args:
exception: an exception of type GoogleAdsException.
"""
print(
f'Request with ID "{exception.request_id}" failed with status '
f'"{exception.error.code().name}" and includes the following errors:"'
)
for error in exception.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)
def _calculate_date_range(
start_date_str: Optional[str],
end_date_str: Optional[str],
date_range_preset: Optional[str],
) -> Tuple[str, str]:
"""Calculates the start and end dates based on provided arguments.
Args:
start_date_str: The start date string (YYYY-MM-DD).
end_date_str: The end date string (YYYY-MM-DD).
date_range_preset: A preset date range (e.g., "LAST_30_DAYS").
Returns:
A tuple containing the calculated start and end date strings.
Raises:
SystemExit: If a valid date range cannot be determined.
"""
calculated_start_date: Optional[datetime] = None
calculated_end_date: Optional[datetime] = None
if date_range_preset:
today = datetime.now()
if date_range_preset == "LAST_7_DAYS":
calculated_start_date = today - timedelta(days=7)
calculated_end_date = today
elif date_range_preset == "LAST_10_DAYS":
calculated_start_date = today - timedelta(days=10)
calculated_end_date = today
elif date_range_preset == "LAST_30_DAYS":
calculated_start_date = today - timedelta(days=30)
calculated_end_date = today
elif date_range_preset == "LAST_32_DAYS":
calculated_start_date = today - timedelta(days=32)
calculated_end_date = today
elif date_range_preset == "LAST_MONTH":
first_day_of_current_month = today.replace(day=1)
calculated_end_date = first_day_of_current_month - timedelta(days=1)
calculated_start_date = calculated_end_date.replace(day=1)
elif date_range_preset == "LAST_6_MONTHS":
calculated_start_date = today - timedelta(days=180)
calculated_end_date = today
elif date_range_preset == "LAST_YEAR":
calculated_start_date = today - timedelta(days=365)
calculated_end_date = today
elif start_date_str and end_date_str:
calculated_start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
calculated_end_date = datetime.strptime(end_date_str, "%Y-%m-%d")
if not calculated_start_date or not calculated_end_date:
print("Error: A date range must be specified either by preset or custom dates.")
sys.exit(1)
return (
calculated_start_date.strftime("%Y-%m-%d"),
calculated_end_date.strftime("%Y-%m-%d"),
)
def _process_and_output_results(
results_data: List[Dict[str, Any]], output_format: str, output_file: str
) -> None:
"""Processes and outputs the results to console or CSV.
Args:
results_data: A list of dictionaries containing the report data.
output_format: The desired output format ("console" or "csv").
output_file: The path to the output CSV file (if output_format is "csv").
"""
if not results_data:
print("No data found matching the criteria.")
return
if output_format == "console":
headers = list(results_data[0].keys())
column_widths = {header: len(header) for header in headers}
for row_data in results_data:
for header, value in row_data.items():
column_widths[header] = max(column_widths[header], len(str(value)))
header_line = " | ".join(
header.ljust(column_widths[header]) for header in headers
)
print(header_line)
print("-" * len(header_line))
for row_data in results_data:
print(
" | ".join(
str(row_data[header]).ljust(column_widths[header])
for header in headers
)
)
elif output_format == "csv":
with open(output_file, "w", newline="", encoding="utf-8") as csvfile:
fieldnames = list(results_data[0].keys())
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(results_data)
print(f"Results successfully written to {output_file}")
def get_conversion_actions_report(
client: "GoogleAdsClient", customer_id: str, output_file: str
) -> None:
"""Retrieves all conversion actions and writes them to a CSV file.
Args:
client: An initialized GoogleAdsClient instance.
customer_id: The client customer ID.
output_file: The path to the CSV file to write the results to.
"""
ga_service = client.get_service("GoogleAdsService")
query = """
SELECT
conversion_action.id,
conversion_action.name,
conversion_action.status,
conversion_action.type,
conversion_action.category,
conversion_action.owner_customer,
conversion_action.include_in_conversions_metric,
conversion_action.click_through_lookback_window_days,
conversion_action.view_through_lookback_window_days,
conversion_action.attribution_model_settings.attribution_model,
conversion_action.attribution_model_settings.data_driven_model_status
FROM conversion_action
"""
stream = ga_service.search_stream(customer_id=customer_id, query=query)
results_data: List[Dict[str, Any]] = []
for batch in stream:
for row in batch.results:
ca = row.conversion_action
results_data.append(
{
"ID": ca.id,
"Name": ca.name,
"Status": ca.status.name,
"Type": ca.type.name,
"Category": ca.category.name,
"Owner": ca.owner_customer,
"Include in Conversions Metric": ca.include_in_conversions_metric,
"Click-Through Lookback Window": ca.click_through_lookback_window_days,
"View-Through Lookback Window": ca.view_through_lookback_window_days,
"Attribution Model": ca.attribution_model_settings.attribution_model.name,
"Data-Driven Model Status": ca.attribution_model_settings.data_driven_model_status.name,
}
)
_process_and_output_results(results_data, "csv", output_file)
def get_conversion_performance_report(
client: "GoogleAdsClient",
customer_id: str,
output_format: str,
output_file: str,
start_date: Optional[str],
end_date: Optional[str],
date_range_preset: Optional[str],
metrics: List[str],
filters: List[str],
order_by: Optional[str],
limit: Optional[int],
) -> None:
"""Retrieves and lists Google Ads conversion performance metrics.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
output_format: the output format for the report.
output_file: the path to the output CSV file.
start_date: the start date of the date range to get conversion data.
end_date: the end date of the date range to get conversion data.
date_range_preset: a preset date range to get conversion data.
metrics: a list of metrics to retrieve.
filters: a list of filters to apply to the report.
order_by: a field to order the report by.
limit: the number of results to limit the report to.
"""
ga_service = client.get_service("GoogleAdsService")
start_date_str, end_date_str = _calculate_date_range(
start_date, end_date, date_range_preset
)
select_fields: List[str] = ["segments.date"]
from_resource = "campaign"
# Determine the FROM resource and initial select fields
if "segments.conversion_action_name" in metrics or any(
f.startswith("conversion_action_name=") for f in filters
):
from_resource = "customer"
select_fields.append("segments.conversion_action_name")
else:
select_fields.extend(["campaign.id", "campaign.name"])
metric_fields: List[str] = []
for metric in metrics:
if metric == "conversions":
metric_fields.append("metrics.conversions")
elif metric == "all_conversions":
metric_fields.append("metrics.all_conversions")
elif metric == "conversions_value":
metric_fields.append("metrics.conversions_value")
elif metric == "all_conversions_value":
metric_fields.append("metrics.all_conversions_value")
elif metric == "clicks":
metric_fields.append("metrics.clicks")
elif metric == "impressions":
metric_fields.append("metrics.impressions")
all_select_fields = list(set(select_fields + metric_fields))
query_parts = [f"SELECT {', '.join(all_select_fields)} FROM {from_resource}"]
where_clauses = [f"segments.date BETWEEN '{start_date_str}' AND '{end_date_str}'"]
for f in filters:
if f.startswith("conversion_action_name="):
where_clauses.append(
f"segments.conversion_action_name = '{f.split('=')[1]}'"
)
elif f.startswith("min_conversions="):
where_clauses.append(f"metrics.conversions > {float(f.split('=')[1])}")
elif f.startswith("campaign_id="):
where_clauses.append(f"campaign.id = {f.split('=')[1]}")
elif f.startswith("campaign_name_like="):
where_clauses.append(f"campaign.name LIKE '%{f.split('=')[1]}%'")
if where_clauses:
query_parts.append("WHERE " + " AND ".join(where_clauses))
if order_by:
order_by_field = (
f"metrics.{order_by}"
if order_by
in [
"conversions",
"all_conversions",
"conversions_value",
"all_conversions_value",
"clicks",
"impressions",
]
else order_by
)
query_parts.append(f"ORDER BY {order_by_field} DESC")
if limit:
query_parts.append(f"LIMIT {limit}")
query = " ".join(query_parts)
# --- Execute Query and Process Results ---
try:
stream = ga_service.search_stream(customer_id=customer_id, query=query)
results_data: List[Dict[str, Any]] = []
for batch in stream:
for row in batch.results:
row_data: Dict[str, Any] = {}
if "segments.date" in all_select_fields:
row_data["Date"] = row.segments.date
if "segments.conversion_action_name" in all_select_fields:
row_data["Conversion Action Name"] = (
row.segments.conversion_action_name
)
if "campaign.id" in all_select_fields:
row_data["Campaign ID"] = row.campaign.id
if "campaign.name" in all_select_fields:
row_data["Campaign Name"] = row.campaign.name
if "metrics.conversions" in all_select_fields:
row_data["Conversions"] = row.metrics.conversions
if "metrics.all_conversions" in all_select_fields:
row_data["All Conversions"] = row.metrics.all_conversions
if "metrics.conversions_value" in all_select_fields:
row_data["Conversions Value"] = row.metrics.conversions_value
if "metrics.all_conversions_value" in all_select_fields:
row_data["All Conversions Value"] = (
row.metrics.all_conversions_value
)
if "metrics.clicks" in all_select_fields:
row_data["Clicks"] = row.metrics.clicks
if "metrics.impressions" in all_select_fields:
row_data["Impressions"] = row.metrics.impressions
results_data.append(row_data)
_process_and_output_results(results_data, output_format, output_file)
except GoogleAdsException as ex:
handle_googleads_exception(ex)
def main(
client: "GoogleAdsClient",
customer_id: str,
report_type: str,
output_format: str,
output_file: str,
start_date: Optional[str],
end_date: Optional[str],
date_range_preset: Optional[str],
metrics: List[str],
filters: List[str],
order_by: Optional[str],
limit: Optional[int],
) -> None:
"""The main method that creates all necessary entities for the example.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
report_type: the type of report to generate ("actions" or "performance").
output_format: the output format for the report.
output_file: the path to the output CSV file.
start_date: the start date of the date range to get conversion data.
end_date: the end date of the date range to get conversion data.
date_range_preset: a preset date range to get conversion data.
metrics: a list of metrics to retrieve.
filters: a list of filters to apply to the report.
order_by: a field to order the report by.
limit: the number of results to limit the report to.
"""
try:
if report_type == "actions":
get_conversion_actions_report(client, customer_id, output_file)
elif report_type == "performance":
get_conversion_performance_report(
client,
customer_id,
output_format,
output_file,
start_date,
end_date,
date_range_preset,
metrics,
filters,
order_by,
limit,
)
else:
print(f"Unknown report type: {report_type}")
sys.exit(1)
except GoogleAdsException as ex:
handle_googleads_exception(ex)
except ValueError as ve:
print(f"Error: {ve}")
sys.exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fetches Google Ads conversion data.")
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-r",
"--report_type",
type=str,
required=True,
choices=["actions", "performance"],
help="The type of report to generate ('actions' for conversion actions, 'performance' for conversion performance).",
)
parser.add_argument(
"-o",
"--output_format",
type=str,
choices=["console", "csv"],
default="csv",
help="Output format: 'console' or 'csv' (default).",
)
parser.add_argument(
"-f",
"--output_file",
type=str,
default="saved_csv/conversion_report.csv",
help="Output CSV file name (only used with --output_format csv).",
)
parser.add_argument(
"--start_date",
type=str,
help="Start date for the report (YYYY-MM-DD). Required if --date_range_preset is not used.",
)
parser.add_argument(
"--end_date",
type=str,
help="End date for the report (YYYY-MM-DD). Required if --date_range_preset is not used.",
)
parser.add_argument(
"--date_range_preset",
type=str,
choices=[
"LAST_7_DAYS",
"LAST_10_DAYS",
"LAST_30_DAYS",
"LAST_32_DAYS",
"LAST_MONTH",
"LAST_6_MONTHS",
"LAST_YEAR",
],
help="Preset date range (e.g., LAST_30_DAYS). Overrides --start_date and --end_date.",
)
parser.add_argument(
"--metrics",
nargs="+",
default=["conversions"],
choices=[
"conversions",
"all_conversions",
"conversions_value",
"all_conversions_value",
"clicks",
"impressions",
],
help="Metrics to retrieve. Default is conversions.",
)
parser.add_argument(
"--filters",
nargs="*",
default=[],
help="Filters to apply (e.g., conversion_action_name=Website_Sale, min_conversions=10, campaign_id=123, campaign_name_like=test).",
)
parser.add_argument(
"--order_by",
type=str,
choices=[
"conversions",
"all_conversions",
"conversions_value",
"all_conversions_value",
"clicks",
"impressions",
"segments.conversion_action_name",
"campaign.id",
"campaign.name",
],
help="Field to order results by (e.g., conversions, conversions_value). Default is no specific order.",
)
parser.add_argument(
"--limit",
type=int,
help="Limit the number of results.",
)
args = parser.parse_args()
googleads_client = GoogleAdsClient.load_from_storage(version="v23")
main(
googleads_client,
args.customer_id,
args.report_type,
args.output_format,
args.output_file,
args.start_date,
args.end_date,
args.date_range_preset,
args.metrics,
args.filters,
args.order_by,
args.limit,
)