/
add_responsive_search_ad_with_ad_customizer.pl
268 lines (230 loc) · 10.3 KB
/
add_responsive_search_ad_with_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
#!/usr/bin/perl -w
#
# Copyright 2021, 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.
#
# Adds a customizer attribute, links the customizer attribute to a customer, and
# then adds a responsive search ad with a description using the ad customizer to
# the specified ad group.
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::V14::Resources::CustomizerAttribute;
use Google::Ads::GoogleAds::V14::Resources::CustomerCustomizer;
use Google::Ads::GoogleAds::V14::Resources::Ad;
use Google::Ads::GoogleAds::V14::Resources::AdGroupAd;
use Google::Ads::GoogleAds::V14::Common::CustomizerValue;
use Google::Ads::GoogleAds::V14::Common::ResponsiveSearchAdInfo;
use Google::Ads::GoogleAds::V14::Common::TextAsset;
use Google::Ads::GoogleAds::V14::Enums::CustomizerAttributeTypeEnum qw(PRICE);
use Google::Ads::GoogleAds::V14::Enums::AdGroupAdStatusEnum qw(PAUSED);
use
Google::Ads::GoogleAds::V14::Services::CustomizerAttributeService::CustomizerAttributeOperation;
use
Google::Ads::GoogleAds::V14::Services::CustomerCustomizerService::CustomerCustomizerOperation;
use Google::Ads::GoogleAds::V14::Services::AdGroupAdService::AdGroupAdOperation;
use Google::Ads::GoogleAds::V14::Utils::ResourceNames;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Cwd qw(abs_path);
# The following parameter(s) should be provided to run the example. You can
# either specify these by changing the INSERT_XXX_ID_HERE values below, or on
# the command line.
#
# Parameters passed on the command line will override any parameters set in
# code.
#
# Running the example with -h will print the command line usage.
my $customer_id = "INSERT_CUSTOMER_ID_HERE";
my $ad_group_id = "INSERT_AD_GROUP_ID_HERE";
# The name of the customizer attribute to be used in the ad customizer, which must
# be unique. To run this example multiple times, change this value or specify its
# corresponding argument.
# Note that there is a limit for the number of enabled customizer attributes in
# one account, so you shouldn't run this example more than necessary.
# Visit https://developers.google.com/google-ads/api/docs/ads/customize-responsive-search-ads#rules_and_limitations
# for details.
my $customizer_attribute_name = "Price";
sub add_responsive_search_ad_with_ad_customizer {
my ($api_client, $customer_id, $ad_group_id, $customizer_attribute_name) = @_;
my $customizer_attribute_resource_name =
create_customizer_attribute($api_client, $customer_id,
$customizer_attribute_name);
link_customizer_attribute_to_customer($api_client, $customer_id,
$customizer_attribute_resource_name);
create_responsive_search_ad_with_customization($api_client, $customer_id,
$ad_group_id, $customizer_attribute_name);
return 1;
}
# Creates a customizer attribute with the specified customizer attribute name.
# [START add_responsive_search_ad_with_ad_customizer_1]
sub create_customizer_attribute {
my ($api_client, $customer_id, $customizer_attribute_name) = @_;
# Create a customizer attribute with the specified name.
my $customizer_attribute =
Google::Ads::GoogleAds::V14::Resources::CustomizerAttribute->new({
name => $customizer_attribute_name,
# Specify the type to be 'PRICE' so that we can dynamically customize the part
# of the ad's description that is a price of a product/service we advertise.
type => PRICE
});
# Create a customizer attribute operation for creating a customizer attribute.
my $operation =
Google::Ads::GoogleAds::V14::Services::CustomizerAttributeService::CustomizerAttributeOperation
->new({
create => $customizer_attribute
});
# Issue a mutate request to add the customizer attribute and print its information.
my $response = $api_client->CustomizerAttributeService()->mutate({
customerId => $customer_id,
operations => [$operation]});
my $customizer_attribute_resource_name =
$response->{results}[0]{resourceName};
printf "Added a customizer attribute with resource name '%s'.\n",
$customizer_attribute_resource_name;
return $customizer_attribute_resource_name;
}
# [END add_responsive_search_ad_with_ad_customizer_1]
# Links the customizer attribute to the customer by providing a value to be used
# in a responsive search ad that will be created in a later step.
# [START add_responsive_search_ad_with_ad_customizer_2]
sub link_customizer_attribute_to_customer {
my ($api_client, $customer_id, $customizer_attribute_resource_name) = @_;
# Create a customer customizer with the value to be used in the responsive search ad.
my $customer_customizer =
Google::Ads::GoogleAds::V14::Resources::CustomerCustomizer->new({
customizerAttribute => $customizer_attribute_resource_name,
# Specify '100USD' as a text value. The ad customizer will dynamically replace
# the placeholder with this value when the ad serves.
value => Google::Ads::GoogleAds::V14::Common::CustomizerValue->new({
type => PRICE,
stringValue => "100USD"
})});
# Create a customer customizer operation.
my $operation =
Google::Ads::GoogleAds::V14::Services::CustomerCustomizerService::CustomerCustomizerOperation
->new({
create => $customer_customizer
});
# Issue a mutate request to add the customer customizer and print its information.
my $response = $api_client->CustomerCustomizerService()->mutate({
customerId => $customer_id,
operations => [$operation]});
printf "Added a customer customizer with resource name '%s'.\n",
$response->{results}[0]{resourceName};
}
# [END add_responsive_search_ad_with_ad_customizer_2]
# Creates a responsive search ad that uses the specified customizer attribute.
# [START add_responsive_search_ad_with_ad_customizer_3]
sub create_responsive_search_ad_with_customization {
my ($api_client, $customer_id, $ad_group_id, $customizer_attribute_name) = @_;
# Create an ad and set responsive search ad info.
my $ad = Google::Ads::GoogleAds::V14::Resources::Ad->new({
responsiveSearchAd =>
Google::Ads::GoogleAds::V14::Common::ResponsiveSearchAdInfo->new({
headlines => [
Google::Ads::GoogleAds::V14::Common::TextAsset->new({
text => "Cruise to Mars"
}
),
Google::Ads::GoogleAds::V14::Common::TextAsset->new({
text => "Best Space Cruise Line"
}
),
Google::Ads::GoogleAds::V14::Common::TextAsset->new({
text => "Experience the Stars"
})
],
descriptions => [
Google::Ads::GoogleAds::V14::Common::TextAsset->new({
text => "Buy your tickets now"
}
),
# Create this particular description using the ad customizer.
# Visit https://developers.google.com/google-ads/api/docs/ads/customize-responsive-search-ads#ad_customizers_in_responsive_search_ads
# for details about the placeholder format.
# The ad customizer replaces the placeholder with the value we previously
# created and linked to the customer using `CustomerCustomizer`.
Google::Ads::GoogleAds::V14::Common::TextAsset->new({
text => "Just {CUSTOMIZER.$customizer_attribute_name:10USD}"
})
],
path1 => "all-inclusive",
path2 => "deals"
}
),
finalUrls => ["http://www.example.com"]});
# Create an ad group ad to hold the above ad.
my $ad_group_ad = Google::Ads::GoogleAds::V14::Resources::AdGroupAd->new({
adGroup => Google::Ads::GoogleAds::V14::Utils::ResourceNames::ad_group(
$customer_id, $ad_group_id
),
status => PAUSED,
ad => $ad
});
# Create an ad group ad operation.
my $operation =
Google::Ads::GoogleAds::V14::Services::AdGroupAdService::AdGroupAdOperation
->new({
create => $ad_group_ad
});
# Issue a mutate request to add the ad group ad and print its information.
my $response = $api_client->AdGroupAdService()->mutate({
customerId => $customer_id,
operations => [$operation]});
printf "Created responsive search ad with resource name '%s'.\n",
$response->{results}[0]{resourceName};
}
# [END add_responsive_search_ad_with_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);
# 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,
"customizer_attribute_name=s" => \$customizer_attribute_name,
);
# 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, $customizer_attribute_name);
# Call the example.
add_responsive_search_ad_with_ad_customizer(
$api_client, $customer_id =~ s/-//gr,
$ad_group_id, $customizer_attribute_name
);
=pod
=head1 NAME
add_responsive_search_ad_with_ad_customizer
=head1 DESCRIPTION
Adds a customizer attribute, links the customizer attribute to a customer, and
then adds a responsive search ad with a description using the ad customizer to
the specified ad group.
=head1 SYNOPSIS
add_responsive_search_ad_with_ad_customizer.pl [options]
-help Show the help message.
-customer_id The Google Ads customer ID.
-ad_group_id The ad group ID.
-customizer_attribute_name [optional] The name of the customizer attribute.
=cut