This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
handle_partial_failures.pl
executable file
·147 lines (130 loc) · 4.76 KB
/
handle_partial_failures.pl
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
#!/usr/bin/perl -w
#
# Copyright 2017, 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 example demonstrates how to handle partial failures.
use strict;
use lib "../../../lib";
use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201806::AdGroupCriterionOperation;
use Google::Ads::AdWords::v201806::BiddableAdGroupCriterion;
use Google::Ads::AdWords::v201806::Keyword;
use Google::Ads::Common::ErrorUtils;
use Cwd qw(abs_path);
# Replace with valid values of your account.
my $ad_group_id = "INSERT_AD_GROUP_ID_HERE";
# Example main subroutine.
sub handle_partial_failures {
my $client = shift;
my $ad_group_id = shift;
# Setting partial failures flag.
$client->set_partial_failure(1);
# Create keywords.
my @keywords = (
Google::Ads::AdWords::v201806::Keyword->new({
text => "mars cruise",
matchType => "BROAD"
}
),
Google::Ads::AdWords::v201806::Keyword->new({
text => "inv\@lid cruise",
matchType => "BROAD"
}
),
Google::Ads::AdWords::v201806::Keyword->new({
text => "venus cruise",
matchType => "BROAD"
}
),
Google::Ads::AdWords::v201806::Keyword->new({
text => "b\(a\)d keyword cruise",
matchType => "BROAD"
}));
# Create biddable ad group criterions and operations.
my @operations = ();
for my $keyword (@keywords) {
my $keyword_biddable_ad_group_criterion =
Google::Ads::AdWords::v201806::BiddableAdGroupCriterion->new({
adGroupId => $ad_group_id,
criterion => $keyword
});
push @operations,
Google::Ads::AdWords::v201806::AdGroupCriterionOperation->new({
operator => "ADD",
operand => $keyword_biddable_ad_group_criterion
});
}
# Add ad group criteria.
my $result =
$client->AdGroupCriterionService()->mutate({operations => \@operations});
# Display results.
if ($result->get_value() || $result->get_partialFailureErrors()) {
# Display added criteria.
foreach my $ad_group_criterion (@{$result->get_value()}) {
if (
$ad_group_criterion->isa(
"Google::Ads::AdWords::v201806::BiddableAdGroupCriterion"))
{
printf "Ad group criterion with ad group id \"%d\", criterion id " .
"\"%d\", and keyword \"%s\" was added.\n",
$ad_group_criterion->get_adGroupId(),
$ad_group_criterion->get_criterion()->get_id(),
$ad_group_criterion->get_criterion()->get_text();
}
}
# Check partial failures.
foreach my $error (@{$result->get_partialFailureErrors()}) {
# Get the index of the failed operation from the error's field path
# elements.
my $field_path_elements = $error->get_fieldPathElements();
my $first_field_path_element =
($field_path_elements && (scalar $field_path_elements > 0))
? $field_path_elements->[0]
: undef;
if ( $first_field_path_element
&& $first_field_path_element->get_field() eq "operations"
&& defined $first_field_path_element->get_index())
{
my $error_index = $first_field_path_element->get_index();
my $ad_group_criterion = $operations[$error_index]->get_operand();
printf "Ad group criterion with ad group id \"%d\" and keyword " .
"\"%s\" as trigger a failure for the following reason: " .
"\"%s\".\n",
$ad_group_criterion->get_adGroupId(),
$ad_group_criterion->get_criterion()->get_text(),
$error->get_errorString();
} else {
printf "A failure for the following reason: \"%s\" has ocurred.\n",
$error->get_errorString();
}
}
} else {
print "No ad group criteria were added.";
}
return 1;
}
# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
return 1;
}
# Log SOAP XML request, response and API errors.
Google::Ads::AdWords::Logging::enable_all_logging();
# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({version => "v201806"});
# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);
# Call the example
handle_partial_failures($client, $ad_group_id);