/
add_dynamic_page_feed.rb
309 lines (263 loc) · 9.13 KB
/
add_dynamic_page_feed.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
#!/usr/bin/env ruby
# Encoding: utf-8
#
# Copyright:: Copyright 2019, Google Inc. All Rights Reserved.
#
# License:: 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 a page feed to specify precisely which URLs to use with
# your Dynamic Search Ads campaign. To use a Dynamic Search Ads campaign, run
# add_dynamic_search_ads_campaign.rb. To get campaigns, run get_campaigns.rb.
require 'optparse'
require 'google/ads/google_ads'
FeedDetails = Struct.new(
:resource_name,
:url_attribute_id,
:label_attribute_id
)
def add_dynamic_page_feed(customer_id, campaign_id, ad_group_id)
client = Google::Ads::GoogleAds::GoogleAdsClient.new
dsa_page_url_label = "discounts"
feed_details = create_feed(client, customer_id)
create_feed_mapping(client, customer_id, feed_details)
create_feed_items(client, customer_id, feed_details, dsa_page_url_label)
update_campaign_dsa_setting(client, customer_id, campaign_id, feed_details)
ad_group_resource_name = client.path.ad_group(customer_id, ad_group_id)
add_dsa_targeting(
client,
customer_id,
ad_group_resource_name,
dsa_page_url_label
)
end
def create_feed(client, customer_id)
feed_attribute_url = client.resource.feed_attribute do |a|
a.type = :URL_LIST
a.name = "Page URL"
end
feed_attribute_label = client.resource.feed_attribute do |a|
a.type = :STRING_LIST
a.name = "Label"
end
feed = client.resource.feed do |f|
f.name = "DSA Feed #{(Time.now.to_f * 1000).to_i}"
f.attributes << feed_attribute_url
f.attributes << feed_attribute_label
f.origin = :USER
end
operation = client.operation.create_resource.feed(feed)
response = client.service.feed.mutate_feeds(
customer_id: customer_id,
operations: [operation],
)
feed_id = response.results.first.resource_name
# We need to look up the attribute name and IDs for the feed we just created
# so that we can give them back to the API for construction of feed mappings
# in the next function.
query = <<~EOD
SELECT
feed.attributes
FROM
feed
WHERE
feed.resource_name = '#{feed_id}'
LIMIT
100
EOD
response = client.service.google_ads.search(
customer_id: customer_id,
query: query,
)
# Hash.[] takes an aray of pairs and turns them in to a hash with keys
# equal to the first item, and values equal to the second item, we get two
# items back one with ["Page URL", id_for_url] and another with
# ["Label", id_for_label], so this expression gives us a convenient way to
# look them back up
attribute_lookup = Hash[
response.first.feed.attributes.map { |attribute|
[attribute.name, attribute.id]
}
]
# Finally, construct a FeedDetails object so that we have a nice small
# wrapper around the details we care about for the next phase.
FeedDetails.new(
feed_id,
attribute_lookup.fetch("Page URL"),
attribute_lookup.fetch("Label"),
)
end
def create_feed_mapping(client, customer_id, feed_details)
url_field_mapping = client.resource.attribute_field_mapping do |mapping|
mapping.feed_attribute_id = feed_details.url_attribute_id
mapping.dsa_page_feed_field = :PAGE_URL
end
label_field_mapping = client.resource.attribute_field_mapping do |mapping|
mapping.feed_attribute_id = feed_details.label_attribute_id
mapping.dsa_page_feed_field = :LABEL
end
feed_mapping = client.resource.feed_mapping do |mapping|
mapping.criterion_type = :DSA_PAGE_FEED
mapping.feed = feed_details.resource_name
mapping.attribute_field_mappings << url_field_mapping
mapping.attribute_field_mappings << label_field_mapping
end
operation = client.operation.create_resource.feed_mapping(feed_mapping)
response = client.service.feed_mapping.mutate_feed_mappings(
customer_id: customer_id,
operations: [operation],
)
puts "Feed mapping created with id #{response.results.first.resource_name}"
end
def create_feed_items(client, customer_id, feed_details, label)
urls = [
"http://www.example.com/discounts/rental-cars",
"http://www.example.com/discounts/hotel-deals",
"http://www.example.com/discounts/flight-deals",
]
feed_items = urls.map { |url|
client.resource.feed_item do |fi|
fi.feed = feed_details.resource_name
fi.attribute_values << client.resource.feed_item_attribute_value do |val|
val.feed_attribute_id = feed_details.url_attribute_id
val.string_values << url
end
fi.attribute_values << client.resource.feed_item_attribute_value do |val|
val.feed_attribute_id = feed_details.label_attribute_id
val.string_values << label
end
end
}
ops = feed_items.map { |fi|
client.operation.create_resource.feed_item(fi)
}
response = client.service.feed_item.mutate_feed_items(
customer_id: customer_id,
operations: ops,
)
response.results.each do |result|
puts "Created feed item with id #{result.resource_name}"
end
end
# [START add_dynamic_page_feed]
def update_campaign_dsa_setting(client, customer_id, campaign_id, feed_details)
query = <<~EOD
SELECT
campaign.id,
campaign.name,
campaign.dynamic_search_ads_setting.feeds
FROM
campaign
WHERE
campaign.id = #{campaign_id}
LIMIT 1000
EOD
response = client.service.google_ads.search(
customer_id: customer_id,
query: query,
)
result = response.first
# [END add_dynamic_page_feed]
if result.nil?
raise "Campaign with id #{id} not found"
end
# [START add_dynamic_page_feed_1]
campaign = result.campaign
op = client.operation.update_resource.campaign(campaign) do
campaign.dynamic_search_ads_setting.feeds << feed_details.resource_name
end
response = client.service.campaign.mutate_campaigns(
customer_id: customer_id,
operations: [op],
)
puts "Updated campaign #{response.results.first.resource_name}"
# [END add_dynamic_page_feed_1]
end
# [START add_dynamic_page_feed_2]
def add_dsa_targeting(client, customer_id, ad_group_resource_name, label)
webpage_condition_info = client.resource.webpage_condition_info do |wci|
wci.operand = :CUSTOM_LABEL
wci.argument = label
end
webpage_criterion = client.resource.webpage_info do |wi|
wi.criterion_name = "Test criterion"
wi.conditions << webpage_condition_info
end
ad_group_criterion = client.resource.ad_group_criterion do |agc|
agc.ad_group = ad_group_resource_name
agc.webpage = webpage_criterion
agc.cpc_bid_micros = 1_500_000
end
op = client.operation.create_resource.ad_group_criterion(ad_group_criterion)
response = client.service.ad_group_criterion.mutate_ad_group_criteria(
customer_id: customer_id,
operations: [op],
)
puts "Created ad group criterion with id: #{response.results.first.resource_name}"
end
# [END add_dynamic_page_feed_2]
if __FILE__ == $0
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'
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.separator ''
opts.separator 'Help:'
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
begin
add_dynamic_page_feed(
options.fetch(:customer_id).tr("-", ""),
options.fetch(:campaign_id),
options.fetch(:ad_group_id),
)
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