/
setup_remarketing.rb
366 lines (326 loc) · 11.8 KB
/
setup_remarketing.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
366
#!/usr/bin/env ruby
# Encoding: utf-8
#
# Copyright 2020 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.
#
# Demonstrates various operations involved in remarketing, including:
# (a) creating a user list based on visitors to a website
# (b) targeting a user list with an ad group criterion
# (c) updating the bid modifier on an ad group criterion
# (d) finding and removing all ad group criteria under a given campaign
# (e) targeting a user list with a campaign criterion
# (f) updating the bid modifier on a campaign criterion
# It is unlikely that users will need to perform all of these operations
# consecutively, and all of the operations contained herein are meant of for
# illustrative purposes.
require 'optparse'
require 'google/ads/google_ads'
require 'date'
def setup_remarketing(customer_id, campaign_id, ad_group_id, bid_modifier_value)
# GoogleAdsClient will read a config file from
# ENV['HOME']/google_ads_config.rb when called without parameters
client = Google::Ads::GoogleAds::GoogleAdsClient.new
user_list = create_user_list(client, customer_id)
ad_group_criterion_resource_name = target_ads_in_ad_group_to_user_list(
client,
customer_id,
ad_group_id,
user_list,
)
modify_ad_group_bids(
client,
customer_id,
ad_group_criterion_resource_name,
bid_modifier_value,
)
remove_existing_list_criteria_from_ad_group(
client,
customer_id,
campaign_id,
)
campaign_criterion_resource_name = target_ads_in_campaign_to_user_list(
client,
customer_id,
campaign_id,
user_list,
)
modify_campaign_bids(
client,
customer_id,
campaign_criterion_resource_name,
bid_modifier_value,
)
end
# Creates a user list targeting users that have visited a given url.
# [START setup_remarketing]
def create_user_list(client, customer_id)
# Creates the user list operation.
operation = client.operation.create_resource.user_list do |ul|
ul.name = "All visitors to example.com ##{(Time.new.to_f * 1000).to_i}"
ul.description = "Any visitor to any page of example.com"
ul.membership_status = :OPEN
ul.membership_life_span = 365
# Defines a representation of a user list that is generated by a rule.
ul.rule_based_user_list = client.resource.rule_based_user_list_info do |r|
# To include past users in the user list, set the prepopulation_status
# to REQUESTED.
r.prepopulation_status = :REQUESTED
# Specifies that the user list targets visitors of a page based on
# the provided rule.
r.expression_rule_user_list = client.resource.expression_rule_user_list_info do |expr|
expr.rule = client.resource.user_list_rule_info do |u|
u.rule_item_groups << client.resource.user_list_rule_item_group_info do |group|
group.rule_items << client.resource.user_list_rule_item_info do |item|
# Uses a built-in parameter to create a domain URL rule.
item.name = "url__"
item.string_rule_item = client.resource.user_list_string_rule_item_info do |s|
s.operator = :CONTAINS
s.value = "example.com"
end
end
end
end
end
end
end
# Issues a mutate request to add the user list.
response = client.service.user_list.mutate_user_lists(
customer_id: customer_id,
operations: [operation],
)
user_list_resource_name = response.results.first.resource_name
puts "Created user list with resource name '#{user_list_resource_name}'"
user_list_resource_name
end
# [END setup_remarketing]
# Creates an ad group criterion that targets a user list with an ad group.
# [START setup_remarketing_1]
def target_ads_in_ad_group_to_user_list(
client,
customer_id,
ad_group_id,
user_list
)
# Creates the ad group criterion targeting members of the user list.
operation = client.operation.create_resource.ad_group_criterion do |agc|
agc.ad_group = client.path.ad_group(customer_id, ad_group_id)
agc.user_list = client.resource.user_list_info do |info|
info.user_list = user_list
end
end
# Issues a mutate request to create the ad group criterion.
response = client.service.ad_group_criterion.mutate_ad_group_criteria(
customer_id: customer_id,
operations: [operation],
)
ad_group_criterion_resource_name = response.results.first.resource_name
puts "Successfully created ad group criterion with resource name " \
"'#{ad_group_criterion_resource_name}' targeting user list with resource name " \
"'#{user_list}' with ad group with ID #{ad_group_id}"
ad_group_criterion_resource_name
end
# [END setup_remarketing_1]
# Updates the bid modifier on an ad group criterion.
def modify_ad_group_bids(
client,
customer_id,
ad_group_criterion_resource_name,
bid_modifier_value
)
# Creates the update operation to modify the bid.
operation = client.operation.update_resource.ad_group_criterion(
ad_group_criterion_resource_name) do |agc|
agc.bid_modifier = bid_modifier_value
end
# Issues a mutate request to update the ad group criterion.
response = client.service.ad_group_criterion.mutate_ad_group_criteria(
customer_id: customer_id,
operations: [operation],
)
puts "Successfully updated the bid for ad group criterion with resource name " \
"#{response.results.first.resource_name}"
end
# Removes all ad group criteria targeting a user list under a given campaign.
# This is a necessary step before targeting a user list at the campaign level.
# [START setup_remarketing_3]
def remove_existing_list_criteria_from_ad_group(
client,
customer_id,
campaign_id
)
# Retrieves all of the ad group criteria under a campaign.
ad_group_criteria = get_user_list_ad_group_criterion(
client, customer_id, campaign_id)
# Creates a list of remove operations.
operations = []
ad_group_criteria.each do |agc|
operations << client.operation.remove_resource.ad_group_criterion(agc)
end
# Issues a mutate request to remove all ad group criteria.
response = client.service.ad_group_criterion.mutate_ad_group_criteria(
customer_id: customer_id,
operations: operations,
)
puts "Removed #{response.results.size} ad group criteria."
response.results.each do |result|
puts "Successfully removed ad group criterion with resource name " \
"'#{result.resource_name}'"
end
end
# [END setup_remarketing_3]
# Finds all of user list ad group criteria under a campaign.
# [START setup_remarketing_2]
def get_user_list_ad_group_criterion(
client,
customer_id,
campaign_id
)
user_list_criteria = []
# Creates a query that will retrieve all of the ad group criteria
# under a campaign.
query = <<~QUERY
SELECT ad_group_criterion.criterion_id
FROM ad_group_criterion
WHERE campaign.id = #{campaign_id}
AND ad_group_criterion.type = 'USER_LIST'
QUERY
# Issues the search request.
response = client.service.google_ads.search(
customer_id: customer_id,
query: query,
page_size: PAGE_SIZE,
)
# Iterates over all rows in all pages. Prints the results and adds the ad
# group criteria resource names to the list.
response.each do |row|
ad_group_criterion_resource_name = row.ad_group_criterion.resource_name
puts "Ad group criterion with resource name " \
"'#{ad_group_criterion_resource_name}' was found"
user_list_criteria << ad_group_criterion_resource_name
end
user_list_criteria
end
# [END setup_remarketing_2]
# Creates a campaign criterion that targets a user list with a campaign.
# [START setup_remarketing_4]
def target_ads_in_campaign_to_user_list(
client,
customer_id,
campaign_id,
user_list
)
# Creates the campaign criterion targeting members of the user list.
operation = client.operation.create_resource.campaign_criterion do |cc|
cc.campaign = client.path.campaign(customer_id, campaign_id)
cc.user_list = client.resource.user_list_info do |info|
info.user_list = user_list
end
end
# Issues a mutate request to create the campaign criterion.
response = client.service.campaign_criterion.mutate_campaign_criteria(
customer_id: customer_id,
operations: [operation],
)
campaign_criterion_resource_name = response.results.first.resource_name
puts "Successfully created campaign criterion with resource name " \
"'#{campaign_criterion_resource_name}' targeting user list with resource name " \
"'#{user_list}' with campaign with ID #{campaign_id}"
campaign_criterion_resource_name
end
# [END setup_remarketing_4]
# Updates the bid modifier on a campaign criterion.
def modify_campaign_bids(
client,
customer_id,
campaign_criterion_resource_name,
bid_modifier_value
)
# Creates the update operation to modify the bid.
operation = client.operation.update_resource.campaign_criterion(
campaign_criterion_resource_name) do |cc|
cc.bid_modifier = bid_modifier_value
end
# Issues a mutate request to update the campaign criterion.
response = client.service.campaign_criterion.mutate_campaign_criteria(
customer_id: customer_id,
operations: [operation],
)
puts "Successfully updated the bid for campaign criterion with resource name " \
"#{response.results.first.resource_name}"
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[:campaign_id] = 'INSERT_CAMPAIGN_ID_HERE'
options[:ad_group_id] = 'INSERT_AD_GROUP_ID_HERE'
# To use a different bid modifier value from the default (1.5), pass the
# value using -B option on the command line.
options[:bid_modifier_value] = 1.5
OptionParser.new do |opts|
opts.banner = sprintf('Usage: %s [options]', File.basename(__FILE__))
opts.separator ''
opts.separator 'Options:'
opts.on('-C', '--customer-id CUSTOMER-ID', String, 'Customer ID') do |v|
options[:customer_id] = v
end
opts.on('-c', '--campaign-id CAMPAIGN-ID', String, 'Campaign ID') do |v|
options[:campaign_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('-B', '--bid-modifier-value BID-MODIFIER-VALUE', Float,
'Bid Modifier Value (Optional)') do |v|
options[:bid_modifier_value] = v
end
opts.separator ''
opts.separator 'Help:'
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
begin
setup_remarketing(
options.fetch(:customer_id).tr("-", ""),
options.fetch(:campaign_id),
options.fetch(:ad_group_id),
options.fetch(:bid_modifier_value),
)
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