/
HandleExpandedTextAdPolicyViolations.cs
228 lines (210 loc) · 10.2 KB
/
HandleExpandedTextAdPolicyViolations.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
// Copyright 2020 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.V10.Common;
using Google.Ads.GoogleAds.V10.Errors;
using Google.Ads.GoogleAds.V10.Resources;
using Google.Ads.GoogleAds.V10.Services;
using System;
using System.Collections.Generic;
using static Google.Ads.GoogleAds.V10.Enums.AdGroupAdStatusEnum.Types;
namespace Google.Ads.GoogleAds.Examples.V10
{
/// <summary>
/// This code example demonstrates how to request an exemption for policy violations of an
/// expanded text ad. If the request somehow fails with exceptions that are not policy finding
/// errors, the code example will stop instead of trying to send an exemption request.
/// </summary>
public class HandleExpandedTextAdPolicyViolations : ExampleBase
{
/// <summary>
/// Command line options for running the <see cref="HandleExpandedTextAdPolicyViolations"/>
/// 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>
/// ID of the ad group to which ads are added.
/// </summary>
[Option("adGroupId", Required = true, HelpText =
"ID of the ad group to which ads are added.")]
public long AdGroupId { 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 = new Options();
CommandLine.Parser.Default.ParseArguments<Options>(args).MapResult(
delegate (Options o)
{
options = o;
return 0;
}, delegate (IEnumerable<Error> errors)
{
// The customer ID for which the call is made.
options.CustomerId = long.Parse("INSERT_CUSTOMER_ID_HERE");
// ID of the ad group to which ads are added.
options.AdGroupId = long.Parse("INSERT_AD_GROUP_ID_HERE");
return 0;
});
HandleExpandedTextAdPolicyViolations codeExample =
new HandleExpandedTextAdPolicyViolations();
Console.WriteLine(codeExample.Description);
codeExample.Run(new GoogleAdsClient(), options.CustomerId, options.AdGroupId);
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description =>
"This code example demonstrates how to request an exemption for policy violations of " +
"an expanded text ad. If the request somehow fails with exceptions that are not " +
"policy finding errors, the code example will stop instead of trying to send an " +
"exemption request.";
/// <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>
/// <param name="adGroupId">ID of the ad group to which ads are added.</param>
public void Run(GoogleAdsClient client, long customerId, long adGroupId)
{
// Get the AdGroupAdServiceClient.
AdGroupAdServiceClient adGroupAdService = client.GetService(
Services.V10.AdGroupAdService);
string adGroupResourceName = ResourceNames.AdGroup(customerId, adGroupId);
// Creates an expanded text ad info object.
ExpandedTextAdInfo expandedTextAdInfo = new ExpandedTextAdInfo()
{
HeadlinePart1 = $"Cruise to Mars #{ExampleUtilities.GetShortRandomString()}",
HeadlinePart2 = "Best Space Cruise Line",
// Intentionally use an ad text that violates policy -- having too many exclamation
// marks.
Description = "Buy your tickets now!!!!!!!"
};
// Creates an ad group ad to hold the above ad.
AdGroupAd adGroupAd = new AdGroupAd()
{
AdGroup = adGroupResourceName,
// Set the ad group ad to PAUSED to prevent it from immediately serving.
// Set to ENABLED once you've added targeting and the ad are ready to serve.
Status = AdGroupAdStatus.Paused,
// Sets the expanded text ad info on an Ad.
Ad = new Ad()
{
ExpandedTextAd = expandedTextAdInfo,
FinalUrls = { "http://www.example.com" }
}
};
// Creates an ad group ad operation.
AdGroupAdOperation operation = new AdGroupAdOperation()
{
Create = adGroupAd
};
try
{
try
{
// Try sending a mutate request to add the ad group ad.
adGroupAdService.MutateAdGroupAds(customerId.ToString(), new[] { operation });
}
catch (GoogleAdsException ex)
{
// The request will always fail because of the policy violation in the
// description of the ad.
var ignorablePolicyTopics = FetchIgnorablePolicyTopics(ex);
// Try sending exemption requests for creating an expanded text ad.
RequestExemption(customerId, adGroupAdService, operation, ignorablePolicyTopics);
}
}
catch (GoogleAdsException e)
{
Console.WriteLine("Failure:");
Console.WriteLine($"Message: {e.Message}");
Console.WriteLine($"Failure: {e.Failure}");
Console.WriteLine($"Request ID: {e.RequestId}");
throw;
}
}
/// <summary>
/// Collects all ignorable policy topics that will be sent for exemption request later.
/// </summary>
/// <param name="ex">The API exception from a previous call to add ad group ads.</param>
/// <returns>The ignorable policy topics</returns>
// [START handle_expanded_text_ad_policy_violations]
private static string[] FetchIgnorablePolicyTopics(GoogleAdsException ex)
{
List<string> ignorablePolicyTopics = new List<string>(); ;
Console.WriteLine("Google Ads failure details:");
foreach (GoogleAdsError error in ex.Failure.Errors)
{
if (error.ErrorCode.ErrorCodeCase != ErrorCode.ErrorCodeOneofCase.PolicyFindingError)
{
throw ex;
}
if (error.Details != null && error.Details.PolicyFindingDetails != null)
{
PolicyFindingDetails details = error.Details.PolicyFindingDetails;
Console.WriteLine($"- Policy finding details:");
foreach (PolicyTopicEntry entry in details.PolicyTopicEntries)
{
ignorablePolicyTopics.Add(entry.Topic);
Console.WriteLine($" - Policy topic name: '{entry.Topic}");
Console.WriteLine($" - Policy topic entry type: '{entry.Type}");
// For the sake of brevity, we exclude printing "policy topic evidences"
// and "policy topic constraints" here. You can fetch those data by
// calling:
// - entry.Evidences
// - entry.Constraints
}
}
}
return ignorablePolicyTopics.ToArray();
}
// [END handle_expanded_text_ad_policy_violations]
/// <summary>
/// Sends exemption requests for creating an expanded text ad.
/// </summary>
/// <param name="customerId">The customer ID for which the call is made.</param>
/// <param name="service">The ad group ad service.</param>
/// <param name="operation">The ad group ad operation to request exemption for.</param>
/// <param name="ignorablePolicyTopics">The ignorable policy topics.</param>
// [START handle_expanded_text_ad_policy_violations_1]
private static void RequestExemption(long customerId, AdGroupAdServiceClient service,
AdGroupAdOperation operation, string[] ignorablePolicyTopics)
{
Console.WriteLine("Try adding an expanded text ad again by requesting exemption for " +
"its policy violations.");
PolicyValidationParameter validationParameter = new PolicyValidationParameter();
validationParameter.IgnorablePolicyTopics.AddRange(ignorablePolicyTopics);
operation.PolicyValidationParameter = validationParameter;
MutateAdGroupAdsResponse response = service.MutateAdGroupAds(
customerId.ToString(), new[] { operation });
Console.WriteLine($"Successfully added an expanded text ad with resource name " +
$"'{response.Results[0].ResourceName}' by requesting for policy violation " +
$"exemption.");
}
// [END handle_expanded_text_ad_policy_violations_1]
}
}