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
#!/usr/bin/perl
#
# 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 text ad given an existing ad group. To create
# an ad group, you can add_ad_group.pl.

use strict;
use warnings;
use English '-no_match_vars';
use SOAP::Lite;

binmode(STDOUT, ':utf8');

# Provide AdWords login information.
my $email = 'INSERT_LOGIN_EMAIL_HERE';
my $password = 'INSERT_PASSWORD_HERE';
my $client_email = 'INSERT_CLIENT_LOGIN_EMAIL_HERE';
my $useragent = 'INSERT_COMPANY_NAME_HERE: AdWords API Perl Sample Code';
my $developer_token = 'INSERT_DEVELOPER_TOKEN_HERE';
my $application_token = 'INSERT_APPLICATION_TOKEN_HERE';

# Set up service connection with autotyping disabled and fault handler
# registered. To send requests to production environment, replace "sandbox" with
# "adwords". To view XML request/response, uncomment
# "SOAP::Lite->import(+trace => 'debug');".
my $url = sprintf('https://%s.google.com/api/adwords/v13/AdService',
'sandbox');
my $wsdl = $url . '?wsdl';
my $service = SOAP::Lite->service($wsdl)->autotype(0)->readable(1)->proxy($url);
$service->on_fault(sub {
my $response = $ARG[1];
die('The following SOAP fault occurred:', "\n",
' faultcode: ', $response->faultcode(), "\n",
' faultstring: ', $response->faultstring(), "\n")
});
# SOAP::Lite->import(+trace => 'debug');

# Define SOAP headers.
my @headers = (
SOAP::Header->name('email' => $email),
SOAP::Header->name('password' => $password),
SOAP::Header->name('clientEmail' => $client_email),
SOAP::Header->name('useragent' => $useragent),
SOAP::Header->name('developerToken' => $developer_token),
SOAP::Header->name('applicationToken' => $application_token)
);

# Create new text ad structure.
my $ad_group_id = 'INSERT_AD_GROUP_ID_HERE';
my $ad = {
'adGroupId' => $ad_group_id,
'adType' => 'TextAd',
'headline' => 'Luxury Cruise to Mars',
'description1' => 'Visit the Red Planet in style.',
'description2' => 'Low-gravity fun for everyone!',
'displayUrl' => 'www.example.com',
'destinationUrl' => 'http://www.example.com'
};

# Check text ad for policy violations before adding it.
my $ads = SOAP::Data->name('ads' => [$ad]);
my $language_target = SOAP::Data->name('languageTarget' => {
'languages' => ['en']});
my $geo_target = SOAP::Data->name('geoTarget' => {'countryTargets' => {
'countries' => ['US']}});
my $response = $service->call('checkAds' => $ads, $language_target, $geo_target,
@headers);
my @errors = ($response->result(), $response->paramsout());

# Add text ad if there are no policy violations.
if (!@errors) {
$response = $service->call('addAds' => $ads, @headers);
my @ads = ($response->result(), $response->paramsout());

# Display new text ad.
foreach $ad (@ads) {
print('New text ad with headline "', $ad->{'headline'}, '" and ID "',
$ad->{'id'}, '" was created.', "\n");
}
} else {
# Display policy violations.
print('New text ad was not created due to the following policy violations:',
"\n");

foreach my $error (@errors) {
print(' detail: ', $error->{'detail'}, "\n",
' isExemptable: ', $error->{'isExemptable'}, "\n");
}
}
Show details Hide details

Change log

r5 by api.jeffy on Mar 05, 2009   Diff
Updated code samples for v13 of the
AdWords API.
Go to: 
Project members, sign in to write a code review

Older revisions

r4 by api.jeffy on Aug 12, 2008   Diff
Updates for v12 of the API, and
additions of code samples that were
previously not written.
r2 by api.jeffy on Mar 13, 2008   Diff
Initial release of the Perl sample
AdWords API code. More samples to
come.
All revisions of this file

File info

Size: 3846 bytes, 102 lines

File properties

svn:executable
*