-
Notifications
You must be signed in to change notification settings - Fork 66
/
add_shopping_product_listing_group_tree.rb
executable file
·365 lines (325 loc) · 11.5 KB
/
add_shopping_product_listing_group_tree.rb
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
#!/usr/bin/env ruby
# Encoding: utf-8
#
# 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
#
# https://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
require 'optparse'
require 'google/ads/google_ads'
require 'date'
# [START add_shopping_product_listing_group_tree]
def add_shopping_product_listing_group_tree(
customer_id,
ad_group_id,
should_replace_existing_tree
)
# GoogleAdsClient will read a config file from
# ENV['HOME']/google_ads_config.rb when called without parameters
client = Google::Ads::GoogleAds::GoogleAdsClient.new
# 1) Optional: Remove the existing listing group tree, if it already exists
# on the ad group.
if should_replace_existing_tree
remove_listing_group_tree(client, customer_id, ad_group_id)
end
# 2) Construct the listing group tree "root" node.
# Subdivision node: (Root node)
ad_group_criterion_root = create_listing_group_subdivision(
client,
customer_id,
ad_group_id,
)
# This resource has not been created yet and will include the temporary ID as
# part of the criterion ID.
ad_group_criterion_root_resource_name = ad_group_criterion_root.resource_name
operations = [client.operation.create_resource.ad_group_criterion(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
listing_dimension_info = client.resource.listing_dimension_info do |ldi|
ldi.product_condition = client.resource.product_condition_info do |pci|
pci.condition = :NEW
end
end
ad_group_criterion_condition_new = create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_root_resource_name,
listing_dimension_info,
200_000,
)
operation = client.operation.create_resource.ad_group_criterion(
ad_group_criterion_condition_new
)
operations << operation
# Biddable Unit node: (Condition USED node)
# * Product Condition: USED
# * CPC bid: $0.10
listing_dimension_info = client.resource.listing_dimension_info do |ldi|
ldi.product_condition = client.resource.product_condition_info do |pci|
pci.condition = :USED
end
end
ad_group_criterion_condition_used = create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_root_resource_name,
listing_dimension_info,
100_000,
)
operation = client.operation.create_resource.ad_group_criterion(
ad_group_criterion_condition_used
)
operations << operation
# Sub-division node: (Condition "other" node)
# * Product Condition: (not specified)
listing_dimension_info = client.resource.listing_dimension_info do |ldi|
ldi.product_condition = client.resource.product_condition_info
end
ad_group_criterion_condition_other = create_listing_group_subdivision(
client,
customer_id,
ad_group_id,
ad_group_criterion_root_resource_name,
listing_dimension_info,
)
operation = client.operation.create_resource.ad_group_criterion(
ad_group_criterion_condition_other
)
operations << operation
ad_group_criterion_condition_other_resource_name =
ad_group_criterion_condition_other.resource_name
# 4) Construct the listing group unit nodes for CoolBrand, CheapBrand, and
# other.
# Biddable Unit node: (Brand CoolBrand node)
# * Brand: CoolBrand
# * CPC bid: $0.90
listing_dimension_info = client.resource.listing_dimension_info do |ldi|
ldi.product_brand = client.resource.product_brand_info do |pbi|
pbi.value = "CoolBrand"
end
end
ad_group_criterion_brand_cool_brand = create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_condition_other_resource_name,
listing_dimension_info,
900_000,
)
operation = client.operation.create_resource.ad_group_criterion(
ad_group_criterion_brand_cool_brand
)
operations << operation
# Biddable Unit node: (Brand CheapBrand node)
# * Brand: CheapBrand
# * CPC bid: $0.01
listing_dimension_info = client.resource.listing_dimension_info do |ldi|
ldi.product_brand = client.resource.product_brand_info do |pbi|
pbi.value = "CheapBrand"
end
end
ad_group_criterion_brand_cheap_brand = create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_condition_other_resource_name,
listing_dimension_info,
10_000,
)
operation = client.operation.create_resource.ad_group_criterion(
ad_group_criterion_brand_cheap_brand
)
operations << operation
# Biddable Unit node: (Brand other node)
# * CPC bid: $0.05
listing_dimension_info = client.resource.listing_dimension_info do |ldi|
ldi.product_brand = client.resource.product_brand_info
end
ad_group_criterion_brand_other_brand = create_listing_group_unit_biddable(
client,
customer_id,
ad_group_id,
ad_group_criterion_condition_other_resource_name,
listing_dimension_info,
50_000,
)
operation = client.operation.create_resource.ad_group_criterion(
ad_group_criterion_brand_other_brand
)
operations << operation
# Issue the mutate request.
response = client.service.ad_group_criterion.mutate_ad_group_criteria(
customer_id: customer_id,
operations: operations,
)
total_count = 0
response.results.each do |added_criterion|
puts "Added ad group criterion with name: #{added_criterion.resource_name}"
total_count += 1
end
puts "#{total_count} criteria added in total."
end
# [END add_shopping_product_listing_group_tree]
def remove_listing_group_tree(client, customer_id, ad_group_id)
ga_service = client.service.google_ads
query = <<~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}
QUERY
response = ga_service.search(customer_id: customer_id, query: query, page_size: PAGE_SIZE)
operations = response.map do |row|
criterion = row.ad_group_criterion
puts "Found an ad group criterion with resource name: #{criterion.resource_name}"
client.operation.remove_resource.ad_group_criterion(criterion.resource_name)
end
if operations.any?
response = client.service.ad_group_criterion.mutate_ad_group_criteria(
customer_id: customer_id,
operations: operations,
)
puts "Removed #{response.results.count} ad group criteria."
end
end
# Specify 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.
def next_id
@id ||= 0
@id -= 1
end
def create_listing_group_subdivision(
client,
customer_id,
ad_group_id,
parent_ad_group_criterion_name = nil,
listing_dimension_info = nil
)
client.resource.ad_group_criterion do |criterion|
criterion.resource_name = client.path.ad_group_criterion(
customer_id,
ad_group_id,
next_id,
)
criterion.status = :ENABLED
criterion.listing_group = client.resource.listing_group_info do |listing_group_info|
listing_group_info.type = :SUBDIVISION
if parent_ad_group_criterion_name && listing_dimension_info
listing_group_info.parent_ad_group_criterion = parent_ad_group_criterion_name
listing_group_info.case_value = listing_dimension_info
end
end
end
end
def create_listing_group_unit_biddable(client, customer_id, ad_group_id,
parent_ad_group_criterion_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
# using the client.path utility.
# 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).
client.resource.ad_group_criterion do |criterion|
criterion.ad_group = client.path.ad_group(customer_id, ad_group_id)
criterion.status = :ENABLED
criterion.cpc_bid_micros = cpc_bid_micros
criterion.listing_group = client.resource.listing_group_info do |listing_group|
# The type UNIT allows the group to be biddable.
listing_group.type = :UNIT
listing_group.parent_ad_group_criterion = parent_ad_group_criterion_name
listing_group.case_value = listing_dimension_info
end
end
end
if __FILE__ == $0
PAGE_SIZE = 1000
options = {}
# 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.
options[:customer_id] = 'INSERT_CUSTOMER_ID_HERE'
options[:ad_group_id] = 'INSERT_AD_GROUP_ID_HERE'
# Specifying any value for this field on the command line will override this
# to true.
options[:should_replace_existing_tree] = false
OptionParser.new do |opts|
opts.banner = sprintf("Usage: #{File.basename(__FILE__)} [options]")
opts.separator ''
opts.separator 'Options:'
opts.on('-C', '--customer-id CUSTOMER-ID', String, 'Customer ID') do |v|
options[:customer_id] = v
end
opts.on('-A', '--ad-group-id AD-GROUP-ID', String, 'Ad Group ID') do |v|
options[:ad_group_id] = v
end
opts.on('-r', '--replace-existing-tree REPLACE-EXISTING-TREE',
TrueClass, 'Create Default Listing Group') do |v|
options[:should_replace_existing_tree] = v
end
opts.separator ''
opts.separator 'Help:'
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
begin
add_shopping_product_listing_group_tree(
options.fetch(:customer_id).tr("-", ""),
options.fetch(:ad_group_id),
options.fetch(:should_replace_existing_tree),
)
rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e
e.failure.errors.each do |error|
STDERR.printf("Error with message: %s\n", error.message)
if error.location
error.location.field_path_elements.each do |field_path_element|
STDERR.printf("\tOn field: %s\n", field_path_element.field_name)
end
end
error.error_code.to_h.each do |k, v|
next if v == :UNSPECIFIED
STDERR.printf("\tType: %s\n\tCode: %s\n", k, v)
end
end
raise
end
end