-
Notifications
You must be signed in to change notification settings - Fork 95
/
UploadCallConversion.cs
246 lines (224 loc) · 11.2 KB
/
UploadCallConversion.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
// 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.V16.Common;
using Google.Ads.GoogleAds.V16.Errors;
using Google.Ads.GoogleAds.V16.Services;
using System;
using System.Collections.Generic;
using static Google.Ads.GoogleAds.V16.Enums.ConsentStatusEnum.Types;
namespace Google.Ads.GoogleAds.Examples.V16
{
/// <summary>
/// This code example imports offline call conversion values for calls related to the
/// ads in your account. To set up a conversion action, run the AddConversionAction.cs example.
/// </summary>
public class UploadCallConversion : ExampleBase
{
/// <summary>
/// Command line options for running the <see cref="UploadCallConversion"/> example.
/// </summary>
public class Options : OptionsBase
{
/// <summary>
/// The Google Ads customer ID for whom the conversion will be imported.
/// </summary>
[Option("customerId", Required = true, HelpText =
"The Google Ads customer ID for whom the conversion will be imported.")]
public long CustomerId { get; set; }
/// <summary>
/// The conversion action ID.
/// </summary>
[Option("conversionActionId", Required = true, HelpText =
"The conversion action ID.")]
public long ConversionActionId { get; set; }
/// <summary>
/// The caller ID in E.164 format with preceding '+' sign. e.g. "+18005550100".
/// </summary>
[Option("callerId", Required = true, HelpText =
"The caller ID in E.164 format with preceding '+' sign. e.g. '+18005550100'.")]
public string CallerId { get; set; }
/// <summary>
/// The call start time in 'yyyy-mm-dd hh:mm:ss +|-hh:mm' format.
/// </summary>
[Option("callStartTime", Required = true, HelpText =
"The call start time in 'yyyy-mm-dd hh:mm:ss+|-hh:mm' format.")]
public string CallStartTime { get; set; }
/// <summary>
/// The call conversion time.
/// </summary>
[Option("conversionTime", Required = true, HelpText =
"The conversion time in 'yyyy-mm-dd hh:mm:ss+|-hh:mm' format.")]
public string ConversionTime { get; set; }
/// <summary>
/// The conversion value.
/// </summary>
[Option("conversionValue", Required = true, HelpText =
"The conversion value.")]
public double ConversionValue { get; set; }
/// <summary>
/// The ID of the conversion custom variable to associate with the upload.
/// </summary>
[Option("conversionCustomVariableId", Required = false, HelpText =
"The ID of the conversion custom variable to associate with the upload.")]
public long? ConversionCustomVariableId { get; set; }
/// <summary>
/// The value of the conversion custom variable to associate with the upload.
/// </summary>
[Option("conversionCustomVariableValue", Required = false, HelpText =
"The value of the conversion custom variable to associate with the upload.")]
public string ConversionCustomVariableValue { get; set; }
/// <summary>
/// The consent status for ad user data.
/// </summary>
[Option("adUserDataConsent", Required = false, HelpText =
"The consent status for ad user data.")]
public ConsentStatus? AdUserDataConsent { 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);
UploadCallConversion codeExample = new UploadCallConversion();
Console.WriteLine(codeExample.Description);
codeExample.Run(new GoogleAdsClient(), options.CustomerId,
options.ConversionActionId, options.CallerId, options.CallStartTime,
options.ConversionTime, options.ConversionValue,
options.ConversionCustomVariableId, options.ConversionCustomVariableValue,
options.AdUserDataConsent);
}
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description =>
"This code example imports offline call conversion values for calls related to the " +
"ads in your account. To set up a conversion action, run the AddConversionAction.cs " +
"example.";
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID for whom the conversion will
/// be imported.</param>
/// <param name="callerId">The caller ID in E.164 format with preceding '+' sign. e.g.
/// "+18005550100".</param>
/// <param name="conversionActionId">The ID of the conversion action</param>
/// <param name="callStartTime">The call start time in "yyyy-mm-dd hh:mm:ss+|-hh:mm"
/// format.</param>
/// <param name="conversionTime">The conversion time in "yyyy-mm-dd hh:mm:ss+|-hh:mm"
/// format.</param>
/// <param name="conversionValue">The conversion value.</param>
/// <param name="conversionCustomVariableId">The ID of the conversion custom variable to
/// associate with the upload.</param>
/// <param name="conversionCustomVariableValue">The value of the conversion custom variable
/// to associate with the upload.</param>
/// <param name="adUserDataConsent">The consent status for ad user data.</param>
// [START upload_call_conversion]
public void Run(GoogleAdsClient client, long customerId,
long conversionActionId, string callerId, string callStartTime,
string conversionTime, double conversionValue,
long? conversionCustomVariableId, string conversionCustomVariableValue,
ConsentStatus? adUserDataConsent)
{
// Get the ConversionUploadService.
ConversionUploadServiceClient conversionUploadService =
client.GetService(Services.V16.ConversionUploadService);
// Create a call conversion by specifying currency as USD.
CallConversion callConversion = new CallConversion()
{
ConversionAction = ResourceNames.ConversionAction(customerId, conversionActionId),
CallerId = callerId,
CallStartDateTime = callStartTime,
ConversionDateTime = conversionTime,
ConversionValue = conversionValue,
CurrencyCode = "USD",
};
if (adUserDataConsent != null)
{
// Specifies whether user consent was obtained for the data you are uploading. See
// https://www.google.com/about/company/user-consent-policy
// for details.
callConversion.Consent = new Consent()
{
AdUserData = (ConsentStatus)adUserDataConsent
};
}
if (conversionCustomVariableId != null &&
!string.IsNullOrEmpty(conversionCustomVariableValue))
{
callConversion.CustomVariables.Add(new CustomVariable()
{
ConversionCustomVariable = ResourceNames.ConversionCustomVariable(
customerId, conversionCustomVariableId.Value),
Value = conversionCustomVariableValue
});
}
UploadCallConversionsRequest request = new UploadCallConversionsRequest()
{
CustomerId = customerId.ToString(),
Conversions = { callConversion },
PartialFailure = true
};
try
{
// Issues a request to upload the call conversion. The partialFailure parameter
// is set to true, and validateOnly parameter to false as required by this method
// call.
// NOTE: This request contains a single conversion as a demonstration. However, if
// you have multiple conversions to upload, it's best to upload multiple conversions
// per request instead of sending a separate request per conversion. See the
// following for per-request limits:
// https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service
UploadCallConversionsResponse response =
conversionUploadService.UploadCallConversions(request);
// Since we set partialFailure = true, we can retrieve error messages (if any) from
// the operation response.
if (response.PartialFailureError != null)
{
Console.WriteLine("Call conversion upload failed.");
// Retrieves the errors from the partial failure and prints them.
List<GoogleAdsError> errors =
response.PartialFailure.GetErrorsByOperationIndex(0);
foreach (GoogleAdsError error in errors)
{
Console.WriteLine($"Operation failed with error: {error}.");
}
}
else
{
// Prints the result.
CallConversionResult uploadedCallConversion = response.Results[0];
Console.WriteLine($"Uploaded call conversion that occurred at " +
$"'{uploadedCallConversion.CallStartDateTime}' for caller ID " +
$"'{uploadedCallConversion.CallerId}' to the conversion action with " +
$"resource name '{uploadedCallConversion.ConversionAction}'.");
}
}
catch (GoogleAdsException e)
{
Console.WriteLine("Failure:");
Console.WriteLine($"Message: {e.Message}");
Console.WriteLine($"Failure: {e.Failure}");
Console.WriteLine($"Request ID: {e.RequestId}");
throw;
}
}
// [END upload_call_conversion]
}
}