-
Notifications
You must be signed in to change notification settings - Fork 96
/
AddDynamicSearchAds.cs
333 lines (298 loc) · 13.9 KB
/
AddDynamicSearchAds.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
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
// 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.V16.Common;
using Google.Ads.GoogleAds.V16.Errors;
using Google.Ads.GoogleAds.V16.Resources;
using Google.Ads.GoogleAds.V16.Services;
using System;
using System.Collections.Generic;
using static Google.Ads.GoogleAds.V16.Enums.AdGroupAdStatusEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.AdGroupCriterionStatusEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.AdGroupStatusEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.AdGroupTypeEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.AdvertisingChannelTypeEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.BudgetDeliveryMethodEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.CampaignStatusEnum.Types;
using static Google.Ads.GoogleAds.V16.Enums.WebpageConditionOperandEnum.Types;
using static Google.Ads.GoogleAds.V16.Resources.Campaign.Types;
namespace Google.Ads.GoogleAds.Examples.V16
{
/// <summary>
/// This code example adds dynamic search ads to a given ad group. To list
/// ad groups, run GetAdGroups.cs.
/// </summary>
public class AddDynamicSearchAds : ExampleBase
{
/// <summary>
/// Command line options for running the <see cref="AddDynamicSearchAds"/> example.
/// </summary>
public class Options : OptionsBase
{
/// <summary>
/// The customer ID for which the call is made.
/// </summary>
[Option("customerId", Required = true, HelpText =
"The 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);
AddDynamicSearchAds codeExample = new AddDynamicSearchAds();
Console.WriteLine(codeExample.Description);
codeExample.Run(new GoogleAdsClient(), options.CustomerId);
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description =>
"This code example adds dynamic search ads to a given ad group. To list ad groups, " +
"run GetAdGroups.cs.";
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The customer ID for which the call is made.</param>
public void Run(GoogleAdsClient client, long customerId)
{
try
{
string budgetResourceName = AddCampaignBudget(client, customerId);
string campaignResourceName = AddCampaign(client, customerId, budgetResourceName);
string adGroupResourceName = AddAdGroup(client, customerId, campaignResourceName);
AddExpandedDSA(client, customerId, adGroupResourceName);
AddWebPageCriteria(client, customerId, adGroupResourceName);
}
catch (GoogleAdsException e)
{
Console.WriteLine("Failure:");
Console.WriteLine($"Message: {e.Message}");
Console.WriteLine($"Failure: {e.Failure}");
Console.WriteLine($"Request ID: {e.RequestId}");
throw;
}
}
/// <summary>
/// Adds a campaign budget.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
/// <returns>The campaign budget resource name.</returns>
private static string AddCampaignBudget(GoogleAdsClient client, long customerId)
{
// Get the CampaignBudgetService.
CampaignBudgetServiceClient campaignBudgetService =
client.GetService(Services.V16.CampaignBudgetService);
// Create the budget.
CampaignBudget campaignBudget = new CampaignBudget()
{
Name = "Interplanetary Cruise Budget #" + ExampleUtilities.GetRandomString(),
AmountMicros = 3_000_000,
DeliveryMethod = BudgetDeliveryMethod.Standard
};
// Create the operation.
CampaignBudgetOperation operation = new CampaignBudgetOperation()
{
Create = campaignBudget
};
// Add the campaign budget.
MutateCampaignBudgetsResponse response =
campaignBudgetService.MutateCampaignBudgets(customerId.ToString(),
new CampaignBudgetOperation[] { operation });
// Display the result.
string budgetResourceName = response.Results[0].ResourceName;
Console.WriteLine($"Added budget with resource name '{budgetResourceName}'.");
return budgetResourceName;
}
/// <summary>
/// Adds a campaign.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
/// <param name="budgetResourceName">The campaign budget resource name.</param>
/// <returns>The campaign resource name.</returns>
// [START add_dynamic_search_ads]
private static string AddCampaign(GoogleAdsClient client, long customerId,
string budgetResourceName)
{
// Get the CampaignService.
CampaignServiceClient campaignService = client.GetService(Services.V16.CampaignService);
// Create the campaign.
Campaign campaign = new Campaign()
{
Name = "Interplanetary Cruise #" + ExampleUtilities.GetRandomString(),
AdvertisingChannelType = AdvertisingChannelType.Search,
Status = CampaignStatus.Paused,
ManualCpc = new ManualCpc(),
CampaignBudget = budgetResourceName,
// Enable the campaign for DSAs.
DynamicSearchAdsSetting = new DynamicSearchAdsSetting()
{
DomainName = "example.com",
LanguageCode = "en"
},
StartDate = DateTime.Now.AddDays(1).ToString("yyyyMMdd"),
EndDate = DateTime.Now.AddDays(30).ToString("yyyyMMdd")
};
// Create the operation.
CampaignOperation operation = new CampaignOperation()
{
Create = campaign
};
// Add the campaign.
MutateCampaignsResponse response =
campaignService.MutateCampaigns(customerId.ToString(),
new CampaignOperation[] { operation });
// Displays the result.
string campaignResourceName = response.Results[0].ResourceName;
Console.WriteLine($"Added campaign with resource name '{campaignResourceName}'.");
return campaignResourceName;
}
// [END add_dynamic_search_ads]
/// <summary>Adds an ad group.</summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
/// <param name="campaignResourceName">The campaign resource name.</param>
/// <returns>The ad group resource name.</returns>
// [START add_dynamic_search_ads_1]
private static string AddAdGroup(GoogleAdsClient client, long customerId,
string campaignResourceName)
{
// Get the AdGroupService.
AdGroupServiceClient adGroupService = client.GetService(Services.V16.AdGroupService);
// Create the ad group.
AdGroup adGroup = new AdGroup()
{
Name = "Earth to Mars Cruises #" + ExampleUtilities.GetRandomString(),
Campaign = campaignResourceName,
Type = AdGroupType.SearchDynamicAds,
Status = AdGroupStatus.Paused,
TrackingUrlTemplate = "http://tracker.examples.com/traveltracker/{escapedlpurl}",
CpcBidMicros = 50_000
};
// Create the operation.
AdGroupOperation operation = new AdGroupOperation()
{
Create = adGroup
};
// Add the ad group.
MutateAdGroupsResponse response =
adGroupService.MutateAdGroups(customerId.ToString(),
new AdGroupOperation[] { operation });
// Display the results.
string adGroupResourceName = response.Results[0].ResourceName;
Console.WriteLine($"Added ad group with resource name '{adGroupResourceName}'.");
return adGroupResourceName;
}
// [END add_dynamic_search_ads_1]
/// <summary>Adds an expanded dynamic search ad.</summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
/// <param name="adGroupResourceName">The ad group resource name.</param>
// [START add_dynamic_search_ads_2]
private static void AddExpandedDSA(GoogleAdsClient client, long customerId,
string adGroupResourceName)
{
// Get the AdGroupAdService.
AdGroupAdServiceClient adGroupAdService =
client.GetService(Services.V16.AdGroupAdService);
// Create an ad group ad.
AdGroupAd adGroupAd = new AdGroupAd()
{
AdGroup = adGroupResourceName,
Status = AdGroupAdStatus.Paused,
// Set the ad as an expanded dynamic search ad.
Ad = new Ad()
{
ExpandedDynamicSearchAd = new ExpandedDynamicSearchAdInfo()
{
Description = "Buy tickets now!"
}
}
};
// Create the operation.
AdGroupAdOperation operation = new AdGroupAdOperation()
{
Create = adGroupAd
};
// Add the dynamic search ad.
MutateAdGroupAdsResponse response = adGroupAdService.MutateAdGroupAds(
customerId.ToString(), new AdGroupAdOperation[] { operation });
// Display the response.
Console.WriteLine($"Added ad group ad with resource name " +
$"'{response.Results[0].ResourceName}'.");
}
// [END add_dynamic_search_ads_2]
/// <summary>Add webpage targeting criteria for the DSA ad group.</summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
/// <param name="adGroupResourceName">The resource name of the ad group for which targeting
/// is aded.</param>
private static void AddWebPageCriteria(GoogleAdsClient client, long customerId,
string adGroupResourceName)
{
// Get the AdGroupCriterionService.
AdGroupCriterionServiceClient adGroupCriterionService =
client.GetService(Services.V16.AdGroupCriterionService);
// Create the criterion.
AdGroupCriterion adGroupCriterion = new AdGroupCriterion()
{
AdGroup = adGroupResourceName,
CpcBidMicros = 1_000_000,
Status = AdGroupCriterionStatus.Paused,
// Set the webpage targeting criteria.
Webpage = new WebpageInfo()
{
CriterionName = "Special Offers",
Conditions =
{
// Adds the url targeting criteria.
new WebpageConditionInfo()
{
Operand = WebpageConditionOperand.Url,
Argument = "/specialoffers"
},
// Adds the page title criteria.
// The list of webpage targeting conditions are
// and-ed together when evaluated for targeting.
new WebpageConditionInfo()
{
Operand = WebpageConditionOperand.PageTitle,
Argument = "Special Offer"
}
}
}
};
// Create the operation.
AdGroupCriterionOperation operation = new AdGroupCriterionOperation()
{
Create = adGroupCriterion
};
// Add the webpage criteria.
MutateAdGroupCriteriaResponse response =
adGroupCriterionService.MutateAdGroupCriteria(
customerId.ToString(), new[] { operation });
// Displays the results.
Console.WriteLine($"Added ad group criterion with resource name " +
$"'{response.Results[0].ResourceName}'");
}
}
}