/
ParallelReportDownload.cs
267 lines (244 loc) · 11.1 KB
/
ParallelReportDownload.cs
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
// Copyright 2020 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
//
// http://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.
using CommandLine;
using Google.Ads.Gax.Examples;
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V13.Errors;
using Google.Ads.GoogleAds.V13.Services;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Google.Ads.GoogleAds.Examples.V13
{
/// <summary>
/// Shows how to download a set of reports from a list of accounts in parallel. If you need
/// to obtain a list of accounts, please see the GetAccountHierarchy or
/// ListAccessibleCustomers examples.";
/// </summary>
public class ParallelReportDownload : ExampleBase
{
/// <summary>
/// Command line options for running the <see cref="ParallelReportDownload"/> example.
/// </summary>
public class Options : OptionsBase
{
/// <summary>
/// The Google Ads customer Id.
/// </summary>
[Option("customerIds", Required = true, HelpText =
"The Google Ads customer IDs for which the call is made.")]
public IEnumerable<long> CustomerIds { get; set; }
/// <summary>
/// Optional login customer ID if your access to the CIDs is via a manager account.
/// </summary>
[Option("loginCustomerId", Required = false, HelpText =
"Optional login customer ID if your access to the CIDs is via a manager account.")]
public long? LoginCustomerId { get; set; }
}
/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
/// <param name="args">The command line arguments.</param>
public static void Main(string[] args)
{
Options options = ExampleUtilities.ParseCommandLine<Options>(args);
ParallelReportDownload codeExample = new ParallelReportDownload();
Console.WriteLine(codeExample.Description);
codeExample.Run(new GoogleAdsClient(), options.CustomerIds.ToArray(),
options.LoginCustomerId);
}
// Defines the Google Ads Query Language (GAQL) query strings to run for each customer ID.
private readonly Dictionary<string, string> GAQL_QUERY_STRINGS =
new Dictionary<string, string>()
{
{
"Campaign Query",
@"SELECT campaign.id, metrics.impressions, metrics.clicks
FROM campaign
WHERE segments.date DURING LAST_30_DAYS"
},
{
"Ad Group Query",
@"SELECT campaign.id, ad_group.id, metrics.impressions, metrics.clicks
FROM ad_group
WHERE segments.date DURING LAST_30_DAYS"
}
};
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description =>
"Shows how to download a set of reports from a list of accounts in parallel. If you " +
"need to obtain a list of accounts, please see the GetAccountHierarchy or " +
"ListAccessibleCustomers examples.";
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerIds">The Google Ads customer Id.</param>
/// <param name="loginCustomerId">Optional login customer ID if your access to the CIDs
/// is via a manager account.</param>
public void Run(GoogleAdsClient client, long[] customerIds, long? loginCustomerId)
{
// If a manager ID is supplied, update the login credentials.
if (loginCustomerId.HasValue)
{
client.Config.LoginCustomerId = loginCustomerId.ToString();
}
// Get the GoogleAdsService. A single client can be shared by all threads.
GoogleAdsServiceClient googleAdsService = client.GetService(
Services.V13.GoogleAdsService);
try
{
// Begin downloading reports and block program termination until complete.
Task task = RunDownloadParallelAsync(googleAdsService, customerIds);
task.Wait();
}
catch (GoogleAdsException e)
{
Console.WriteLine("Failure:");
Console.WriteLine($"Message: {e.Message}");
Console.WriteLine($"Failure: {e.Failure}");
Console.WriteLine($"Request ID: {e.RequestId}");
throw;
}
}
/// <summary>
/// Initiate all download requests, wait for their completion, and report the results.
/// </summary>
/// <param name="googleAdsService">The Google Ads service.</param>
/// <param name="customerIds">The list of customer IDs from which to request data.</param>
/// <returns>The asynchronous operation.</returns>
private async Task RunDownloadParallelAsync(
GoogleAdsServiceClient googleAdsService, long[] customerIds)
{
// List of all requests to ensure that we wait for the reports to complete on all
// customer IDs before proceeding.
ConcurrentBag<Task<bool>> tasks =
new ConcurrentBag<Task<bool>>();
// Collection of downloaded responses.
ConcurrentBag<ReportDownload> responses = new ConcurrentBag<ReportDownload>();
// IMPORTANT: You should avoid hitting the same customer ID in parallel. There are rate
// limits at the customer ID level which are much stricter than limits at the developer
// token level. Hitting these limits frequently enough will significantly reduce
// throughput as the client library will automatically retry with exponential back-off
// before failing the request.
Parallel.ForEach(GAQL_QUERY_STRINGS, query =>
{
Parallel.ForEach(customerIds, customerId =>
{
Console.WriteLine($"Requesting {query.Key} for CID {customerId}.");
// Issue an asynchronous search request and add it to the list of requests
// in progress.
tasks.Add(DownloadReportAsync(googleAdsService, customerId, query.Key,
query.Value, responses));
});
}
);
Console.WriteLine($"Awaiting results from {tasks.Count} requests...\n");
// Proceed only when all requests have completed.
await Task.WhenAll(tasks);
// Give a summary report for each successful download.
foreach (ReportDownload reportDownload in responses)
{
Console.WriteLine(reportDownload);
}
}
/// <summary>
/// Initiates one asynchronous report download.
/// </summary>
/// <param name="googleAdsService">The Google Ads service client.</param>
/// <param name="customerId">The customer ID from which data is requested.</param>
/// <param name="queryKey">The name of the query to be downloaded.</param>
/// <param name="queryValue">The query for the download request.</param>
/// <param name="responses">Collection of all successful report downloads.</param>
/// <returns>The asynchronous operation.</returns>
/// <exception cref="GoogleAdsException">Thrown if errors encountered in the execution of
/// the request.</exception>
private Task<bool> DownloadReportAsync(
GoogleAdsServiceClient googleAdsService, long customerId, string queryKey,
string queryValue, ConcurrentBag<ReportDownload> responses)
{
try
{
// Issue an asynchronous download request.
googleAdsService.SearchStream(
customerId.ToString(), queryValue,
delegate (SearchGoogleAdsStreamResponse resp)
{
// Store the results.
responses.Add(new ReportDownload()
{
CustomerId = customerId,
QueryKey = queryKey,
Response = resp
});
}
);
return Task.FromResult(true);
}
catch (AggregateException ae)
{
Console.WriteLine($"Download failed for {queryKey} and CID {customerId}!");
GoogleAdsException gae = GoogleAdsException.FromTaskException(ae);
var download = new ReportDownload()
{
CustomerId = customerId,
QueryKey = queryKey,
Exception = gae
};
if (gae != null)
{
Console.WriteLine($"Message: {gae.Message}");
Console.WriteLine($"Failure: {gae.Failure}");
Console.WriteLine($"Request ID: {gae.RequestId}");
download.Exception = gae;
}
else
{
download.Exception = ae;
}
responses.Add(download);
return Task.FromResult(false);
}
}
/// <summary>
/// Stores a result from a reporting API call. In this case we simply report a count of
/// the responses, but one could also write the response to a database/file, pass the
/// response on to another method for further processing, etc.
/// </summary>
private class ReportDownload
{
internal long CustomerId { get; set; }
internal string QueryKey { get; set; }
internal SearchGoogleAdsStreamResponse Response { get; set; }
internal Exception Exception { get; set; }
public override string ToString()
{
if (Exception != null)
{
return $"Download failed for {QueryKey} and CID {CustomerId}. " +
$"Exception: {Exception}";
}
else
{
return $"{QueryKey} downloaded for CID {CustomerId}: " +
$"{Response.Results.Count} rows returned.";
}
}
}
}
}