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
#!/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 keyword given an existing ad group. To create
# an ad group, you can run 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/CriterionService',
'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 keyword structure.
my $ad_group_id = 'INSERT_AD_GROUP_ID_HERE';
my $criterion = {
'adGroupId' => $ad_group_id,
'criterionType' => 'Keyword',
'text' => 'mars cruise',
'type' => 'Broad'
};

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

# Add keyword if there are no policy violations.
unless (@errors) {
$response = $service->call('addCriteria' => $criteria, @headers);
my @criteria = ($response->result(), $response->paramsout());

# Display new keyword.
foreach $criterion (@criteria) {
print('New keyword with text "', $criterion->{'text'}, '" and ID "',
$criterion->{'id'}, '" was created.', "\n");
}
}
else {
# Display policy violations.
print('New keyword 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: 3750 bytes, 100 lines

File properties

svn:executable
*