/
add_ad_customizer.pl
325 lines (273 loc) · 11.5 KB
/
add_ad_customizer.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/perl -w
#
# 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.
#
# This code example adds two ad customizer attributes and associates them with the ad group.
# Then it adds an ad that uses the ad customizer attributes to populate dynamic data.
use strict;
use warnings;
use utf8;
use FindBin qw($Bin);
use lib "$Bin/../../lib";
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::Utils::GoogleAdsHelper;
use Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping;
use Google::Ads::GoogleAds::V16::Resources::Ad;
use Google::Ads::GoogleAds::V16::Resources::AdGroupAd;
use Google::Ads::GoogleAds::V16::Resources::AdGroupCustomizer;
use Google::Ads::GoogleAds::V16::Resources::CustomizerAttribute;
use Google::Ads::GoogleAds::V16::Common::AdTextAsset;
use Google::Ads::GoogleAds::V16::Common::CustomizerValue;
use Google::Ads::GoogleAds::V16::Common::ResponsiveSearchAdInfo;
use Google::Ads::GoogleAds::V16::Enums::CustomizerAttributeTypeEnum
qw(TEXT PRICE);
use Google::Ads::GoogleAds::V16::Enums::ServedAssetFieldTypeEnum qw(HEADLINE_1);
use Google::Ads::GoogleAds::V16::Services::AdGroupAdService::AdGroupAdOperation;
use
Google::Ads::GoogleAds::V16::Services::AdGroupCustomizerService::AdGroupCustomizerOperation;
use
Google::Ads::GoogleAds::V16::Services::CustomizerAttributeService::CustomizerAttributeOperation;
use Google::Ads::GoogleAds::V16::Utils::ResourceNames;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);
sub add_ad_customizer {
my ($api_client, $customer_id, $ad_group_id) = @_;
my $string_customizer_name = "Planet_" . uniqid();
my $price_customizer_name = "Price_" . uniqid();
my $text_customizer_attribute_resource_name =
create_text_customizer_attribute($api_client, $customer_id,
$string_customizer_name);
my $price_customizer_attribute_resource_name =
create_price_customizer_attribute($api_client, $customer_id,
$price_customizer_name);
# Link the customizer attributes to the ad group.
link_customizer_attributes(
$api_client, $customer_id, $ad_group_id,
$text_customizer_attribute_resource_name,
$price_customizer_attribute_resource_name
);
# Create an ad with the customizations provided by the ad customizer attributes.
create_ad_with_customizations($api_client, $customer_id, $ad_group_id,
$string_customizer_name, $price_customizer_name);
return 1;
}
# Creates a text customizer attribute and returns its resource name.
# [START add_ad_customizer]
sub create_text_customizer_attribute {
my ($api_client, $customer_id, $customizer_name) = @_;
# Creates a text customizer attribute. The customizer attribute name is
# arbitrary and will be used as a placeholder in the ad text fields.
my $text_attribute =
Google::Ads::GoogleAds::V16::Resources::CustomizerAttribute->new({
name => $customizer_name,
type => TEXT
});
# Create a customizer attribute operation for creating a customizer attribute.
my $text_attribute_operation =
Google::Ads::GoogleAds::V16::Services::CustomizerAttributeService::CustomizerAttributeOperation
->new({
create => $text_attribute
});
# Issue a mutate request to add the customizer attribute.
my $response = $api_client->CustomizerAttributeService()->mutate({
customerId => $customer_id,
operations => [$text_attribute_operation]});
my $customizer_attribute_resource_name =
$response->{results}[0]{resourceName};
printf "Added text customizer attribute with resource name '%s'.\n",
$customizer_attribute_resource_name;
return $customizer_attribute_resource_name;
}
# [END add_ad_customizer]
# Creates a price customizer attribute and returns its resource name.
# [START add_ad_customizer_1]
sub create_price_customizer_attribute {
my ($api_client, $customer_id, $customizer_name) = @_;
# Creates a price customizer attribute. The customizer attribute name is
# arbitrary and will be used as a placeholder in the ad text fields.
my $price_attribute =
Google::Ads::GoogleAds::V16::Resources::CustomizerAttribute->new({
name => $customizer_name,
type => PRICE
});
# Create a customizer attribute operation for creating a customizer attribute.
my $price_attribute_operation =
Google::Ads::GoogleAds::V16::Services::CustomizerAttributeService::CustomizerAttributeOperation
->new({
create => $price_attribute
});
# Issue a mutate request to add the customizer attribute.
my $response = $api_client->CustomizerAttributeService()->mutate({
customerId => $customer_id,
operations => [$price_attribute_operation]});
my $customizer_attribute_resource_name =
$response->{results}[0]{resourceName};
printf "Added price customizer attribute with resource name '%s'.\n",
$customizer_attribute_resource_name;
return $customizer_attribute_resource_name;
}
# [END add_ad_customizer_1]
# Restricts the ad customizer attributes to work only with a specific ad group;
# this prevents the customizer attributes from being used elsewhere and makes sure
# they are used only for customizing a specific ad group.
# [START add_ad_customizer_2]
sub link_customizer_attributes {
my (
$api_client, $customer_id, $ad_group_id,
$text_customizer_attribute_resource_name,
$price_customizer_attribute_resource_name
) = @_;
my $ad_group_customizer_operations = [];
# Binds the text attribute customizer to a specific ad group to
# make sure it will only be used to customizer ads inside that ad group.
my $marsCustomizer =
Google::Ads::GoogleAds::V16::Resources::AdGroupCustomizer->new({
customizerAttribute => $text_customizer_attribute_resource_name,
value => Google::Ads::GoogleAds::V16::Common::CustomizerValue->new({
type => TEXT,
stringValue => "Mars"
}
),
adGroup => Google::Ads::GoogleAds::V16::Utils::ResourceNames::ad_group(
$customer_id, $ad_group_id
)});
push @$ad_group_customizer_operations,
Google::Ads::GoogleAds::V16::Services::AdGroupCustomizerService::AdGroupCustomizerOperation
->new({
create => $marsCustomizer
});
# Binds the price attribute customizer to a specific ad group to
# make sure it will only be used to customizer ads inside that ad group.
my $priceCustomizer =
Google::Ads::GoogleAds::V16::Resources::AdGroupCustomizer->new({
customizerAttribute => $price_customizer_attribute_resource_name,
value => Google::Ads::GoogleAds::V16::Common::CustomizerValue->new({
type => PRICE,
stringValue => "100.0€"
}
),
adGroup => Google::Ads::GoogleAds::V16::Utils::ResourceNames::ad_group(
$customer_id, $ad_group_id
)});
push @$ad_group_customizer_operations,
Google::Ads::GoogleAds::V16::Services::AdGroupCustomizerService::AdGroupCustomizerOperation
->new({
create => $priceCustomizer
});
# Issue a mutate request to link the customizers to the ad group.
my $mutate_ad_group_customizers_response =
$api_client->AdGroupCustomizerService()->mutate({
customerId => $customer_id,
operations => $ad_group_customizer_operations
});
# Display the results.
foreach my $result (@{$mutate_ad_group_customizers_response->{results}}) {
printf "Added an ad group customizer with resource name '%s'.\n",
$result->{resourceName};
}
}
# [END add_ad_customizer_2]
# Creates a responsive search ad that uses the ad customizer attributes to populate the placeholders.
# [START add_ad_customizer_3]
sub create_ad_with_customizations {
my ($api_client, $customer_id, $ad_group_id, $string_customizer_name,
$price_customizer_name)
= @_;
# Creates a responsive search ad using the attribute customizer names as
# placeholders and default values to be used in case there are no attribute
# customizer values.
my $responsive_search_ad_info =
Google::Ads::GoogleAds::V16::Common::ResponsiveSearchAdInfo->new({
headlines => [
Google::Ads::GoogleAds::V16::Common::AdTextAsset->new({
text =>
"Luxury cruise to {CUSTOMIZER.$string_customizer_name:Venus}",
pinnedField => HEADLINE_1
}
),
Google::Ads::GoogleAds::V16::Common::AdTextAsset->new({
text => "Only {CUSTOMIZER.$price_customizer_name:10.0€}",
}
),
Google::Ads::GoogleAds::V16::Common::AdTextAsset->new({
text =>
"Cruise to {CUSTOMIZER.$string_customizer_name:Venus} for {CUSTOMIZER.$price_customizer_name:10.0€}",
})
],
descriptions => [
Google::Ads::GoogleAds::V16::Common::AdTextAsset->new({
text =>
"Tickets are only {CUSTOMIZER.$price_customizer_name:10.0€}!",
}
),
Google::Ads::GoogleAds::V16::Common::AdTextAsset->new({
text =>
"Buy your tickets to {CUSTOMIZER.$string_customizer_name:Venus} now!"
})]});
my $ad = Google::Ads::GoogleAds::V16::Resources::Ad->new({
responsiveSearchAd => $responsive_search_ad_info,
finalUrls => ["https://www.example.com"]});
my $ad_group_ad = Google::Ads::GoogleAds::V16::Resources::AdGroupAd->new({
ad => $ad,
adGroup => Google::Ads::GoogleAds::V16::Utils::ResourceNames::ad_group(
$customer_id, $ad_group_id
)});
my $ad_group_ad_operation =
Google::Ads::GoogleAds::V16::Services::AdGroupAdService::AdGroupAdOperation
->new({
create => $ad_group_ad
});
# Issue a mutate request to add the ads.
my $response = $api_client->AdGroupAdService()->mutate({
customerId => $customer_id,
operations => [$ad_group_ad_operation]});
my $ad_group_ad_resource_name = $response->{results}[0]{resourceName};
printf "Added an ad with resource name '%s'.\n", $ad_group_ad_resource_name;
}
# [END add_ad_customizer_3]
# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
return 1;
}
# Get Google Ads Client, credentials will be read from ~/googleads.properties.
my $api_client = Google::Ads::GoogleAds::Client->new();
# By default examples are set to die on any server returned fault.
$api_client->set_die_on_faults(1);
my $customer_id = undef;
my $ad_group_id = undef;
# Parameters passed on the command line will override any parameters set in code.
GetOptions(
"customer_id=s" => \$customer_id,
"ad_group_id=i" => \$ad_group_id
);
# Print the help message if the parameters are not initialized in the code nor
# in the command line.
pod2usage(2) if not check_params($customer_id, $ad_group_id);
# Call the example.
add_ad_customizer($api_client, $customer_id =~ s/-//gr, $ad_group_id);
=pod
=head1 NAME
add_ad_customizer
=head1 DESCRIPTION
This code example adds two ad customizer attributes and associates them with the ad group.
Then it adds an ad that uses the ad customizer attributes to populate dynamic data.
=head1 SYNOPSIS
add_ad_customizer.pl [options]
-help Show the help message.
-customer_id The Google Ads customer ID.
-ad_group_id The ad group ID to add the ad customizers to.
=cut