/
AddCampaignGroupsAndPerformanceTargets.cs
executable file
·267 lines (239 loc) · 10.7 KB
/
AddCampaignGroupsAndPerformanceTargets.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 2018 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 Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201809;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
/// <summary>
/// This code example adds a campaign group and sets a performance target for that group. To
/// get campaigns, run GetCampaigns.cs. To download reports, run
/// DownloadCriteriaReportWithAwql.cs.
/// </summary>
public class AddCampaignGroupsAndPerformanceTargets : ExampleBase
{
/// <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)
{
AddCampaignGroupsAndPerformanceTargets codeExample =
new AddCampaignGroupsAndPerformanceTargets();
Console.WriteLine(codeExample.Description);
try
{
long campaignId1 = long.Parse("INSERT_CAMPAIGN_ID1_HERE");
long campaignId2 = long.Parse("INSERT_CAMPAIGN_ID2_HERE");
codeExample.Run(new AdWordsUser(), campaignId1, campaignId2);
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while running this code example. {0}",
ExampleUtilities.FormatException(e));
}
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description
{
get
{
return
"This code example adds a campaign group and sets a performance target for " +
"that group. To get campaigns, run GetCampaigns.cs. To download reports, run " +
"DownloadCriteriaReportWithAwql.cs.";
}
}
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="campaignId1">Id of the campaign to be added to the campaign group.</param>
/// <param name="campaignId2">Id of the campaign to be added to the campaign group.</param>
public void Run(AdWordsUser user, long campaignId1, long campaignId2)
{
CampaignGroup campaignGroup = CreateCampaignGroup(user);
AddCampaignsToGroup(user, campaignGroup.id, new long[]
{
campaignId1,
campaignId2
});
CreatePerformanceTarget(user, campaignGroup.id);
Console.WriteLine("Campaign group and its performance target were setup successfully.");
}
/// <summary>
/// Create a campaign group.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <returns>The campaign group.</returns>
private static CampaignGroup CreateCampaignGroup(AdWordsUser user)
{
using (CampaignGroupService campaignGroupService =
(CampaignGroupService) user.GetService(AdWordsService.v201809.CampaignGroupService))
{
// Create the campaign group.
CampaignGroup campaignGroup = new CampaignGroup
{
name = "Mars campaign group - " + ExampleUtilities.GetShortRandomString()
};
// Create the operation.
CampaignGroupOperation operation = new CampaignGroupOperation
{
operand = campaignGroup,
@operator = Operator.ADD
};
try
{
CampaignGroupReturnValue retval = campaignGroupService.mutate(
new CampaignGroupOperation[]
{
operation
});
// Display the results.
CampaignGroup newCampaignGroup = retval.value[0];
Console.WriteLine(
"Campaign group with ID = '{0}' and name = '{1}' was created.",
newCampaignGroup.id, newCampaignGroup.name);
return newCampaignGroup;
}
catch (Exception e)
{
throw new System.ApplicationException("Failed to add campaign group.", e);
}
}
}
/// <summary>
/// Adds multiple campaigns to a campaign group.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="campaignGroupId">The campaign group ID.</param>
/// <param name="campaignIds">IDs of the campaigns to be added to the campaign group.
/// </param>
private static void AddCampaignsToGroup(AdWordsUser user, long campaignGroupId,
long[] campaignIds)
{
using (CampaignService campaignService =
(CampaignService) user.GetService(AdWordsService.v201809.CampaignService))
{
List<CampaignOperation> operations = new List<CampaignOperation>();
for (int i = 0; i < campaignIds.Length; i++)
{
Campaign campaign = new Campaign
{
id = campaignIds[i],
campaignGroupId = campaignGroupId
};
CampaignOperation operation = new CampaignOperation
{
operand = campaign,
@operator = Operator.SET
};
operations.Add(operation);
}
try
{
CampaignReturnValue retval = campaignService.mutate(operations.ToArray());
List<long> updatedCampaignIds = new List<long>();
retval.value.ToList().ForEach(updatedCampaign =>
updatedCampaignIds.Add(updatedCampaign.id));
// Display the results.
Console.WriteLine(
"The following campaign IDs were added to the campaign group " +
"with ID '{0}':\n\t{1}'", campaignGroupId,
string.Join(", ", updatedCampaignIds));
}
catch (Exception e)
{
throw new System.ApplicationException(
"Failed to add campaigns to campaign group.", e);
}
}
}
/// <summary>
/// Creates a performance target for the campaign group.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="campaignGroupId">Campaign group ID.</param>
/// <returns>The newly created performance target.</returns>
private static CampaignGroupPerformanceTarget CreatePerformanceTarget(AdWordsUser user,
long campaignGroupId)
{
using (CampaignGroupPerformanceTargetService campaignGroupPerformanceTargetService =
(CampaignGroupPerformanceTargetService) user.GetService(AdWordsService.v201809
.CampaignGroupPerformanceTargetService))
{
// Create the performance target.
CampaignGroupPerformanceTarget campaignGroupPerformanceTarget =
new CampaignGroupPerformanceTarget
{
campaignGroupId = campaignGroupId
};
PerformanceTarget performanceTarget = new PerformanceTarget
{
// Keep the CPC for the campaigns < $3.
efficiencyTargetType = EfficiencyTargetType.CPC_LESS_THAN_OR_EQUAL_TO,
efficiencyTargetValue = 3000000,
// Keep the maximum spend under $50.
spendTargetType = SpendTargetType.MAXIMUM
};
Money maxSpend = new Money
{
microAmount = 500000000
};
performanceTarget.spendTarget = maxSpend;
// Aim for at least 3000 clicks.
performanceTarget.volumeTargetValue = 3000;
performanceTarget.volumeGoalType = VolumeGoalType.MAXIMIZE_CLICKS;
// Start the performance target today, and run it for the next 90 days.
System.DateTime startDate = System.DateTime.Now;
System.DateTime endDate = startDate.AddDays(90);
performanceTarget.startDate = startDate.ToString("yyyyMMdd");
performanceTarget.endDate = endDate.ToString("yyyyMMdd");
campaignGroupPerformanceTarget.performanceTarget = performanceTarget;
// Create the operation.
CampaignGroupPerformanceTargetOperation operation =
new CampaignGroupPerformanceTargetOperation
{
operand = campaignGroupPerformanceTarget,
@operator = Operator.ADD
};
try
{
CampaignGroupPerformanceTargetReturnValue retval =
campaignGroupPerformanceTargetService.mutate(
new CampaignGroupPerformanceTargetOperation[]
{
operation
});
// Display the results.
CampaignGroupPerformanceTarget newCampaignPerfTarget = retval.value[0];
Console.WriteLine(
"Campaign performance target with id = '{0}' was added for " +
"campaign group ID '{1}'.", newCampaignPerfTarget.id,
newCampaignPerfTarget.campaignGroupId);
return newCampaignPerfTarget;
}
catch (Exception e)
{
throw new System.ApplicationException(
"Failed to create campaign performance target.", e);
}
}
}
}
}