My favorites | Sign in
Google
             
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
// Copyright 2009, Google Inc. All Rights Reserved.
//
// 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.

/**
* This code sample creates a new local business ad given an existing ad group.
* To create an ad group, you can run AddAdGroup.cs.
*/

using SampleCode.com.google.sandbox.v13.AdService;

using System;
using System.Drawing;
using System.IO;

namespace SampleCode.v13 {
public class AddLocalBusinessAd {
// Provide AdWords login information.
private static String email = "INSERT_LOGIN_EMAIL_HERE";
private static String password = "INSERT_PASSWORD_HERE";
private static String clientEmail = "INSERT_CLIENT_LOGIN_EMAIL_HERE";
private static String useragent =
"INSERT_COMPANY_NAME: AdWords API DotNet Sample Code";
private static String developerToken = "INSERT_DEVELOPER_TOKEN_HERE";
private static String applicationToken =
"INSERT_APPLICATION_TOKEN_HERE";

public static void Main() {
try {
// Set up service connection.
AdService service = new AdService();

// Define SOAP headers.
service.emailValue = new email();
service.emailValue.Text = new String[] {email};
service.passwordValue = new password();
service.passwordValue.Text = new String[] {password};
service.clientEmailValue = new clientEmail();
service.clientEmailValue.Text = new String[] {clientEmail};
service.useragentValue = new useragent();
service.useragentValue.Text = new String[] {useragent};
service.developerTokenValue = new developerToken();
service.developerTokenValue.Text = new String[] {developerToken};
service.applicationTokenValue = new applicationToken();
service.applicationTokenValue.Text = new String[] {applicationToken};

// Find similar businesses in Local Business Center.
String businessName = "Domino's Pizza";
String businessAddress = "89 Charlwood St, London, SW1V 4PB";
String businessCountryCode = "GB";
int inLocalBusinessCenter = 0;

// Get business from Local Business Center or find similar business.
Business[] businesses = {};
if (inLocalBusinessCenter == 1) {
businesses = service.getMyBusinesses();
} else {
businesses = service.findBusinesses(
businessName, businessAddress, businessCountryCode);
}

// Get business key.
String businessKey = null;
for (int i = 0; i < businesses.Length; i++) {
String name = businesses[i].name;
String address = businesses[i].address;
String countryCode = businesses[i].countryCode;

if (businessName.IndexOf(name) > -1 ||
businessAddress.IndexOf(address) > -1 ||
businessCountryCode.IndexOf(countryCode) > -1) {
businessKey = businesses[i].key;
}
}

// Create new local business ad structure.
if (businessKey != null) {
long adGroupId = long.Parse("INSERT_AD_GROUP_ID_HERE");

FileStream fs = File.OpenRead("INSERT_BUSINESS_IMAGE_PATH_HERE");
byte[] localBusinessImageData = new byte[fs.Length];
fs.Read(localBusinessImageData, 0, localBusinessImageData.Length);
fs.Close();

fs = File.OpenRead("INSERT_CUSTOM_ICON_PATH_HERE");
byte[] localBusinessIconData = new byte[fs.Length];
fs.Read(localBusinessIconData, 0, localBusinessIconData.Length);
fs.Close();

LocalBusinessAd localBusinessAd = new LocalBusinessAd();
localBusinessAd.adGroupId = adGroupId;
localBusinessAd.adType = AdType.LocalBusinessAd;
localBusinessAd.adTypeSpecified = true;

SampleCode.com.google.sandbox.v13.AdService.Image businessImage =
new SampleCode.com.google.sandbox.v13.AdService.Image();
businessImage.data = localBusinessImageData;
localBusinessAd.businessImage = businessImage;

SampleCode.com.google.sandbox.v13.AdService.Image customImage =
new SampleCode.com.google.sandbox.v13.AdService.Image();
customImage.data = localBusinessIconData;
localBusinessAd.customIcon = customImage;

localBusinessAd.businessKey = businessKey;
localBusinessAd.countryCode = "GB";
localBusinessAd.description1 = "Choose from our delicious range now";
localBusinessAd.description2 = "Pre-order or delivered to your door";
localBusinessAd.destinationUrl = "http://www.dominos.co.uk";
localBusinessAd.displayUrl = "www.dominos.co.uk";

// Check new ad for policy violations before adding it.
String[] languageTarget = {"en"};
GeoTarget geoTarget = new GeoTarget();
CountryTargets countryTargets = new CountryTargets();
countryTargets.countries = new String[] {"GB"};
geoTarget.countryTargets = countryTargets;
ApiError[] errors = service.checkAds(new Ad[] {localBusinessAd},
languageTarget, geoTarget);

// Add local business ad if there are no policy violations.
if (errors == null) {
Ad[] ads = service.addAds(new Ad[] {localBusinessAd});

// Display new local business ad.
for (int i = 0; i < ads.Length; i++) {
localBusinessAd = (LocalBusinessAd) ads[i];
Console.WriteLine("New local business ad with name \""
+ localBusinessAd.businessName + "\" and id \""
+ localBusinessAd.id + "\" was created.");
}
} else {
Console.WriteLine("New local business ad was not created due to the"
+ " following policy violations:");
for (int i = 0; i < errors.Length; i++) {
Console.WriteLine(" Detail: " + errors[i].detail
+ " isExemptable: " + errors[i].isExemptable);
}
}
} else {
Console.WriteLine("New local business ad was not created because "
+ "business key was not found.");
}
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
}
Show details Hide details

Change log

r25 by api.anash on Mar 05, 2009   Diff
Removed support for v12, made v13 the
default version (Merge with r23)
Go to: 
Project members, sign in to write a code review

Older revisions

r24 by api.anash on Mar 05, 2009   Diff
Rolling back to Revision 22
r23 by api.anash on Mar 05, 2009   Diff
Removed support for v12, made v13 the
default version.
r9 by api.anash on Nov 21, 2008   Diff
Old v12 files, for the documentation
to work.
All revisions of this file

File info

Size: 6740 bytes, 159 lines