-
Notifications
You must be signed in to change notification settings - Fork 770
/
AddDynamicPageFeed.php
executable file
·455 lines (392 loc) · 16 KB
/
AddDynamicPageFeed.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* 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.
*/
namespace Google\AdsApi\Examples\AdWords\v201809\AdvancedOperations;
require __DIR__ . '/../../../../vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionOperation;
use Google\AdsApi\AdWords\v201809\cm\AdGroupCriterionService;
use Google\AdsApi\AdWords\v201809\cm\AttributeFieldMapping;
use Google\AdsApi\AdWords\v201809\cm\BiddableAdGroupCriterion;
use Google\AdsApi\AdWords\v201809\cm\BiddingStrategyConfiguration;
use Google\AdsApi\AdWords\v201809\cm\Campaign;
use Google\AdsApi\AdWords\v201809\cm\CampaignOperation;
use Google\AdsApi\AdWords\v201809\cm\CampaignService;
use Google\AdsApi\AdWords\v201809\cm\CpcBid;
use Google\AdsApi\AdWords\v201809\cm\DynamicSearchAdsSetting;
use Google\AdsApi\AdWords\v201809\cm\Feed;
use Google\AdsApi\AdWords\v201809\cm\FeedAttribute;
use Google\AdsApi\AdWords\v201809\cm\FeedAttributeType;
use Google\AdsApi\AdWords\v201809\cm\FeedItem;
use Google\AdsApi\AdWords\v201809\cm\FeedItemAttributeValue;
use Google\AdsApi\AdWords\v201809\cm\FeedItemOperation;
use Google\AdsApi\AdWords\v201809\cm\FeedItemService;
use Google\AdsApi\AdWords\v201809\cm\FeedMapping;
use Google\AdsApi\AdWords\v201809\cm\FeedMappingOperation;
use Google\AdsApi\AdWords\v201809\cm\FeedMappingService;
use Google\AdsApi\AdWords\v201809\cm\FeedOperation;
use Google\AdsApi\AdWords\v201809\cm\FeedOrigin;
use Google\AdsApi\AdWords\v201809\cm\FeedService;
use Google\AdsApi\AdWords\v201809\cm\Money;
use Google\AdsApi\AdWords\v201809\cm\Operator;
use Google\AdsApi\AdWords\v201809\cm\PageFeed;
use Google\AdsApi\AdWords\v201809\cm\Predicate;
use Google\AdsApi\AdWords\v201809\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\AdWords\v201809\cm\Webpage;
use Google\AdsApi\AdWords\v201809\cm\WebpageCondition;
use Google\AdsApi\AdWords\v201809\cm\WebpageConditionOperand;
use Google\AdsApi\AdWords\v201809\cm\WebpageParameter;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use InvalidArgumentException;
/**
* This code example adds a page feed to specify precisely which URLs to use
* with your Dynamic Search Ads campaign. To create a Dynamic Search Ads
* campaign, run AddDynamicSearchAdsCampaign.php. To get campaigns, run
* GetCampaigns.php.
*/
class AddDynamicPageFeed
{
const CAMPAIGN_ID = 'INSERT_CAMPAIGN_ID_HERE';
const AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE';
// The criterion type to be used for DSA page feeds. DSA page feeds use
// criterionType field instead of the placeholderType field unlike most other
// feed types.
const DSA_PAGE_FEED_CRITERION_TYPE = 61;
// ID that corresponds to the page URLs.
const DSA_PAGE_URLS_FIELD_ID = 1;
// ID that corresponds to the labels.
const DSA_LABEL_FIELD_ID = 2;
public static function runExample(
AdWordsServices $adWordsServices,
AdWordsSession $session,
$campaignId,
$adGroupId
) {
$dsaPageUrlLabel = 'discounts';
// Get the page feed details. This code example creates a new feed, but you
// can fetch and re-use an existing feed.
$feedDetails = self::createFeed($adWordsServices, $session);
self::createFeedMapping($adWordsServices, $session, $feedDetails);
self::createFeedItems(
$adWordsServices,
$session,
$feedDetails,
$dsaPageUrlLabel
);
// Associate the page feed with the campaign.
self::updateCampaignDsaSetting(
$adWordsServices,
$session,
$campaignId,
$feedDetails
);
// Optional: Target web pages matching the feed's label in the ad group.
self::addDsaTargeting(
$adWordsServices,
$session,
$adGroupId,
$dsaPageUrlLabel
);
printf(
"Dynamic page feed setup is complete for campaign ID %d.\n",
$campaignId
);
}
/**
* Creates the feed for DSA page URLs.
*/
private static function createFeed(
AdWordsServices $adWordsServices,
AdWordsSession $session
) {
$feedService = $adWordsServices->get($session, FeedService::class);
// Create feed attributes.
$urlAttribute = new FeedAttribute();
$urlAttribute->setType(FeedAttributeType::URL_LIST);
$urlAttribute->setName('Page URL');
$labelAttribute = new FeedAttribute();
$labelAttribute->setType(FeedAttributeType::STRING_LIST);
$labelAttribute->setName('Label');
// Create the feed.
$dsaPageFeed = new Feed();
$dsaPageFeed->setName('DSA Feed #' . uniqid());
$dsaPageFeed->setAttributes([$urlAttribute, $labelAttribute]);
$dsaPageFeed->setOrigin(FeedOrigin::USER);
// Create the feed operation and add it on the server.
$operation = new FeedOperation();
$operation->setOperator(Operator::ADD);
$operation->setOperand($dsaPageFeed);
$result = $feedService->mutate([$operation]);
// Holds the feeds metadata.
$feedDetails = new DSAFeedDetails();
$savedFeed = $result->getValue()[0];
$feedDetails->feedId = $savedFeed->getId();
$savedAttributes = $savedFeed->getAttributes();
$feedDetails->urlAttributeId = $savedAttributes[0]->getId();
$feedDetails->labelAttributeId = $savedAttributes[1]->getId();
// Print out some information about the created feed.
printf(
"Feed with name '%s', ID %d with urlAttributeId %d and labelAttributeId %d was created.\n",
$savedFeed->getName(),
$feedDetails->feedId,
$feedDetails->urlAttributeId,
$feedDetails->labelAttributeId
);
return $feedDetails;
}
/**
* Creates the feed mapping for the DSA page feeds.
*/
private static function createFeedMapping(
AdWordsServices $adWordsServices,
AdWordsSession $session,
DSAFeedDetails $feedDetails
) {
$feedMappingService = $adWordsServices->get($session, FeedMappingService::class);
// Map the feed attribute IDs to the field ID constants.
$urlFieldMapping = new AttributeFieldMapping();
$urlFieldMapping->setFeedAttributeId(
$feedDetails->urlAttributeId
);
$urlFieldMapping->setFieldId(self::DSA_PAGE_URLS_FIELD_ID);
$labelFieldMapping = new AttributeFieldMapping();
$labelFieldMapping->setFeedAttributeId(
$feedDetails->labelAttributeId
);
$labelFieldMapping->setFieldId(self::DSA_LABEL_FIELD_ID);
// Create the feed mapping and feed mapping operation.
$feedMapping = new FeedMapping();
$feedMapping->setCriterionType(self::DSA_PAGE_FEED_CRITERION_TYPE);
$feedMapping->setFeedId($feedDetails->feedId);
$feedMapping->setAttributeFieldMappings(
[$urlFieldMapping, $labelFieldMapping]
);
$operation = new FeedMappingOperation();
$operation->setOperand($feedMapping);
$operation->setOperator(Operator::ADD);
// Create the feed mapping operation on the server and print out some
// information.
$result = $feedMappingService->mutate([$operation]);
$feedMapping = $result->getValue()[0];
printf(
"Feed mapping with ID %d and criterion type %d was saved for feed with ID %d.\n",
$feedMapping->getFeedMappingId(),
$feedMapping->getCriterionType(),
$feedMapping->getFeedId()
);
}
/**
* Creates the page URLs in the DSA page feed.
*/
private static function createFeedItems(
AdWordsServices $adWordsServices,
AdWordsSession $session,
DSAFeedDetails $feedDetails,
$labelName
) {
$feedItemService = $adWordsServices->get($session, FeedItemService::class);
// Create operations to add feed items.
$rentalCars = self::createDsaUrlAddOperation(
$feedDetails,
'http://www.example.com/discounts/rental-cars',
$labelName
);
$hotelDeals = self::createDsaUrlAddOperation(
$feedDetails,
'http://www.example.com/discounts/hotel-deals',
$labelName
);
$flightDeals = self::createDsaUrlAddOperation(
$feedDetails,
'http://www.example.com/discounts/flight-deals',
$labelName
);
// Add feed item operations on the server and print out some information.
$result = $feedItemService->mutate(
[$rentalCars, $hotelDeals, $flightDeals]
);
foreach ($result->getValue() as $feedItem) {
printf(
"Feed item with feed item ID %d was added.\n",
$feedItem->getFeedItemId()
);
}
}
/**
* Creates a feed item operation to add the DSA URL.
*/
private static function createDsaUrlAddOperation(
DSAFeedDetails $feedDetails,
$url,
$labelName
) {
// Create the FeedItemAttributeValues for the URL and label.
$urlAttributeValue = new FeedItemAttributeValue();
$urlAttributeValue->setFeedAttributeId($feedDetails->urlAttributeId);
// See https://support.google.com/adwords/answer/7166527 for page feed
// URL recommendations and rules.
$urlAttributeValue->setStringValues([$url]);
$labelAttributeValue = new FeedItemAttributeValue();
$labelAttributeValue->setFeedAttributeId(
$feedDetails->labelAttributeId
);
$labelAttributeValue->setStringValues([$labelName]);
// Create the feed item.
$feedItem = new FeedItem();
$feedItem->setFeedId($feedDetails->feedId);
$feedItem->setAttributeValues([$urlAttributeValue, $labelAttributeValue]);
// Create the feed item operation.
$operation = new FeedItemOperation();
$operation->setOperand($feedItem);
$operation->setOperator(Operator::ADD);
return $operation;
}
/**
* Updates the campaign DSA setting to add DSA pagefeeds.
*/
private static function updateCampaignDsaSetting(
AdWordsServices $adWordsServices,
AdWordsSession $session,
$campaignId,
DSAFeedDetails $feedDetails
) {
$campaignService = $adWordsServices->get($session, CampaignService::class);
// Create selector.
$selector = new Selector();
$selector->setFields(['Id', 'Settings']);
$selector->setPredicates(
[new Predicate('CampaignId', PredicateOperator::IN, [$campaignId])]
);
$result = $campaignService->get($selector);
if (empty($result->getEntries()) || $result->getTotalNumEntries() === 0) {
throw new InvalidArgumentException(
'No campaign found with ID: ' . $campaignId
);
}
$campaign = $result->getEntries()[0];
if ($campaign->getSettings() === null) {
throw new InvalidArgumentException(
'Campaign with ID ' . $campaignId . ' is not a DSA campaign.'
);
}
$dsaSetting = null;
foreach ($campaign->getSettings() as $setting) {
if ($setting instanceof DynamicSearchAdsSetting) {
$dsaSetting = $setting;
break;
}
}
if ($dsaSetting === null) {
throw new InvalidArgumentException(
'Campaign with ID ' . $campaignId . ' is not a DSA campaign.'
);
}
// Use a page feed to specify precisely which URLs to use with your
// Dynamic Search Ads.
$pageFeed = new PageFeed();
$pageFeed->setFeedIds([$feedDetails->feedId]);
$dsaSetting->setPageFeed($pageFeed);
// Optional: Specify whether only the supplied URLs should be used with your
// Dynamic Search Ads.
$dsaSetting->setUseSuppliedUrlsOnly(true);
$updatedCampaign = new Campaign();
$updatedCampaign->setId($campaignId);
$updatedCampaign->setSettings($campaign->getSettings());
$operation = new CampaignOperation();
$operation->setOperand($updatedCampaign);
$operation->setOperator(Operator::SET);
// Update the campaign on the server and print out some information.
$result = $campaignService->mutate([$operation]);
$updatedCampaign = $result->getValue()[0];
printf(
"DSA page feed for campaign ID %d was updated with feed ID %d.\n",
$updatedCampaign->getId(),
$feedDetails->feedId
);
}
/**
* Sets custom targeting for the page feed URLs based on a list of labels.
*/
private static function addDsaTargeting(
AdWordsServices $adWordsServices,
AdWordsSession $session,
$adGroupId,
$dsaPageUrlLabel
) {
$adGroupCriterionService = $adWordsServices->get($session, AdGroupCriterionService::class);
// Create a webpage criterion.
$webpage = new Webpage();
$parameter = new WebpageParameter();
$parameter->setCriterionName('Test criterion');
$webpage->setParameter($parameter);
// Add a condition for label=specified_label_name.
$condition = new WebpageCondition();
$condition->setOperand(WebpageConditionOperand::CUSTOM_LABEL);
$condition->setArgument($dsaPageUrlLabel);
$parameter->setConditions([$condition]);
$criterion = new BiddableAdGroupCriterion();
$criterion->setAdGroupId($adGroupId);
$criterion->setCriterion($webpage);
// Set a custom bid for this criterion.
$biddingStrategyConfiguration = new BiddingStrategyConfiguration();
$cpcBid = new CpcBid();
$money = new Money();
$money->setMicroAmount(1500000);
$cpcBid->setBid($money);
$biddingStrategyConfiguration->setBids([$cpcBid]);
$criterion->setBiddingStrategyConfiguration($biddingStrategyConfiguration);
$operation = new AdGroupCriterionOperation();
$operation->setOperand($criterion);
$operation->setOperator(Operator::ADD);
// Create ad group criterion on the server and print out some information.
$result = $adGroupCriterionService->mutate([$operation]);
$criterion = $result->getValue()[0];
printf(
"Web page criterion with ID %d and status '%s' was created.\n",
$criterion->getCriterion()->getId(),
$criterion->getUserStatus()
);
}
public static function main()
{
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
// Construct an API session configured from a properties file and the
// OAuth2 credentials above.
$session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
self::runExample(
new AdWordsServices(),
$session,
intval(self::CAMPAIGN_ID),
intval(self::AD_GROUP_ID)
);
}
}
/**
* Class to keep track of DSA page feed details.
*/
// @codingStandardsIgnoreStart
final class DSAFeedDetails
{
// @codingStandardsIgnoreEnd
public $feedId;
public $urlAttributeId;
public $labelAttributeId;
}
AddDynamicPageFeed::main();