-
Notifications
You must be signed in to change notification settings - Fork 199
/
FindAndRemoveCriteriaFromSharedSet.cs
executable file
·291 lines (264 loc) · 12 KB
/
FindAndRemoveCriteriaFromSharedSet.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
// 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;
namespace Google.Api.Ads.AdWords.Examples.CSharp.v201809
{
/// <summary>
/// This code example demonstrates how to find and remove shared sets and
/// shared set criteria.
/// </summary>
public class FindAndRemoveCriteriaFromSharedSet : ExampleBase
{
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description
{
get
{
return "This code example demonstrates how to find and remove shared sets and " +
"shared set criteria.";
}
}
/// <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)
{
FindAndRemoveCriteriaFromSharedSet codeExample =
new FindAndRemoveCriteriaFromSharedSet();
Console.WriteLine(codeExample.Description);
try
{
long campaignId = long.Parse("INSERT_CAMPAIGN_ID_HERE");
codeExample.Run(new AdWordsUser(), campaignId);
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while running this code example. {0}",
ExampleUtilities.FormatException(e));
}
}
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="campaignId">Id of the campaign from which items shared
/// criteria are removed.</param>
public void Run(AdWordsUser user, long campaignId)
{
// Get the list of shared sets that are attached to the campaign.
List<string> sharedSetIds = GetSharedSetIds(user, campaignId);
// Get the shared criteria in those shared sets.
List<SharedCriterion> sharedCriteria = GetSharedCriteria(user, sharedSetIds);
// Remove the shared criteria from the shared sets.
RemoveSharedCriteria(user, sharedCriteria);
}
/// <summary>
/// Gets the shared set IDs associated with a campaign.
/// </summary>
/// <param name="user">The user that owns the campaign.</param>
/// <param name="campaignId">The campaign identifier.</param>
/// <returns>The list of shared set IDs associated with the campaign.</returns>
private List<string> GetSharedSetIds(AdWordsUser user, long campaignId)
{
using (CampaignSharedSetService campaignSharedSetService =
(CampaignSharedSetService) user.GetService(AdWordsService.v201809
.CampaignSharedSetService))
{
Selector selector = new Selector()
{
fields = new string[]
{
CampaignSharedSet.Fields.SharedSetId,
CampaignSharedSet.Fields.CampaignId,
CampaignSharedSet.Fields.SharedSetName,
CampaignSharedSet.Fields.SharedSetType
},
predicates = new Predicate[]
{
Predicate.Equals(CampaignSharedSet.Fields.CampaignId, campaignId),
Predicate.In(CampaignSharedSet.Fields.SharedSetType, new string[]
{
SharedSetType.NEGATIVE_KEYWORDS.ToString()
}),
},
paging = Paging.Default,
};
List<string> sharedSetIds = new List<string>();
CampaignSharedSetPage page = new CampaignSharedSetPage();
try
{
do
{
// Get the campaigns.
page = campaignSharedSetService.get(selector);
// Display the results.
if (page != null && page.entries != null)
{
int i = selector.paging.startIndex;
foreach (CampaignSharedSet campaignSharedSet in page.entries)
{
sharedSetIds.Add(campaignSharedSet.sharedSetId.ToString());
Console.WriteLine(
"{0}) Campaign shared set ID {1} and name '{2}' found for " +
"campaign ID {3}.\n", i + 1, campaignSharedSet.sharedSetId,
campaignSharedSet.sharedSetName, campaignSharedSet.campaignId);
i++;
}
}
selector.paging.IncreaseOffset();
} while (selector.paging.startIndex < page.totalNumEntries);
return sharedSetIds;
}
catch (Exception e)
{
throw new Exception("Failed to get shared set ids for campaign.", e);
}
}
}
/// <summary>
/// Gets the shared criteria in a shared set.
/// </summary>
/// <param name="user">The user that owns the shared set.</param>
/// <param name="sharedSetIds">The shared criteria IDs.</param>
/// <returns>The list of shared criteria.</returns>
private List<SharedCriterion> GetSharedCriteria(AdWordsUser user, List<string> sharedSetIds)
{
using (SharedCriterionService sharedCriterionService =
(SharedCriterionService) user.GetService(AdWordsService.v201809
.SharedCriterionService))
{
Selector selector = new Selector()
{
fields = new string[]
{
SharedSet.Fields.SharedSetId,
Criterion.Fields.Id,
Keyword.Fields.KeywordText,
Keyword.Fields.KeywordMatchType,
Placement.Fields.PlacementUrl
},
predicates = new Predicate[]
{
Predicate.In(SharedSet.Fields.SharedSetId, sharedSetIds)
},
paging = Paging.Default
};
List<SharedCriterion> sharedCriteria = new List<SharedCriterion>();
SharedCriterionPage page = new SharedCriterionPage();
try
{
do
{
// Get the campaigns.
page = sharedCriterionService.get(selector);
// Display the results.
if (page != null && page.entries != null)
{
int i = selector.paging.startIndex;
foreach (SharedCriterion sharedCriterion in page.entries)
{
switch (sharedCriterion.criterion.type)
{
case CriterionType.KEYWORD:
Keyword keyword = (Keyword) sharedCriterion.criterion;
Console.WriteLine(
"{0}) Shared negative keyword with ID {1} and " +
"text '{2}' was found.", i + 1, keyword.id,
keyword.text);
break;
case CriterionType.PLACEMENT:
Placement placement = (Placement) sharedCriterion.criterion;
Console.WriteLine(
"{0}) Shared negative placement with ID {1} and " +
"URL '{2}' was found.", i + 1, placement.id,
placement.url);
break;
default:
Console.WriteLine(
"{0}) Shared criteria with ID {1} was found.", i + 1,
sharedCriterion.criterion.id);
break;
}
i++;
sharedCriteria.Add(sharedCriterion);
}
}
selector.paging.IncreaseOffset();
} while (selector.paging.startIndex < page.totalNumEntries);
return sharedCriteria;
}
catch (Exception e)
{
throw new Exception("Failed to get shared criteria.", e);
}
}
}
/// <summary>
/// Removes a list of shared criteria.
/// </summary>
/// <param name="user">The user that owns the shared criteria.</param>
/// <param name="sharedCriteria">The list shared criteria to be removed.</param>
private void RemoveSharedCriteria(AdWordsUser user, List<SharedCriterion> sharedCriteria)
{
if (sharedCriteria.Count == 0)
{
Console.WriteLine("No shared criteria to remove.");
return;
}
using (SharedCriterionService sharedCriterionService =
(SharedCriterionService) user.GetService(AdWordsService.v201809
.SharedCriterionService))
{
List<SharedCriterionOperation> operations = new List<SharedCriterionOperation>();
foreach (SharedCriterion sharedCriterion in sharedCriteria)
{
operations.Add(new SharedCriterionOperation()
{
@operator = Operator.REMOVE,
operand = new SharedCriterion()
{
sharedSetId = sharedCriterion.sharedSetId,
criterion = new Criterion()
{
id = sharedCriterion.criterion.id
}
}
});
}
try
{
SharedCriterionReturnValue sharedCriterionReturnValue =
sharedCriterionService.mutate(operations.ToArray());
foreach (SharedCriterion removedCriterion in sharedCriterionReturnValue.value)
{
Console.WriteLine(
"Shared criterion ID {0} was successfully removed from shared " +
"set ID {1}.", removedCriterion.criterion.id,
removedCriterion.sharedSetId);
}
}
catch (Exception e)
{
throw new Exception("Failed to remove shared criteria.", e);
}
}
}
}
}