/
add_flights_feed.pl
351 lines (297 loc) · 12.3 KB
/
add_flights_feed.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
#!/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 adds a flights feed, creates the associated feed mapping, and
# adds a feed item.
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::V16::Resources::FeedAttribute;
use Google::Ads::GoogleAds::V16::Resources::Feed;
use Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping;
use Google::Ads::GoogleAds::V16::Resources::FeedMapping;
use Google::Ads::GoogleAds::V16::Resources::FeedItemAttributeValue;
use Google::Ads::GoogleAds::V16::Resources::FeedItem;
use Google::Ads::GoogleAds::V16::Enums::FeedAttributeTypeEnum
qw(STRING URL_LIST);
use Google::Ads::GoogleAds::V16::Enums::FlightPlaceholderFieldEnum
qw(FLIGHT_DESCRIPTION DESTINATION_ID FLIGHT_PRICE FLIGHT_SALE_PRICE FINAL_URLS);
use Google::Ads::GoogleAds::V16::Enums::PlaceholderTypeEnum qw(DYNAMIC_FLIGHT);
use Google::Ads::GoogleAds::V16::Services::FeedService::FeedOperation;
use
Google::Ads::GoogleAds::V16::Services::FeedMappingService::FeedMappingOperation;
use Google::Ads::GoogleAds::V16::Services::FeedItemService::FeedItemOperation;
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";
sub add_flights_feed {
my ($api_client, $customer_id) = @_;
# Create a new flights feed.
my $feed_resource_name = create_feed($api_client, $customer_id);
# Get the newly created feed's attributes and package them into a hash. This read
# operation is required to retrieve the attribute IDs.
my $feed_attributes =
get_feed($api_client, $customer_id, $feed_resource_name);
# Create a feed mapping.
create_feed_mapping($api_client, $customer_id, $feed_attributes,
$feed_resource_name);
# Create a feed item.
create_feed_item($api_client, $customer_id, $feed_attributes,
$feed_resource_name);
return 1;
}
# Creates a flight feed.
sub create_feed {
my ($api_client, $customer_id) = @_;
# Create a Flight Description attribute.
my $flight_description_attribute =
Google::Ads::GoogleAds::V16::Resources::FeedAttribute->new({
type => STRING,
name => "Flight Description"
});
# Create a Destination ID attribute.
my $destination_id_attribute =
Google::Ads::GoogleAds::V16::Resources::FeedAttribute->new({
type => STRING,
name => "Destination ID"
});
# Create a Flight Price attribute.
my $flight_price_attribute =
Google::Ads::GoogleAds::V16::Resources::FeedAttribute->new({
type => STRING,
name => "Flight Price"
});
# Create a Flight Sale Price attribute.
my $flight_sales_price_attribute =
Google::Ads::GoogleAds::V16::Resources::FeedAttribute->new({
type => STRING,
name => "Flight Sale Price"
});
# Create a Final URLs attribute.
my $final_urls_Attribute =
Google::Ads::GoogleAds::V16::Resources::FeedAttribute->new({
type => URL_LIST,
name => "Final URLs"
});
# Create a feed.
my $feed = Google::Ads::GoogleAds::V16::Resources::Feed->new({
name => "Flights Feed #" . uniqid(),
attributes => [
$flight_description_attribute, $destination_id_attribute,
$flight_price_attribute, $flight_sales_price_attribute,
$final_urls_Attribute
]});
# Create a feed operation.
my $feed_operation =
Google::Ads::GoogleAds::V16::Services::FeedService::FeedOperation->new(({
create => $feed
}));
# Add the feed.
my $feeds_response = $api_client->FeedService()->mutate({
customerId => $customer_id,
operations => [$feed_operation]});
my $feed_resource_name = $feeds_response->{results}[0]{resourceName};
printf "Feed with resource name '%s' was created.\n", $feed_resource_name;
return $feed_resource_name;
}
# Retrieves details about a feed. The initial query retrieves the FeedAttributes, or columns,
# of the feed. Each FeedAttribute will also include the FeedAttributeId, which will be used in
# a subsequent step. The example then inserts a new key, value pair into a hash for each
# FeedAttribute, which is the return value of the method. The keys are the placeholder types
# that the columns will be. The values are the FeedAttributes.
sub get_feed {
my ($api_client, $customer_id, $feed_resource_name) = @_;
# Construct the search query.
my $search_query =
sprintf "SELECT feed.attributes FROM feed WHERE feed.resource_name = '%s'",
$feed_resource_name;
my $search_response = $api_client->GoogleAdsService()->search({
customerId => $customer_id,
query => $search_query,
pageSize => PAGE_SIZE
});
# Get the first result because we only need the single feed item we created previously.
my $google_ads_row = $search_response->{results}[0];
# Get the attributes list from the feed and create a hash with keys of each attribute and
# values of each corresponding ID.
my $feed_attribute_list = $google_ads_row->{feed}{attributes};
# Create a hash to return.
my $feed_attributes = {};
# Loop through the feed attributes to populate the hash.
foreach my $feed_attribute (@$feed_attribute_list) {
my $feed_attribute_name = $feed_attribute->{name};
# The full list of FlightPlaceholderFields can be found here
# https://developers.google.com/google-ads/api/reference/rpc/latest/FlightPlaceholderFieldEnum.FlightPlaceholderField.
if ($feed_attribute_name eq "Flight Description") {
$feed_attributes->{FLIGHT_DESCRIPTION} = $feed_attribute;
} elsif ($feed_attribute_name eq "Destination ID") {
$feed_attributes->{DESTINATION_ID} = $feed_attribute;
} elsif ($feed_attribute_name eq "Flight Price") {
$feed_attributes->{FLIGHT_PRICE} = $feed_attribute;
} elsif ($feed_attribute_name eq "Flight Sale Price") {
$feed_attributes->{FLIGHT_SALE_PRICE} = $feed_attribute;
} elsif ($feed_attribute_name eq "Final URLs") {
$feed_attributes->{FINAL_URLS} = $feed_attribute;
} else {
die("Invalid attribute name.");
}
}
return $feed_attributes;
}
# Creates a feed mapping for a given feed.
sub create_feed_mapping {
my ($api_client, $customer_id, $feed_attributes, $feed_resource_name) = @_;
# Map the FeedAttributeIds to the fieldId constants.
my $flight_description_mapping =
Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping->new({
feedAttributeId => $feed_attributes->{FLIGHT_DESCRIPTION}{id},
flightField => FLIGHT_DESCRIPTION
});
my $destination_id_mapping =
Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping->new({
feedAttributeId => $feed_attributes->{DESTINATION_ID}{id},
flightField => DESTINATION_ID
});
my $flight_price_mapping =
Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping->new({
feedAttributeId => $feed_attributes->{FLIGHT_PRICE}{id},
flightField => FLIGHT_PRICE
});
my $flight_sale_price_mapping =
Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping->new({
feedAttributeId => $feed_attributes->{FLIGHT_SALE_PRICE}{id},
flightField => FLIGHT_SALE_PRICE
});
my $final_urls_mapping =
Google::Ads::GoogleAds::V16::Resources::AttributeFieldMapping->new({
feedAttributeId => $feed_attributes->{FINAL_URLS}{id},
flightField => FINAL_URLS
});
# Create a feed mapping.
my $feed_mapping = Google::Ads::GoogleAds::V16::Resources::FeedMapping->new({
placeholderType => DYNAMIC_FLIGHT,
feed => $feed_resource_name,
attributeFieldMappings => [
$flight_description_mapping, $destination_id_mapping,
$flight_price_mapping, $flight_sale_price_mapping,
$final_urls_mapping
]});
# Create a feed mapping operation.
my $feed_mapping_operation =
Google::Ads::GoogleAds::V16::Services::FeedMappingService::FeedMappingOperation
->new({
create => $feed_mapping
});
# Add the feed mapping.
my $feed_mappings_response = $api_client->FeedMappingService()->mutate({
customerId => $customer_id,
operations => [$feed_mapping_operation]});
printf "Created feed mapping with resource name '%s'.\n",
$feed_mappings_response->{results}[0]{resourceName};
}
# Adds a new item to the feed.
sub create_feed_item {
my ($api_client, $customer_id, $feed_attributes, $feed_resource_name) = @_;
# Create the flight description feed attribute value.
my $flight_description =
Google::Ads::GoogleAds::V16::Resources::FeedItemAttributeValue->new({
feedAttributeId => $feed_attributes->{FLIGHT_DESCRIPTION}{id},
stringValue => "Earth to Mars"
});
# Create the destination ID feed attribute value.
my $destination_id =
Google::Ads::GoogleAds::V16::Resources::FeedItemAttributeValue->new({
feedAttributeId => $feed_attributes->{DESTINATION_ID}{id},
stringValue => "Mars"
});
# Create the flight price feed attribute value.
my $flight_price =
Google::Ads::GoogleAds::V16::Resources::FeedItemAttributeValue->new({
feedAttributeId => $feed_attributes->{FLIGHT_PRICE}{id},
stringValue => "499.99 USD"
});
# Create the flight sale price feed attribute value.
my $flight_sale_price =
Google::Ads::GoogleAds::V16::Resources::FeedItemAttributeValue->new({
feedAttributeId => $feed_attributes->{FLIGHT_SALE_PRICE}{id},
stringValue => "299.99 USD"
});
# Create the final URLs feed attribute value.
my $final_urls =
Google::Ads::GoogleAds::V16::Resources::FeedItemAttributeValue->new({
feedAttributeId => $feed_attributes->{FINAL_URLS}{id},
stringValues => ["http://www.example.com/flights/"]});
# Create a feed item, specifying the Feed ID and the attributes created above.
my $feed_item = Google::Ads::GoogleAds::V16::Resources::FeedItem->new({
feed => $feed_resource_name,
attributeValues => [
$flight_description, $destination_id, $flight_price,
$flight_sale_price, $final_urls
]});
# Create a feed item operation.
my $feed_item_operation =
Google::Ads::GoogleAds::V16::Services::FeedItemService::FeedItemOperation->
new({
create => $feed_item
});
# Add the feed item.
my $feed_items_response = $api_client->FeedItemService()->mutate({
customerId => $customer_id,
operations => [$feed_item_operation]});
printf "Created feed item with resource name '%s'.\n",
$feed_items_response->{results}[0]{resourceName};
}
# 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);
# 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);
# Call the example.
add_flights_feed($api_client, $customer_id =~ s/-//gr);
=pod
=head1 NAME
add_flights_feed
=head1 DESCRIPTION
This example adds a flights feed, creates the associated feed mapping, and adds
a feed item.
=head1 SYNOPSIS
add_flights_feed.pl [options]
-help Show the help message.
-customer_id The Google Ads customer ID.
=cut