|
28 | 28 | """ |
29 | 29 | import argparse |
30 | 30 | import csv |
31 | | -import sys |
32 | 31 | import os |
| 32 | +import sys |
33 | 33 |
|
34 | 34 | from google.ads.googleads.client import GoogleAdsClient |
35 | 35 | from google.ads.googleads.errors import GoogleAdsException |
@@ -57,110 +57,114 @@ def main( |
57 | 57 | output_file: str, |
58 | 58 | write_headers: bool, |
59 | 59 | ) -> 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}") |
110 | 108 |
|
111 | 109 |
|
112 | 110 | 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, |
133 | 159 | ) |
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:' |
142 | 164 | ) |
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