-
Notifications
You must be signed in to change notification settings - Fork 95
/
AddThingsToDoAd.cs
280 lines (246 loc) · 12 KB
/
AddThingsToDoAd.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
// Copyright 2023 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.Gax.Util;
using Google.Ads.GoogleAds.Extensions.Config;
using Google.Ads.GoogleAds.Config;
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V17.Common;
using Google.Ads.GoogleAds.V17.Errors;
using Google.Ads.GoogleAds.V17.Resources;
using Google.Ads.GoogleAds.V17.Services;
using Google.Protobuf;
using System;
using System.Collections.Generic;
using static Google.Ads.GoogleAds.V17.Enums.AdGroupAdStatusEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AdGroupStatusEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AdGroupTypeEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AdvertisingChannelTypeEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AdvertisingChannelSubTypeEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.BudgetDeliveryMethodEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.CampaignStatusEnum.Types;
using static Google.Ads.GoogleAds.V17.Resources.Campaign.Types;
namespace Google.Ads.GoogleAds.Examples.V17
{
/// <summary>
/// This example creates a Things to do campaign, an ad group and a Things to do ad.
/// Prerequisite: You need to have access to a Things to Do Center account. The integration
/// instructions can be found at: https://support.google.com/google-ads/answer/13387362.
/// </summary>
public class AddThingsToDoAd : ExampleBase
{
/// <summary>
/// Command line options for running the
/// <see cref="AddThingsToDoAd"/> example.
/// </summary>
public class Options : OptionsBase
{
/// <summary>
/// The Google Ads customer ID.
/// </summary>
[Option("customerId", Required = true, HelpText =
"The Google Ads customer ID.")]
public long CustomerId { get; set; }
/// <summary>
/// The Things To Do Center account ID.
/// </summary>
[Option("thingsToDoCenterAccountId", Required = true, HelpText =
"The Things To Do Center account ID.")]
public long ThingsToDoCenterAccountId { 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);
AddThingsToDoAd codeExample = new AddThingsToDoAd();
Console.WriteLine(codeExample.Description);
GoogleAdsClient client = new GoogleAdsClient();
codeExample.Run(client, options.CustomerId,
options.ThingsToDoCenterAccountId);
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description => "This example creates a Things to do campaign, an " +
"ad group and a Things to do ad.\n" +
"Prerequisite: You need to have access to a Things to Do Center account. The " +
"integration instructions can be found at: " +
"https://support.google.com/google-ads/answer/13387362.";
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <param name="thingsToDoCenterAccountId">The Things To Do Center account ID.</param>
public void Run(GoogleAdsClient client, long customerId, long thingsToDoCenterAccountId)
{
// Creates a budget to be used by the campaign that will be created below.
string budget = CreateBudget(client, customerId);
// Creates a Things to do campaign.
string campaign = CreateThingsToDoCampaign(client, customerId, budget,
thingsToDoCenterAccountId);
// Creates an ad group.
string adGroup = CreateAdGroup(client, customerId, campaign);
// Creates an ad.
CreateAdGroupAd(client, customerId, adGroup);
}
/// <summary>
/// Creates the budget for the 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>
/// <returns>The resource name of the newly created campaign budget.</returns>
private static string CreateBudget(GoogleAdsClient client, long customerId)
{
// Get the BudgetService.
CampaignBudgetServiceClient budgetService = client.GetService(
Services.V17.CampaignBudgetService);
// Create the campaign budget.
CampaignBudget budget = new CampaignBudget()
{
Name = "Interplanetary Cruise Budget #" + ExampleUtilities.GetRandomString(),
DeliveryMethod = BudgetDeliveryMethod.Standard,
AmountMicros = 500000,
ExplicitlyShared = true
};
// Create the operation.
CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()
{
Create = budget
};
// Create the campaign budget.
MutateCampaignBudgetsResponse response = budgetService.MutateCampaignBudgets(
customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });
return response.Results[0].ResourceName;
}
/// <summary>
/// Creates a new Things to do campaign in the specified customer account.
/// </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="budget">The resource name of the budget for the new campaign.</param>
/// <param name="thingsToDoCenterAccountId">The Things to Do Center account ID.</param>
/// <returns>The resource name of the newly created campaign.</returns>
private static string CreateThingsToDoCampaign(GoogleAdsClient client, long customerId,
string budget, long thingsToDoCenterAccountId)
{
// Get the CampaignService.
CampaignServiceClient campaignService = client.GetService(Services.V17.CampaignService);
// [START add_things_to_do_ad]
// Creates a campaign.
Campaign campaign = new Campaign()
{
Name = "Interplanetary Cruise #" + ExampleUtilities.GetRandomString(),
AdvertisingChannelType = AdvertisingChannelType.Travel,
AdvertisingChannelSubType = AdvertisingChannelSubType.TravelActivities,
TravelCampaignSettings = new TravelCampaignSettings()
{
TravelAccountId = thingsToDoCenterAccountId
},
// Recommendation: Set the campaign to PAUSED when creating it to prevent
// the ads from immediately serving. Set to ENABLED once you've added
// targeting and the ads are ready to serve
Status = CampaignStatus.Paused,
// Set the bidding strategy and budget.
MaximizeConversionValue = new MaximizeConversionValue(),
CampaignBudget = budget,
// Set the campaign network options.
NetworkSettings = new NetworkSettings
{
TargetGoogleSearch = true
}
};
// [END add_things_to_do_ad]
MutateCampaignsResponse response = campaignService.MutateCampaigns(
customerId.ToString(), new CampaignOperation[] { new CampaignOperation() {
Create = campaign
}}
);
string campaignResourceName = response.Results[0].ResourceName;
Console.WriteLine("Campaign with resource name = '{0}' was added.", campaignResourceName);
return campaignResourceName;
}
/// <summary>
/// Creates a new ad group in the specified Things to do 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="campaign">The resource name of the campaign for the new ad group.</param>
/// <returns>The resource name of the newly created ad group.</returns>
// [START add_things_to_do_ad_2]
private static string CreateAdGroup(GoogleAdsClient client, long customerId,
string campaign)
{
// Get the AdGroupService.
AdGroupServiceClient adGroupService = client.GetService(Services.V17.AdGroupService);
// Create the ad group.
AdGroup adGroup = new AdGroup()
{
Name = $"Earth to Mars Cruises #{ExampleUtilities.GetRandomString()}",
Status = AdGroupStatus.Enabled,
Campaign = campaign,
Type = AdGroupType.TravelAds
};
MutateAdGroupsResponse response = adGroupService.MutateAdGroups(
customerId.ToString(), new AdGroupOperation[] { new AdGroupOperation() {
Create = adGroup
}}
);
string adGroupResourceName = response.Results[0].ResourceName;
Console.WriteLine("Ad group with resource name = '{0}' was added.", adGroupResourceName);
return adGroupResourceName;
}
// [END add_things_to_do_ad_2]
/// <summary>
/// Creates a new ad group ad in the specified 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="adGroup">The resource name of the ad group for the new ad group ad.</param>
// [START add_things_to_do_ad_3]
private static void CreateAdGroupAd(GoogleAdsClient client, long customerId,
string adGroup)
{
// Get the AdGroupAdService.
AdGroupAdServiceClient adGroupAdService =
client.GetService(Services.V17.AdGroupAdService);
// Creates a new ad group ad and sets a travel ad info.
AdGroupAd adGroupAd = new AdGroupAd()
{
Ad = new Ad()
{
TravelAd = new TravelAdInfo()
},
// Set the ad group ad to enabled. Setting this to paused will cause an error for
// Things to do campaigns. Pausing should happen at either the ad group or campaign
// level.
Status = AdGroupAdStatus.Enabled,
AdGroup = adGroup
};
MutateAdGroupAdsResponse response = adGroupAdService.MutateAdGroupAds(
customerId.ToString(), new AdGroupAdOperation[] { new AdGroupAdOperation() {
Create = adGroupAd
}}
);
string adGroupAdResourceName = response.Results[0].ResourceName;
Console.WriteLine("Ad group ad with resource name = '{0}' was added.",
adGroupAdResourceName);
}
// [END add_things_to_do_ad_3]
}
}