-
Notifications
You must be signed in to change notification settings - Fork 17
/
add_shopping_product_listing_group_tree.pl
452 lines (393 loc) · 16.1 KB
/
add_shopping_product_listing_group_tree.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/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 example shows how to add a shopping listing group tree to a shopping ad
# group. The example will optionally clear an existing listing group tree and
# rebuild it to include the following tree structure:
#
# ProductCanonicalCondition NEW $0.20
# ProductCanonicalCondition USED $0.10
# ProductCanonicalCondition null (everything else)
# ProductBrand CoolBrand $0.90
# ProductBrand CheapBrand $0.01
# ProductBrand null (everything else) $0.50
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::Utils::SearchGoogleAdsIterator;
use Google::Ads::GoogleAds::V8::Resources::AdGroupCriterion;
use Google::Ads::GoogleAds::V8::Common::ListingGroupInfo;
use Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo;
use Google::Ads::GoogleAds::V8::Common::ProductConditionInfo;
use Google::Ads::GoogleAds::V8::Common::ProductBrandInfo;
use Google::Ads::GoogleAds::V8::Enums::ListingGroupTypeEnum
qw(SUBDIVISION UNIT);
use Google::Ads::GoogleAds::V8::Enums::AdGroupCriterionStatusEnum qw(ENABLED);
use Google::Ads::GoogleAds::V8::Enums::ProductConditionEnum qw(NEW USED);
use
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation;
use
Google::Ads::GoogleAds::V8::Services::GoogleAdsService::SearchGoogleAdsRequest;
use Google::Ads::GoogleAds::V8::Utils::ResourceNames;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);
use constant PAGE_SIZE => 1000;
# 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";
my $replace_existing_tree = undef;
# [START add_shopping_product_listing_group_tree]
sub add_shopping_product_listing_group_tree {
my ($api_client, $customer_id, $ad_group_id, $replace_existing_tree) = @_;
# 1) Optional: Remove the existing listing group tree, if it already exists
# on the ad group.
if ($replace_existing_tree) {
remove_listing_group_tree($api_client, $customer_id, $ad_group_id);
}
# Create a list of ad group criteria operations to add.
my $operations = [];
# 2) Construct the listing group tree "root" node.
# Subdivision node: (Root node)
my $ad_group_criterion_root =
create_listing_group_subdivision($customer_id, $ad_group_id);
# Get the resource name that will be used for the root node.
# This resource has not been created yet and will include the temporary ID as
# part of the criterion ID.
my $ad_group_criterion_root_resource_name =
$ad_group_criterion_root->{resourceName};
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_root
});
# 3) Construct the listing group unit nodes for NEW, USED, and other.
# Biddable Unit node: (Condition NEW node)
# * Product Condition: NEW
# * CPC bid: $0.20
my $ad_group_criterion_condition_new = create_listing_group_unit_biddable(
$customer_id,
$ad_group_id,
$ad_group_criterion_root_resource_name,
Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo->new({
productCondition =>
Google::Ads::GoogleAds::V8::Common::ProductConditionInfo->new({
condition => NEW
})}
),
200000
);
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_condition_new
});
# Biddable Unit node: (Condition USED node)
# * Product Condition: USED
# * CPC bid: $0.10
my $ad_group_criterion_condition_used = create_listing_group_unit_biddable(
$customer_id,
$ad_group_id,
$ad_group_criterion_root_resource_name,
Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo->new({
productCondition =>
Google::Ads::GoogleAds::V8::Common::ProductConditionInfo->new({
condition => USED
})}
),
100000
);
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_condition_used
});
# Sub-division node: (Condition "other" node)
# * Product Condition: (not specified)
my $ad_group_criterion_condition_other = create_listing_group_subdivision(
$customer_id,
$ad_group_id,
$ad_group_criterion_root_resource_name,
Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo->new({
# All sibling nodes must have the same dimension type, even if they
# don't contain a bid.
productCondition =>
Google::Ads::GoogleAds::V8::Common::ProductConditionInfo->new()}));
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_condition_other
});
# Get the resource name that will be used for the condition other node.
# This resource has not been created yet and will include the temporary ID as
# part of the criterion ID.
my $ad_group_criterion_condition_other_resource_name =
$ad_group_criterion_condition_other->{resourceName};
# 4) Construct the listing group unit nodes for CoolBrand, CheapBrand, and
# other.
# Biddable Unit node: (Brand CoolBrand node)
# * Brand: CoolBrand
# * CPC bid: $0.90
my $ad_group_criterion_brand_cool_brand = create_listing_group_unit_biddable(
$customer_id,
$ad_group_id,
$ad_group_criterion_condition_other_resource_name,
Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo->new({
productBrand =>
Google::Ads::GoogleAds::V8::Common::ProductBrandInfo->new(
{value => "CoolBrand"})}
),
900000
);
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_brand_cool_brand
});
# Biddable Unit node: (Brand CheapBrand node)
# * Brand: CheapBrand
# * CPC bid: $0.01
my $ad_group_criterion_brand_cheap_brand = create_listing_group_unit_biddable(
$customer_id,
$ad_group_id,
$ad_group_criterion_condition_other_resource_name,
Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo->new({
productBrand =>
Google::Ads::GoogleAds::V8::Common::ProductBrandInfo->new(
{value => "CheapBrand"})}
),
10000
);
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_brand_cheap_brand
});
# Biddable Unit node: (Brand other node)
# * CPC bid: $0.05
my $ad_group_criterion_brand_other_brand = create_listing_group_unit_biddable(
$customer_id,
$ad_group_id,
$ad_group_criterion_condition_other_resource_name,
Google::Ads::GoogleAds::V8::Common::ListingDimensionInfo->new({
productBrand =>
Google::Ads::GoogleAds::V8::Common::ProductBrandInfo->new()}
),
50000
);
push @$operations,
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
create => $ad_group_criterion_brand_other_brand
});
# Add the ad group criterion.
my $ad_group_criteria_response =
$api_client->AdGroupCriterionService()->mutate({
customerId => $customer_id,
operations => $operations
});
printf "Added %d ad group criteria for listing group tree with the " .
"following resource names:\n",
scalar @{$ad_group_criteria_response->{results}};
foreach my $result (@{$ad_group_criteria_response->{results}}) {
print $result->{resourceName}, "\n";
}
return 1;
}
# [END add_shopping_product_listing_group_tree]
# Removes all the ad group criteria that define the existing listing group
# tree for an ad group.
sub remove_listing_group_tree {
my ($api_client, $customer_id, $ad_group_id) = @_;
my $search_query =
"SELECT ad_group_criterion.resource_name " .
"FROM ad_group_criterion WHERE ad_group_criterion.type = LISTING_GROUP " .
"AND ad_group_criterion.listing_group.parent_ad_group_criterion IS NULL " .
"AND ad_group.id = $ad_group_id";
# Create a search Google Ads request that will retrieve all listing groups
# where the parent ad group criterion is NULL (and hence the root node in
# the tree) for a given ad group id.
my $search_request =
Google::Ads::GoogleAds::V8::Services::GoogleAdsService::SearchGoogleAdsRequest
->new({
customerId => $customer_id,
query => $search_query,
pageSize => PAGE_SIZE
});
# Get the GoogleAdsService.
my $google_ads_service = $api_client->GoogleAdsService();
my $iterator = Google::Ads::GoogleAds::Utils::SearchGoogleAdsIterator->new({
service => $google_ads_service,
request => $search_request
});
my $operations = [];
# Iterate over all rows in all pages to find the ad group criterion to remove.
while ($iterator->has_next) {
my $google_ads_row = $iterator->next;
my $ad_group_criterion = $google_ads_row->{adGroupCriterion};
printf "Found an ad group criterion with the resource name: '%s'.\n",
$ad_group_criterion->{resourceName};
# Create an ad group criterion operation.
my $ad_group_criterion_operation =
Google::Ads::GoogleAds::V8::Services::AdGroupCriterionService::AdGroupCriterionOperation
->new({
remove => $ad_group_criterion->{resourceName}});
push @$operations, $ad_group_criterion_operation;
}
if (scalar @$operations) {
# Remove the ad group criterion that define the listing group tree.
my $ad_group_criteria_response =
$api_client->AdGroupCriterionService()->mutate({
customerId => $customer_id,
operations => $operations
});
printf "Removed %d ad group criteria.\n",
scalar @{$ad_group_criteria_response->{results}};
}
}
# Creates a new criterion containing a subdivision listing group node. If
# the parent ad group criterion resource name is not specified, this method
# creates a root node.
sub create_listing_group_subdivision {
my ($customer_id, $ad_group_id, $parent_ad_group_criterion_resource_name,
$listing_dimension_info)
= @_;
my $listing_group_info =
Google::Ads::GoogleAds::V8::Common::ListingGroupInfo->new({
# Set the type as a SUBDIVISION, which will allow the node to be the
# parent of another sub-tree.
'type' => SUBDIVISION
});
# If $parent_ad_group_criterion_resource_name and $listing_dimension_info
# are not null, create a non-root division by setting its parent and case value.
if ($parent_ad_group_criterion_resource_name and $listing_dimension_info) {
# Set the ad group criterion resource name for the parent listing group.
# This can include a temporary ID if the parent criterion is not yet created.
$listing_group_info->{parentAdGroupCriterion} =
$parent_ad_group_criterion_resource_name;
# Case values contain the listing dimension used for the node.
$listing_group_info->{caseValue} = $listing_dimension_info;
}
my $ad_group_criterion =
Google::Ads::GoogleAds::V8::Resources::AdGroupCriterion->new({
# The resource name the criterion will be created with. This will define
# the ID for the ad group criterion.
resourceName =>
Google::Ads::GoogleAds::V8::Utils::ResourceNames::ad_group_criterion(
$customer_id, $ad_group_id, next_id()
),
status => ENABLED,
listingGroup => $listing_group_info
});
return $ad_group_criterion;
}
# Creates a new criterion containing a biddable unit listing group node.
sub create_listing_group_unit_biddable {
my ($customer_id, $ad_group_id, $parent_ad_group_criterion_resource_name,
$listing_dimension_info, $cpc_bid_micros)
= @_;
# Note: There are two approaches for creating new unit nodes:
# (1) Set the ad group resource name on the criterion (no temporary ID
# required).
# (2) Use a temporary ID to construct the criterion resource name and set it
# to the 'resourceName' attribute.
# In both cases you must set the parent ad group criterion's resource name on
# the listing group for non-root nodes.
# This example demonstrates method (1).
my $ad_group_criterion =
Google::Ads::GoogleAds::V8::Resources::AdGroupCriterion->new({
adGroup => Google::Ads::GoogleAds::V8::Utils::ResourceNames::ad_group(
$customer_id, $ad_group_id
),
status => ENABLED,
listingGroup => Google::Ads::GoogleAds::V8::Common::ListingGroupInfo->new(
{
# Set the type as a UNIT, which will allow the group to be biddable.
type => UNIT,
# Set the ad group criterion resource name for the parent listing group.
# This can include a temporary ID if the parent criterion is not yet created.
parentAdGroupCriterion => $parent_ad_group_criterion_resource_name,
# Case values contain the listing dimension used for the node.
caseValue => $listing_dimension_info
}
),
# Set the bid for this listing group unit.
# This will be used as the CPC bid for items that are included in this
# listing group.
cpcBidMicros => $cpc_bid_micros
});
return $ad_group_criterion;
}
# Specifies a decreasing negative number for temporary ad group criteria IDs.
# The ad group criteria will get real IDs when created on the server.
# Returns -1, -2, -3, etc. on subsequent calls.
sub next_id {
our $id ||= 0;
$id -= 1;
}
# 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,
"replace_existing_tree=s" => \$replace_existing_tree
);
# 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_shopping_product_listing_group_tree($api_client, $customer_id =~ s/-//gr,
$ad_group_id, $replace_existing_tree);
=pod
=head1 NAME
add_shopping_product_listing_group_tree
=head1 DESCRIPTION
This example shows how to add a shopping listing group tree to a shopping ad group.
The example will optionally clear an existing listing group tree and rebuild it to
include the following tree structure:
ProductCanonicalCondition NEW $0.20
ProductCanonicalCondition USED $0.10
ProductCanonicalCondition null (everything else)
ProductBrand CoolBrand $0.90
ProductBrand CheapBrand $0.01
ProductBrand null (everything else) $0.50
=head1 SYNOPSIS
add_shopping_product_listing_group_tree.pl [options]
-help Show the help message.
-customer_id The Google Ads customer ID.
-ad_group_id The ad group ID.
-replace_existing_tree [optional] Replace the existing listing group tree
on the ad group, if it already exists.
=cut