/
CreateNativeCreative.cs
executable file
·195 lines (174 loc) · 7.59 KB
/
CreateNativeCreative.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
// Copyright 2019 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.Common.Util;
using Google.Api.Ads.AdManager.Lib;
using Google.Api.Ads.AdManager.Util.v202205;
using Google.Api.Ads.AdManager.v202205;
using System;
using System.Collections.Generic;
namespace Google.Api.Ads.AdManager.Examples.CSharp.v202205
{
/// <summary>
/// This code example creates a new native creative. To determine which creatives
/// already exist, run GetAllCreatives.cs.
/// </summary>
public class CreateNativeCreative : SampleBase
{
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description
{
get
{
return "This code example creates a new native creative. To determine which " +
"creatives already exist, run GetAllCreatives.cs.";
}
}
/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
public static void Main()
{
CreateNativeCreative codeExample = new CreateNativeCreative();
Console.WriteLine(codeExample.Description);
codeExample.Run(new AdManagerUser());
}
/// <summary>
/// Run the code example.
/// </summary>
public void Run(AdManagerUser user)
{
using (CreativeService creativeService = user.GetService<CreativeService>())
{
// Set the ID of the advertiser (company) that the creative will be
// assigned to.
long advertiserId = long.Parse(_T("INSERT_ADVERTISER_COMPANY_ID_HERE"));
// Use the system defined native app install creative template.
long creativeTemplateId = 10004400L;
// Create a native app install creative for the Pie Noon app.
TemplateCreative nativeAppInstallCreative = new TemplateCreative();
nativeAppInstallCreative.name = String.Format("Native creative #{0}",
new Random().Next(int.MaxValue));
nativeAppInstallCreative.advertiserId = advertiserId;
nativeAppInstallCreative.creativeTemplateId = creativeTemplateId;
nativeAppInstallCreative.destinationUrl =
"https://play.google.com/store/apps/details?id=com.google.fpl.pie_noon";
// Use 1x1 as the size for native creatives.
Size size = new Size();
size.width = 1;
size.height = 1;
size.isAspectRatio = false;
nativeAppInstallCreative.size = size;
List<BaseCreativeTemplateVariableValue> templateVariables =
new List<BaseCreativeTemplateVariableValue>();
// Set the headline.
templateVariables.Add(new StringCreativeTemplateVariableValue()
{
uniqueName = "Headline",
value = "Pie Noon"
});
// Set the body text.
templateVariables.Add(new StringCreativeTemplateVariableValue()
{
uniqueName = "Body",
value = "Try multi-screen mode!"
});
// Set the image asset.
var imageUrl = "https://lh4.ggpht.com/" +
"GIGNKdGHMEHFDw6TM2bgAUDKPQQRIReKZPqEpMeEhZOPYnTdOQGaSpGSEZflIFs0iw=h300";
templateVariables.Add(new AssetCreativeTemplateVariableValue()
{
uniqueName = "Image",
asset = new CreativeAsset()
{
fileName = String.Format("image{0}.png", this.GetTimeStamp()),
assetByteArray =
MediaUtilities.GetAssetDataFromUrl(
imageUrl,
user.Config)
}
});
// Set the price.
templateVariables.Add(new StringCreativeTemplateVariableValue()
{
uniqueName = "Price",
value = "Free"
});
// Set app icon image asset.
var iconUrl = "https://lh6.ggpht.com/" +
"Jzvjne5CLs6fJ1MHF-XeuUfpABzl0YNMlp4RpHnvPRCIj4--eTDwtyouwUDzVVekXw=w300";
templateVariables.Add(new AssetCreativeTemplateVariableValue()
{
uniqueName = "Appicon",
asset = new CreativeAsset()
{
fileName = String.Format("icon{0}.png", this.GetTimeStamp()),
assetByteArray =
MediaUtilities.GetAssetDataFromUrl(
iconUrl,
user.Config)
}
});
// Set the call to action text.
templateVariables.Add(new StringCreativeTemplateVariableValue()
{
uniqueName = "Calltoaction",
value = "Install"
});
// Set the star rating.
templateVariables.Add(new StringCreativeTemplateVariableValue()
{
uniqueName = "Starrating",
value = "4"
});
// Set the store type.
templateVariables.Add(new StringCreativeTemplateVariableValue()
{
uniqueName = "Store",
value = "Google Play"
});
// Set the deep link URL.
templateVariables.Add(new UrlCreativeTemplateVariableValue()
{
uniqueName = "DeeplinkclickactionURL",
value = "market://details?id=com.google.fpl.pie_noon"
});
nativeAppInstallCreative.creativeTemplateVariableValues =
templateVariables.ToArray();
try
{
// Create the native creative on the server.
Creative[] createdNativeCreatives = creativeService.createCreatives(
new Creative[]
{
nativeAppInstallCreative
});
foreach (Creative createdNativeCreative in createdNativeCreatives)
{
Console.WriteLine(
"A native creative with ID \"{0}\" and name \"{1}\" " +
"was created and can be previewed at {2}", createdNativeCreative.id,
createdNativeCreative.name, createdNativeCreative.previewUrl);
}
}
catch (Exception e)
{
Console.WriteLine("Failed to create creatives. Exception says \"{0}\"",
e.Message);
}
}
}
}
}