/
AddDynamicSearchAds.php
359 lines (324 loc) · 14.3 KB
/
AddDynamicSearchAds.php
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
<?php
/**
* 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.
*/
namespace Google\Ads\GoogleAds\Examples\AdvancedOperations;
require __DIR__ . '/../../vendor/autoload.php';
use GetOpt\GetOpt;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Examples\Utils\Helper;
use Google\Ads\GoogleAds\Lib\V13\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V13\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V13\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\V13\Common\ExpandedDynamicSearchAdInfo;
use Google\Ads\GoogleAds\V13\Common\ManualCpc;
use Google\Ads\GoogleAds\V13\Common\WebpageConditionInfo;
use Google\Ads\GoogleAds\V13\Common\WebpageInfo;
use Google\Ads\GoogleAds\V13\Enums\AdGroupAdStatusEnum\AdGroupAdStatus;
use Google\Ads\GoogleAds\V13\Enums\AdGroupCriterionStatusEnum\AdGroupCriterionStatus;
use Google\Ads\GoogleAds\V13\Enums\AdGroupStatusEnum\AdGroupStatus;
use Google\Ads\GoogleAds\V13\Enums\AdGroupTypeEnum\AdGroupType;
use Google\Ads\GoogleAds\V13\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType;
use Google\Ads\GoogleAds\V13\Enums\BudgetDeliveryMethodEnum\BudgetDeliveryMethod;
use Google\Ads\GoogleAds\V13\Enums\CampaignStatusEnum\CampaignStatus;
use Google\Ads\GoogleAds\V13\Enums\WebpageConditionOperandEnum\WebpageConditionOperand;
use Google\Ads\GoogleAds\V13\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V13\Resources\Ad;
use Google\Ads\GoogleAds\V13\Resources\AdGroup;
use Google\Ads\GoogleAds\V13\Resources\AdGroupCriterion;
use Google\Ads\GoogleAds\V13\Resources\AdGroupAd;
use Google\Ads\GoogleAds\V13\Resources\Campaign;
use Google\Ads\GoogleAds\V13\Resources\Campaign\DynamicSearchAdsSetting;
use Google\Ads\GoogleAds\V13\Resources\CampaignBudget;
use Google\Ads\GoogleAds\V13\Services\AdGroupCriterionOperation;
use Google\Ads\GoogleAds\V13\Services\AdGroupOperation;
use Google\Ads\GoogleAds\V13\Services\AdGroupAdOperation;
use Google\Ads\GoogleAds\V13\Services\CampaignBudgetOperation;
use Google\Ads\GoogleAds\V13\Services\CampaignOperation;
use Google\Ads\GoogleAds\V13\Services\MutateAdGroupAdsResponse;
use Google\Ads\GoogleAds\V13\Services\MutateAdGroupsResponse;
use Google\Ads\GoogleAds\V13\Services\MutateAdGroupCriteriaResponse;
use Google\Ads\GoogleAds\V13\Services\MutateCampaignBudgetsResponse;
use Google\Ads\GoogleAds\V13\Services\MutateCampaignsResponse;
use Google\ApiCore\ApiException;
/**
* This example adds a new dynamic search ad (DSA) and a webpage targeting criterion for the DSA.
*/
class AddDynamicSearchAds
{
private const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE';
public static function main()
{
// Either pass the required parameters for this example on the command line, or insert them
// into the constants above.
$options = (new ArgumentParser())->parseCommandArguments([
ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT
]);
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
// Construct a Google Ads client configured from a properties file and the
// OAuth2 credentials above.
$googleAdsClient = (new GoogleAdsClientBuilder())->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
try {
self::runExample(
$googleAdsClient,
$options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID
);
} catch (GoogleAdsException $googleAdsException) {
printf(
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
$googleAdsException->getRequestId(),
PHP_EOL,
PHP_EOL
);
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
/** @var GoogleAdsError $error */
printf(
"\t%s: %s%s",
$error->getErrorCode()->getErrorCode(),
$error->getMessage(),
PHP_EOL
);
}
exit(1);
} catch (ApiException $apiException) {
printf(
"ApiException was thrown with message '%s'.%s",
$apiException->getMessage(),
PHP_EOL
);
exit(1);
}
}
/**
* Runs the example.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
*/
public static function runExample(
GoogleAdsClient $googleAdsClient,
int $customerId
) {
$budgetResourceName = self::createCampaignBudget($googleAdsClient, $customerId);
$campaignResourceName = self::createCampaign(
$googleAdsClient,
$customerId,
$budgetResourceName
);
$adGroupResourceName = self::createAdGroup(
$googleAdsClient,
$customerId,
$campaignResourceName
);
self::createExpandedDSA($googleAdsClient, $customerId, $adGroupResourceName);
self::createWebPageCriterion($googleAdsClient, $customerId, $adGroupResourceName);
}
/**
* Creates a campaign budget.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
* @return string the campaign budget resource name
*/
private static function createCampaignBudget(
GoogleAdsClient $googleAdsClient,
int $customerId
) {
$campaignBudget = new CampaignBudget([
'name' => 'Interplanetary Cruise Budget #' . Helper::getPrintableDatetime(),
'delivery_method' => BudgetDeliveryMethod::STANDARD,
'amount_micros' => 500000
]);
// Creates a campaign budget operation.
$campaignBudgetOperation = new CampaignBudgetOperation();
$campaignBudgetOperation->setCreate($campaignBudget);
// Issues a mutate request to add campaign budgets.
$campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
/** @var MutateCampaignBudgetsResponse $campaignBudgetResponse */
$campaignBudgetResponse = $campaignBudgetServiceClient->mutateCampaignBudgets(
$customerId,
[$campaignBudgetOperation]
);
$campaignBudgetResourceName = $campaignBudgetResponse->getResults()[0]->getResourceName();
printf("Added budget named '%s'.%s", $campaignBudgetResourceName, PHP_EOL);
return $campaignBudgetResourceName;
}
/**
* Creates a campaign.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
* @param string $campaignBudgetResourceName the resource name of the campaign budget
* @return string the resource name of the newly created campaign
*/
// [START add_dynamic_search_ads]
private static function createCampaign(
GoogleAdsClient $googleAdsClient,
int $customerId,
string $campaignBudgetResourceName
) {
$campaign = new Campaign([
'name' => 'Interplanetary Cruise #' . Helper::getPrintableDatetime(),
'advertising_channel_type' => AdvertisingChannelType::SEARCH,
'status' => CampaignStatus::PAUSED,
'manual_cpc' => new ManualCpc(),
'campaign_budget' => $campaignBudgetResourceName,
// Enables the campaign for DSAs.
'dynamic_search_ads_setting' => new DynamicSearchAdsSetting([
'domain_name' => 'example.com',
'language_code' => 'en'
]),
// Optional: Sets the start and end dates for the campaign, beginning one day from
// now and ending a month from now.
'start_date' => date('Ymd', strtotime('+1 day')),
'end_date' => date('Ymd', strtotime('+1 month'))
]);
// Creates a campaign operation.
$campaignOperation = new CampaignOperation();
$campaignOperation->setCreate($campaign);
// Issues a mutate request to add campaigns.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
/** @var MutateCampaignsResponse $campaignResponse */
$campaignResponse = $campaignServiceClient->mutateCampaigns(
$customerId,
[$campaignOperation]
);
$campaignResourceName = $campaignResponse->getResults()[0]->getResourceName();
printf("Added campaign named '%s'.%s", $campaignResourceName, PHP_EOL);
return $campaignResourceName;
}
// [END add_dynamic_search_ads]
/**
* Creates an ad group.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
* @param string $campaignResourceName the resource name of the campaign
* @return string the resource name of the newly created ad group
*/
// [START add_dynamic_search_ads_1]
private static function createAdGroup(
GoogleAdsClient $googleAdsClient,
int $customerId,
string $campaignResourceName
) {
// Constructs an ad group and sets an optional CPC value.
$adGroup = new AdGroup([
'name' => 'Earth to Mars Cruises #' . Helper::getPrintableDatetime(),
'campaign' => $campaignResourceName,
'status' => AdGroupStatus::PAUSED,
'type' => AdGroupType::SEARCH_DYNAMIC_ADS,
'tracking_url_template' => 'http://tracker.examples.com/traveltracker/{escapedlpurl}',
'cpc_bid_micros' => 10000000
]);
// Creates an ad group operation.
$adGroupOperation = new AdGroupOperation();
$adGroupOperation->setCreate($adGroup);
// Issues a mutate request to add the ad groups.
$adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient();
/** @var MutateAdGroupsResponse $adGroupResponse */
$adGroupResponse = $adGroupServiceClient->mutateAdGroups($customerId, [$adGroupOperation]);
$adGroupResourceName = $adGroupResponse->getResults()[0]->getResourceName();
printf("Added ad group named '%s'.%s", $adGroupResourceName, PHP_EOL);
return $adGroupResourceName;
}
// [END add_dynamic_search_ads_1]
/**
* Creates an expanded dynamic search ad.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
* @param string $adGroupResourceName the ad group resource name
*/
// [START add_dynamic_search_ads_2]
private static function createExpandedDSA(
GoogleAdsClient $googleAdsClient,
int $customerId,
string $adGroupResourceName
) {
$adGroupAd = new AdGroupAd([
'ad_group' => $adGroupResourceName,
'status' => AdGroupAdStatus::PAUSED,
'ad' => new Ad([
'expanded_dynamic_search_ad' => new ExpandedDynamicSearchAdInfo([
'description' => 'Buy tickets now!'
])
])
]);
$adGroupAdOperation = new AdGroupAdOperation();
$adGroupAdOperation->setCreate($adGroupAd);
// Issues a mutate request to add the ad group ads.
$adGroupAdServiceClient = $googleAdsClient->getAdGroupAdServiceClient();
/** @var MutateAdGroupAdsResponse $adGroupAdResponse */
$adGroupAdResponse = $adGroupAdServiceClient->mutateAdGroupAds(
$customerId,
[$adGroupAdOperation]
);
$adGroupAdResourceName = $adGroupAdResponse->getResults()[0]->getResourceName();
printf("Added ad group ad named '%s'.%s", $adGroupAdResourceName, PHP_EOL);
return $adGroupAdResourceName;
}
// [END add_dynamic_search_ads_2]
/**
* Creates a webpage targeting criterion for the DSA.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
* @param string $adGroupResourceName the resource name of the ad group
*/
private static function createWebPageCriterion(
GoogleAdsClient $googleAdsClient,
int $customerId,
string $adGroupResourceName
) {
$adGroupCriterion = new AdGroupCriterion([
'ad_group' => $adGroupResourceName,
'status' => AdGroupCriterionStatus::PAUSED,
'cpc_bid_micros' => 10000000,
// Sets the criterion to match a specific page URL and title.
'webpage' => new WebpageInfo([
'criterion_name' => 'Special Offers',
'conditions' => [
new WebpageConditionInfo([
'operand' => WebpageConditionOperand::URL,
'argument' => '/specialoffers'
]),
new WebpageConditionInfo([
'operand' => WebpageConditionOperand::PAGE_TITLE,
'argument' => 'Special Offers'
])
]
])
]);
$adGroupCriterionOperation = new AdGroupCriterionOperation();
$adGroupCriterionOperation->setCreate($adGroupCriterion);
// Issues a mutate request to add the ad group criterion.
$adGroupCriterionServiceClient = $googleAdsClient->getAdGroupCriterionServiceClient();
/** @var MutateAdGroupCriteriaResponse $adGroupCriterionResponse */
$adGroupCriterionResponse = $adGroupCriterionServiceClient->mutateAdGroupCriteria(
$customerId,
[$adGroupCriterionOperation]
);
$adGroupCriterionResourceName =
$adGroupCriterionResponse->getResults()[0]->getResourceName();
printf("Added ad group criterion named '%s'.%s", $adGroupCriterionResourceName, PHP_EOL);
}
}
AddDynamicSearchAds::main();