-
Notifications
You must be signed in to change notification settings - Fork 199
/
AddExpandedTextAdWithUpgradedUrls.cs
executable file
·157 lines (133 loc) · 6.2 KB
/
AddExpandedTextAdWithUpgradedUrls.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
// 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.v201806;
using System;
using System.Collections.Generic;
namespace Google.Api.Ads.AdWords.Examples.CSharp.v201806 {
/// <summary>
/// This code example adds an expanded text ad that uses advanced features
/// of upgraded URLs.
/// </summary>
public class AddExpandedTextAdWithUpgradedUrls : 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) {
AddExpandedTextAdWithUpgradedUrls codeExample = new AddExpandedTextAdWithUpgradedUrls();
Console.WriteLine(codeExample.Description);
try {
long adGroupId = long.Parse("INSERT_ADGROUP_ID_HERE");
codeExample.Run(new AdWordsUser(), adGroupId);
} 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 an expanded text ad that uses advanced features of " +
"upgraded URLs.";
}
}
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="user">The AdWords user.</param>
/// <param name="adGroupId">ID of the ad group to which ad is added.
/// </param>
public void Run(AdWordsUser user, long adGroupId) {
using (AdGroupAdService adGroupAdService =
(AdGroupAdService) user.GetService(AdWordsService.v201806.AdGroupAdService)) {
// Create the expanded text ad.
ExpandedTextAd expandedTextAd = new ExpandedTextAd() {
headlinePart1 = "Luxury Cruise to Mars",
headlinePart2 = "Visit the Red Planet in style.",
description = "Low-gravity fun for everyone!",
};
// Specify a tracking URL for 3rd party tracking provider. You may
// specify one at customer, campaign, ad group, ad, criterion or
// feed item levels.
expandedTextAd.trackingUrlTemplate =
"http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}";
// Since your tracking URL has two custom parameters, provide their
// values too. This can be provided at campaign, ad group, ad, criterion
// or feed item levels.
CustomParameter seasonParameter = new CustomParameter();
seasonParameter.key = "season";
seasonParameter.value = "christmas";
CustomParameter promoCodeParameter = new CustomParameter();
promoCodeParameter.key = "promocode";
promoCodeParameter.value = "NYC123";
expandedTextAd.urlCustomParameters = new CustomParameters();
expandedTextAd.urlCustomParameters.parameters =
new CustomParameter[] { seasonParameter, promoCodeParameter };
// Specify a list of final URLs. This field cannot be set if URL field is
// set. This may be specified at ad, criterion and feed item levels.
expandedTextAd.finalUrls = new string[] {
"http://www.example.com/cruise/space/",
"http://www.example.com/locations/mars/"
};
// Specify a list of final mobile URLs. This field cannot be set if URL
// field is set, or finalUrls is unset. This may be specified at ad,
// criterion and feed item levels.
expandedTextAd.finalMobileUrls = new string[] {
"http://mobile.example.com/cruise/space/",
"http://mobile.example.com/locations/mars/"
};
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.adGroupId = adGroupId;
adGroupAd.ad = expandedTextAd;
// Optional: Set the status.
adGroupAd.status = AdGroupAdStatus.PAUSED;
// Create the operation.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.@operator = Operator.ADD;
operation.operand = adGroupAd;
AdGroupAdReturnValue retVal = null;
try {
// Create the ads.
retVal = adGroupAdService.mutate(new AdGroupAdOperation[] { operation });
// Display the results.
if (retVal != null && retVal.value != null) {
ExpandedTextAd newExpandedTextAd = retVal.value[0].ad as ExpandedTextAd;
Console.WriteLine("Expanded text ad with ID '{0}' and headline '{1} - {2}' was added.",
newExpandedTextAd.id, newExpandedTextAd.headlinePart1,
newExpandedTextAd.headlinePart2);
Console.WriteLine("Upgraded URL properties:");
Console.WriteLine(" Final URLs: {0}", string.Join(", ", newExpandedTextAd.finalUrls));
Console.WriteLine(" Final Mobile URLs: {0}",
string.Join(", ", newExpandedTextAd.finalMobileUrls));
Console.WriteLine(" Tracking URL template: {0}",
newExpandedTextAd.trackingUrlTemplate);
List<string> parameters = new List<string>();
foreach (CustomParameter customParam in
newExpandedTextAd.urlCustomParameters.parameters) {
parameters.Add(string.Format("{0}={1}", customParam.key, customParam.value));
}
Console.WriteLine(" Custom parameters: {0}", string.Join(", ", parameters.ToArray()));
} else {
Console.WriteLine("No expanded text ads were created.");
}
} catch (Exception e) {
throw new System.ApplicationException("Failed to create expanded text ad.", e);
}
}
}
}
}