-
Notifications
You must be signed in to change notification settings - Fork 17
/
add_things_to_do_ad.pl
290 lines (240 loc) · 10.3 KB
/
add_things_to_do_ad.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
#!/usr/bin/perl -w
#
# Copyright 2023, 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 creates a Things to do campaign, an ad group and a Things to
# do ad.
#
# Prerequisite: You need to have access to the Things to do Center.
# The integration instructions can be found at:
# https://support.google.com/google-ads/answer/13387362
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::V17::Resources::CampaignBudget;
use Google::Ads::GoogleAds::V17::Resources::Campaign;
use Google::Ads::GoogleAds::V17::Resources::NetworkSettings;
use Google::Ads::GoogleAds::V17::Resources::AdGroup;
use Google::Ads::GoogleAds::V17::Resources::AdGroupAd;
use Google::Ads::GoogleAds::V17::Resources::Ad;
use Google::Ads::GoogleAds::V17::Resources::TravelCampaignSettings;
use Google::Ads::GoogleAds::V17::Common::MaximizeConversionValue;
use Google::Ads::GoogleAds::V17::Common::TravelAdInfo;
use Google::Ads::GoogleAds::V17::Enums::BudgetDeliveryMethodEnum qw(STANDARD);
use Google::Ads::GoogleAds::V17::Enums::CampaignStatusEnum;
use Google::Ads::GoogleAds::V17::Enums::AdGroupTypeEnum qw(TRAVEL_ADS);
use Google::Ads::GoogleAds::V17::Enums::AdGroupStatusEnum;
use Google::Ads::GoogleAds::V17::Enums::AdGroupAdStatusEnum;
use Google::Ads::GoogleAds::V17::Enums::AdvertisingChannelTypeEnum qw(TRAVEL);
use Google::Ads::GoogleAds::V17::Enums::AdvertisingChannelSubTypeEnum
qw(TRAVEL_ACTIVITIES);
use
Google::Ads::GoogleAds::V17::Services::CampaignBudgetService::CampaignBudgetOperation;
use Google::Ads::GoogleAds::V17::Services::CampaignService::CampaignOperation;
use Google::Ads::GoogleAds::V17::Services::AdGroupService::AdGroupOperation;
use Google::Ads::GoogleAds::V17::Services::AdGroupAdService::AdGroupAdOperation;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);
sub add_things_to_do_ad {
my ($api_client, $customer_id, $things_to_do_center_account_id) = @_;
# Create a budget to be used by the campaign that will be created below.
my $budget_resource_name = add_campaign_budget($api_client, $customer_id);
# Create a Things to do campaign.
my $campaign_resource_name =
add_things_to_do_campaign($api_client, $customer_id,
$budget_resource_name, $things_to_do_center_account_id);
# Create an ad group.
my $ad_group_resource_name =
add_ad_group($api_client, $customer_id, $campaign_resource_name);
# Create an ad group ad.
add_ad_group_ad($api_client, $customer_id, $ad_group_resource_name);
return 1;
}
# Creates a new campaign budget in the specified client account.
sub add_campaign_budget {
my ($api_client, $customer_id) = @_;
# Create a campaign budget.
my $campaign_budget =
Google::Ads::GoogleAds::V17::Resources::CampaignBudget->new({
name => "Interplanetary Cruise Budget #" . uniqid(),
deliveryMethod => STANDARD,
# Set the amount of budget.
amountMicros => 5000000,
# Make the budget explicitly shared.
explicitlyShared => "true"
});
# Create a campaign budget operation.
my $campaign_budget_operation =
Google::Ads::GoogleAds::V17::Services::CampaignBudgetService::CampaignBudgetOperation
->new({create => $campaign_budget});
# Add the campaign budget.
my $campaign_budget_resource_name =
$api_client->CampaignBudgetService()->mutate({
customerId => $customer_id,
operations => [$campaign_budget_operation]})->{results}[0]{resourceName};
printf "Added a budget with resource name: '%s'.\n",
$campaign_budget_resource_name;
return $campaign_budget_resource_name;
}
# Creates a new Things to do campaign in the specified client account.
# [START add_things_to_do_ad]
sub add_things_to_do_campaign {
my ($api_client, $customer_id, $budget_resource_name,
$things_to_do_center_account_id)
= @_;
# [START add_things_to_do_ad_1]
# Create a campaign.
my $campaign = Google::Ads::GoogleAds::V17::Resources::Campaign->new({
name => "Interplanetary Cruise Campaign #" . uniqid(),
# Configure settings related to Things to do campaigns including
# advertising channel type, advertising channel sub type and travel
# campaign settings.
advertisingChannelType => TRAVEL,
advertisingChannelSubType => TRAVEL_ACTIVITIES,
travelCampaignSettings =>
Google::Ads::GoogleAds::V17::Resources::TravelCampaignSettings->new({
travelAccountId => $things_to_do_center_account_id
}
),
# Recommendation: Set the campaign to PAUSED when creating it to prevent
# the ads from immediately serving. Set to ENABLED once you've added
# targeting and the ads are ready to serve.
status => Google::Ads::GoogleAds::V17::Enums::CampaignStatusEnum::PAUSED,
# Set the bidding strategy to MaximizeConversionValue. Only this type can be
# used for Things to do campaigns.
maximizeConversionValue =>
Google::Ads::GoogleAds::V17::Common::MaximizeConversionValue->new(),
# Set the budget.
campaignBudget => $budget_resource_name,
# Configure the campaign network options. Only Google Search is allowed for
# Things to do campaigns.
networkSettings =>
Google::Ads::GoogleAds::V17::Resources::NetworkSettings->new({
targetGoogleSearch => "true"
})});
# [END add_things_to_do_ad_1]
# Create a campaign operation.
my $campaign_operation =
Google::Ads::GoogleAds::V17::Services::CampaignService::CampaignOperation->
new({create => $campaign});
# Add the campaign.
my $campaign_resource_name = $api_client->CampaignService()->mutate({
customerId => $customer_id,
operations => [$campaign_operation]})->{results}[0]{resourceName};
printf "Added a Things to do campaign with resource name: '%s'.\n",
$campaign_resource_name;
return $campaign_resource_name;
}
# [END add_things_to_do_ad]
# Creates a new ad group in the specified Things to do campaign.
# [START add_things_to_do_ad_2]
sub add_ad_group {
my ($api_client, $customer_id, $campaign_resource_name) = @_;
# Create an ad group.
my $ad_group = Google::Ads::GoogleAds::V17::Resources::AdGroup->new({
name => "Earth to Mars Cruise #" . uniqid(),
# Set the campaign.
campaign => $campaign_resource_name,
# Set the ad group type to TRAVEL_ADS.
# This cannot be set to other types.
type => TRAVEL_ADS,
status => Google::Ads::GoogleAds::V17::Enums::AdGroupStatusEnum::ENABLED
});
# Create an ad group operation.
my $ad_group_operation =
Google::Ads::GoogleAds::V17::Services::AdGroupService::AdGroupOperation->
new({create => $ad_group});
# Add the ad group.
my $ad_group_resource_name = $api_client->AdGroupService()->mutate({
customerId => $customer_id,
operations => [$ad_group_operation]})->{results}[0]{resourceName};
printf "Added an ad group with resource name: '%s'.\n",
$ad_group_resource_name;
return $ad_group_resource_name;
}
# [END add_things_to_do_ad_2]
# Creates a new ad group ad in the specified ad group.
# [START add_things_to_do_ad_3]
sub add_ad_group_ad {
my ($api_client, $customer_id, $ad_group_resource_name) = @_;
# Create an ad group ad and set a travel ad info.
my $ad_group_ad = Google::Ads::GoogleAds::V17::Resources::AdGroupAd->new({
# Set the ad group.
adGroup => $ad_group_resource_name,
ad => Google::Ads::GoogleAds::V17::Resources::Ad->new({
travelAd => Google::Ads::GoogleAds::V17::Common::TravelAdInfo->new()}
),
# Set the ad group to enabled. Setting this to paused will cause an error
# for Things to do campaigns. Pausing should happen at either the ad group
# or campaign level.
status => Google::Ads::GoogleAds::V17::Enums::AdGroupAdStatusEnum::ENABLED
});
# Create an ad group ad operation.
my $ad_group_ad_operation =
Google::Ads::GoogleAds::V17::Services::AdGroupAdService::AdGroupAdOperation
->new({create => $ad_group_ad});
# Add the ad group ad.
my $ad_group_ad_resource_name = $api_client->AdGroupAdService()->mutate({
customerId => $customer_id,
operations => [$ad_group_ad_operation]})->{results}[0]{resourceName};
printf "Added an ad group ad with resource name: '%s'.\n",
$ad_group_ad_resource_name;
return $ad_group_ad_resource_name;
}
# [END add_things_to_do_ad_3]
# 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);
my $customer_id = undef;
my $things_to_do_center_account_id = undef;
# Parameters passed on the command line will override any parameters set in code.
GetOptions(
"customer_id=s" => \$customer_id,
"things_to_do_center_account_id=i" => \$things_to_do_center_account_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, $things_to_do_center_account_id);
# Call the example.
add_things_to_do_ad(
$api_client,
$customer_id =~ s/-//gr,
$things_to_do_center_account_id
);
=pod
=head1 NAME
add_things_to_do_ad
=head1 DESCRIPTION
This example creates a Things to do campaign, an ad group and a Things to do ad.
Prerequisite: You need to have an access to the Things to Do Center. The integration
instructions can be found at: https://support.google.com/google-ads/answer/13387362.
=head1 SYNOPSIS
add_things_to_do_ad.pl [options]
-help Show the help message.
-customer_id The Google Ads customer ID.
-things_to_do_center_account_id The Things to Do Center account ID.
=cut