-
Notifications
You must be signed in to change notification settings - Fork 95
/
ForecastReach.cs
296 lines (269 loc) · 12.8 KB
/
ForecastReach.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
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
// Copyright 2019 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.Common;
using Google.Ads.GoogleAds.V13.Errors;
using Google.Ads.GoogleAds.V13.Services;
using System;
using System.Collections.Generic;
using static Google.Ads.GoogleAds.V13.Enums.DeviceEnum.Types;
using static Google.Ads.GoogleAds.V13.Enums.GenderTypeEnum.Types;
using static Google.Ads.GoogleAds.V13.Enums.ReachPlanAgeRangeEnum.Types;
namespace Google.Ads.GoogleAds.Examples.V13
{
/// <summary>
/// This example demonstrates how to interact with the ReachPlanService to find plannable
/// locations and product codes, build a media plan, and generate a video ads reach forecast.
/// </summary>
public class ForecastReach : ExampleBase
{
/// <summary>
/// Command line options for running the <see cref="ForecastReach"/> example.
/// </summary>
public class Options : OptionsBase
{
/// <summary>
/// The Google Ads customer ID for which the call is made.
/// </summary>
[Option("customerId", Required = true, HelpText =
"The Google Ads customer ID for which the call is made.")]
public long CustomerId { 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);
ForecastReach codeExample = new ForecastReach();
Console.WriteLine(codeExample.Description);
codeExample.Run(new GoogleAdsClient(), options.CustomerId);
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description =>
"This example demonstrates how to interact with the ReachPlanService to find " +
"plannable locations and product codes, build a media plan, and generate a video ads " +
"reach forecast.";
/// <summary>
/// Runs the code example, showing a typical series of calls to the
/// <see cref="Services.V13.ReachPlanService"/>.
/// </summary>
/// <param name="client">The Google Ads API client.</param>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
public void Run(GoogleAdsClient client, long customerId)
{
string locationId = "2840"; // US
string currencyCode = "USD";
long budgetMicros = 5_000_000L;
ReachPlanServiceClient reachPlanService =
client.GetService(Services.V13.ReachPlanService);
try
{
ShowPlannableLocations(reachPlanService);
ShowPlannableProducts(reachPlanService, locationId);
ForecastMix(reachPlanService, customerId.ToString(), locationId, currencyCode,
budgetMicros);
}
catch (GoogleAdsException e)
{
Console.WriteLine("Failure:");
Console.WriteLine($"Message: {e.Message}");
Console.WriteLine($"Failure: {e.Failure}");
Console.WriteLine($"Request ID: {e.RequestId}");
throw;
}
}
/// <summary>
/// Maps friendly names of plannable locations to location IDs usable with
/// <see cref="ReachPlanServiceClient"/>.
/// </summary>
/// <param name="reachPlanService">Instance of Reach Plan Service client.</param>
public void ShowPlannableLocations(ReachPlanServiceClient reachPlanService)
{
ListPlannableLocationsRequest request = new ListPlannableLocationsRequest();
ListPlannableLocationsResponse response = reachPlanService.ListPlannableLocations(
request);
Console.WriteLine("Plannable Locations:");
Console.WriteLine("Name,\tId,\t,ParentCountryId");
foreach (PlannableLocation location in response.PlannableLocations)
{
Console.WriteLine(
$"\"{location.Name}\",\t{location.Id},{location.ParentCountryId}");
}
}
/// <summary>
/// Lists plannable products for a given location.
/// </summary>
/// <param name="reachPlanService">Instance of Reach Plan Service client.</param>
/// <param name="locationId">Location ID to plan for. To find a valid location ID, either
/// see https://developers.google.com/google-ads/api/reference/data/geotargets or call
/// <see cref="ReachPlanServiceClient.ListPlannableLocations"/>.</param>
// [START forecast_reach_2]
public void ShowPlannableProducts(
ReachPlanServiceClient reachPlanService, string locationId)
{
ListPlannableProductsRequest request = new ListPlannableProductsRequest
{
PlannableLocationId = locationId
};
ListPlannableProductsResponse response = reachPlanService.ListPlannableProducts(
request);
Console.WriteLine($"Plannable Products for location {locationId}:");
foreach (ProductMetadata product in response.ProductMetadata)
{
Console.WriteLine($"{product.PlannableProductCode}:");
Console.WriteLine("Age Ranges:");
foreach (ReachPlanAgeRange ageRange in product.PlannableTargeting.AgeRanges)
{
Console.WriteLine($"\t- {ageRange}");
}
Console.WriteLine("Genders:");
foreach (GenderInfo gender in product.PlannableTargeting.Genders)
{
Console.WriteLine($"\t- {gender.Type}");
}
Console.WriteLine("Devices:");
foreach (DeviceInfo device in product.PlannableTargeting.Devices)
{
Console.WriteLine($"\t- {device.Type}");
}
}
}
// [END forecast_reach_2]
/// <summary>
/// Create a base request to generate a reach forecast.
/// </summary>
/// <param name="customerId">The customer ID for the reach forecast.</param>
/// <param name="productMix">The product mix for the reach forecast.</param>
/// <param name="locationId">Location ID to plan for. To find a valid location ID, either
/// see https://developers.google.com/google-ads/api/reference/data/geotargets or call
/// <see cref="ReachPlanServiceClient.ListPlannableLocations"/>.</param>
/// <param name="currencyCode">Three-character ISO 4217 currency code.</param>
public GenerateReachForecastRequest BuildReachRequest(
string customerId, List<PlannedProduct> productMix, string locationId,
string currencyCode)
{
// Valid durations are between 1 and 90 days.
CampaignDuration duration = new CampaignDuration
{
DurationInDays = 28
};
GenderInfo[] genders =
{
new GenderInfo {Type = GenderType.Female},
new GenderInfo {Type = GenderType.Male}
};
DeviceInfo[] devices =
{
new DeviceInfo {Type = Device.Desktop},
new DeviceInfo {Type = Device.Mobile},
new DeviceInfo {Type = Device.Tablet}
};
Targeting targeting = new Targeting
{
PlannableLocationId = locationId,
AgeRange = ReachPlanAgeRange.AgeRange1865Up,
};
targeting.Genders.AddRange(genders);
targeting.Devices.AddRange(devices);
// See the docs for defaults and valid ranges:
// https://developers.google.com/google-ads/api/reference/rpc/latest/GenerateReachForecastRequest
GenerateReachForecastRequest request = new GenerateReachForecastRequest
{
CustomerId = customerId,
CurrencyCode = currencyCode,
CampaignDuration = duration,
Targeting = targeting,
MinEffectiveFrequency = 1
};
request.PlannedProducts.AddRange(productMix);
return request;
}
/// <summary>
/// Retrieves and prints the reach curve for the given request.
/// </summary>
/// <param name="reachPlanService">Instance of Reach Plan Service client.</param>
/// <param name="request">An already-populated reach curve request.</param>
// [START forecast_reach]
public void GetReachCurve(ReachPlanServiceClient reachPlanService,
GenerateReachForecastRequest request)
{
GenerateReachForecastResponse response = reachPlanService.GenerateReachForecast(
request);
Console.WriteLine("Reach curve output:");
Console.WriteLine(
"Currency, Cost Micros, On-Target Reach, On-Target Impressions, Total Reach," +
" Total Impressions, Products");
foreach (ReachForecast point in response.ReachCurve.ReachForecasts)
{
Console.Write($"{request.CurrencyCode}, ");
Console.Write($"{point.CostMicros}, ");
Console.Write($"{point.Forecast.OnTargetReach}, ");
Console.Write($"{point.Forecast.OnTargetImpressions}, ");
Console.Write($"{point.Forecast.TotalReach}, ");
Console.Write($"{point.Forecast.TotalImpressions}, ");
Console.Write("\"[");
foreach (PlannedProductReachForecast productReachForecast in
point.PlannedProductReachForecasts)
{
Console.Write($"(Product: {productReachForecast.PlannableProductCode}, ");
Console.Write($"Budget Micros: {productReachForecast.CostMicros}), ");
}
Console.WriteLine("]\"");
}
}
// [END forecast_reach]
/// <summary>
/// Gets a forecast for a budget split 15% and 85% between two products.
/// </summary>
/// <param name="reachPlanService">Instance of Reach Plan Service client.</param>
/// <param name="customerId">The customer ID for the reach forecast.</param>
/// <param name="locationId">Location ID to plan for. To find a valid location ID, either
/// see https://developers.google.com/google-ads/api/reference/data/geotargets or call
/// <see cref="ReachPlanServiceClient.ListPlannableLocations"/>.</param>
/// <param name="currencyCode">Three-character ISO 4217 currency code.</param>
/// <param name="budgetMicros">Budget in currency to plan for.</param>
// [START forecast_reach_3]
public void ForecastMix(ReachPlanServiceClient reachPlanService, string customerId,
string locationId, string currencyCode, long budgetMicros)
{
List<PlannedProduct> productMix = new List<PlannedProduct>();
// Set up a ratio to split the budget between two products.
double trueviewAllocation = 0.15;
double bumperAllocation = 1 - trueviewAllocation;
// See listPlannableProducts on ReachPlanService to retrieve a list
// of valid PlannableProductCode's for a given location:
// https://developers.google.com/google-ads/api/reference/rpc/latest/ReachPlanService
productMix.Add(new PlannedProduct
{
PlannableProductCode = "TRUEVIEW_IN_STREAM",
BudgetMicros = Convert.ToInt64(budgetMicros * trueviewAllocation)
});
productMix.Add(new PlannedProduct
{
PlannableProductCode = "BUMPER",
BudgetMicros = Convert.ToInt64(budgetMicros * bumperAllocation)
});
GenerateReachForecastRequest request =
BuildReachRequest(customerId, productMix, locationId, currencyCode);
GetReachCurve(reachPlanService, request);
}
// [END forecast_reach_3]
}
}