|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This example adds an ad customizer feed and associates it with the customer. |
| 4 | + * Then it adds an ad that uses the feed to populate dynamic data. |
| 5 | + * |
| 6 | + * Tags: CustomerFeedService.mutate |
| 7 | + * Tags: FeedItemService.mutate |
| 8 | + * Tags: FeedMappingService.mutate |
| 9 | + * Tags: FeedService.mutate |
| 10 | + * Tags: AdGroupAdService.mutate |
| 11 | + * Restriction: adwords-only |
| 12 | + * |
| 13 | + * Copyright 2014, Google Inc. All Rights Reserved. |
| 14 | + * |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 16 | + * you may not use this file except in compliance with the License. |
| 17 | + * You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, software |
| 22 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 23 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | + * See the License for the specific language governing permissions and |
| 25 | + * limitations under the License. |
| 26 | + * |
| 27 | + * @package GoogleApiAdsAdWords |
| 28 | + * @subpackage v201406 |
| 29 | + * @category WebServices |
| 30 | + * @copyright 2014, Google Inc. All Rights Reserved. |
| 31 | + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, |
| 32 | + * Version 2.0 |
| 33 | + * @author Ray Tsang |
| 34 | + */ |
| 35 | + |
| 36 | +// Include the initialization file |
| 37 | +require_once dirname(dirname(__FILE__)) . '/init.php'; |
| 38 | + |
| 39 | +// Enter parameters required by the code example. |
| 40 | +$adGroupIds = array('INSERT_AD_GROUP_ID_HERE', 'INSERT_AD_GROUP_ID_HERE'); |
| 41 | + |
| 42 | +// See the Placeholder reference page for a list of all the placeholder types |
| 43 | +// and fields. |
| 44 | +// https://developers.google.com/adwords/api/docs/appendix/placeholders.html |
| 45 | +define('PLACEHOLDER_AD_CUSTOMIZER', 10); |
| 46 | +define('PLACEHOLDER_FIELD_PRICE', 3); |
| 47 | +define('PLACEHOLDER_FIELD_DATE', 4); |
| 48 | +define('PLACEHOLDER_FIELD_STRING', 5); |
| 49 | + |
| 50 | +/** |
| 51 | + * Runs the example. |
| 52 | + * |
| 53 | + * @param AdWordsUser $user the user to run the example with |
| 54 | + * @param array $adGroupIds the IDs of the ad groups to target with the FeedItem |
| 55 | + */ |
| 56 | +function AddAdCustomizerExample(AdWordsUser $user, $adGroupIds) { |
| 57 | + // Create a customizer feed. One feed per account can be used for all ads. |
| 58 | + $dataHolder = CreateCustomizerFeed($user); |
| 59 | + |
| 60 | + // Create a feed mapping to map the fields with customizer IDs. |
| 61 | + CreateFeedMapping($user, $dataHolder); |
| 62 | + |
| 63 | + // Add feed items containing the values we'd like to place in ads. |
| 64 | + CreateCustomizerFeedItems($user, $adGroupIds, $dataHolder); |
| 65 | + |
| 66 | + // Create a customer (account-level) feed with a matching function that |
| 67 | + // determines when to use this feed. For this case we use the "IDENTITY" |
| 68 | + // matching function that is always true just to associate this feed with the |
| 69 | + // customer. The targeting is done within the feed items using the |
| 70 | + // campaignTargeting, adGroupTargeting, or keywordTargeting attributes. |
| 71 | + CreateCustomerFeed($user, $dataHolder); |
| 72 | + |
| 73 | + // All set! We can now create ads with customizations. |
| 74 | + CreateAdsWithCustomizations($user, $adGroupIds); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Creates a new Feed for ad customizers. |
| 79 | + * |
| 80 | + * @param AdWordsUser $user the user to run the example with |
| 81 | + */ |
| 82 | +function CreateCustomizerFeed(AdWordsUser $user) { |
| 83 | + // Map that holds IDs associated to the feeds metadata. |
| 84 | + $dataHolder = array(); |
| 85 | + |
| 86 | + // Get the FeedService, which loads the required classes. |
| 87 | + $feedService = $user->GetService('FeedService', ADWORDS_VERSION); |
| 88 | + |
| 89 | + // Create attributes. |
| 90 | + $nameAttribute = new FeedAttribute(); |
| 91 | + $nameAttribute->type = 'STRING'; |
| 92 | + $nameAttribute->name = 'Name'; |
| 93 | + $priceAttribute = new FeedAttribute(); |
| 94 | + $priceAttribute->type = 'STRING'; |
| 95 | + $priceAttribute->name = 'Price'; |
| 96 | + $dateAttribute = new FeedAttribute(); |
| 97 | + $dateAttribute->type = 'DATE_TIME'; |
| 98 | + $dateAttribute->name = 'Date'; |
| 99 | + |
| 100 | + // Create the feed. |
| 101 | + $customizerFeed = new Feed(); |
| 102 | + $customizerFeed->name = 'CustomizerFeed'; |
| 103 | + $customizerFeed->attributes = array($nameAttribute, $priceAttribute, |
| 104 | + $dateAttribute); |
| 105 | + $customizerFeed->origin = 'USER'; |
| 106 | + |
| 107 | + // Create operation. |
| 108 | + $operation = new FeedOperation(); |
| 109 | + $operation->operator = 'ADD'; |
| 110 | + $operation->operand = $customizerFeed; |
| 111 | + |
| 112 | + $operations = array($operation); |
| 113 | + |
| 114 | + // Add the feed. |
| 115 | + $result = $feedService->mutate($operations); |
| 116 | + |
| 117 | + $savedFeed = $result->value[0]; |
| 118 | + $dataHolder['feedId'] = $savedFeed->id; |
| 119 | + $savedAttributes = $savedFeed->attributes; |
| 120 | + $dataHolder['nameFeedAttributeId'] = $savedAttributes[0]->id; |
| 121 | + $dataHolder['priceFeedAttributeId'] = $savedAttributes[1]->id; |
| 122 | + $dataHolder['dateFeedAttributeId'] = $savedAttributes[2]->id; |
| 123 | + |
| 124 | + printf('Feed with name "%s" and ID %d with nameAttributeId %d' |
| 125 | + . ", priceAttributeId %d, and dateAttribute %d were created.\n", |
| 126 | + $savedFeed->name, |
| 127 | + $savedFeed->id, |
| 128 | + $savedAttributes[0]->id, |
| 129 | + $savedAttributes[1]->id, |
| 130 | + $savedAttributes[2]->id); |
| 131 | + |
| 132 | + return $dataHolder; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Creates a new FeedMapping that indicates how the data holder's feed should |
| 137 | + * be interpreted in the context of ad customizers. |
| 138 | + * |
| 139 | + * @param AdWordsUser $user the user to run the example with |
| 140 | + * @param array $dataHolder IDs associated to created customizer feed metadata |
| 141 | + */ |
| 142 | +function CreateFeedMapping($user, $dataHolder) { |
| 143 | + // Get the FeedMappingService, which loads the required classes. |
| 144 | + $feedMappingService = $user->GetService('FeedMappingService', |
| 145 | + ADWORDS_VERSION); |
| 146 | + |
| 147 | + // Map the FeedAttributeIds to the fieldId constants. |
| 148 | + $nameFieldMapping = new AttributeFieldMapping(); |
| 149 | + $nameFieldMapping->feedAttributeId = |
| 150 | + $dataHolder['nameFeedAttributeId']; |
| 151 | + $nameFieldMapping->fieldId = PLACEHOLDER_FIELD_STRING; |
| 152 | + $priceFieldMapping = new AttributeFieldMapping(); |
| 153 | + $priceFieldMapping->feedAttributeId = |
| 154 | + $dataHolder['priceFeedAttributeId']; |
| 155 | + $priceFieldMapping->fieldId = PLACEHOLDER_FIELD_PRICE; |
| 156 | + $dateFieldMapping = new AttributeFieldMapping(); |
| 157 | + $dateFieldMapping->feedAttributeId = $dataHolder['dateFeedAttributeId']; |
| 158 | + $dateFieldMapping->fieldId = PLACEHOLDER_FIELD_DATE; |
| 159 | + |
| 160 | + // Create the FieldMapping and operation. |
| 161 | + $feedMapping = new FeedMapping(); |
| 162 | + $feedMapping->placeholderType = PLACEHOLDER_AD_CUSTOMIZER; |
| 163 | + $feedMapping->feedId = $dataHolder['feedId']; |
| 164 | + $feedMapping->attributeFieldMappings = |
| 165 | + array($nameFieldMapping, $priceFieldMapping, $dateFieldMapping); |
| 166 | + $operation = new FeedMappingOperation(); |
| 167 | + $operation->operand = $feedMapping; |
| 168 | + $operation->operator = 'ADD'; |
| 169 | + |
| 170 | + $operations = array($operation); |
| 171 | + |
| 172 | + // Save the field mapping. |
| 173 | + $result = $feedMappingService->mutate($operations); |
| 174 | + foreach ($result->value as $feedMapping) { |
| 175 | + printf('Feed mapping with ID %d and placeholderType %d was saved for ' . |
| 176 | + "feed with ID %d.\n", |
| 177 | + $feedMapping->feedMappingId, |
| 178 | + $feedMapping->placeholderType, |
| 179 | + $feedMapping->feedId); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Creates FeedItems with the values to use in ad customizations for each ad |
| 185 | + * group in adGroupIds |
| 186 | + * |
| 187 | + * @param AdWordsUser $user the user to run the example with |
| 188 | + * @param array $adGroupIds the IDs of the ad groups to target with the FeedItem |
| 189 | + * @param array $dataHolder IDs associated to created customizer feed metadata |
| 190 | + */ |
| 191 | +function CreateCustomizerFeedItems(AdWordsUser $user, $adGroupIds, |
| 192 | + $dataHolder) { |
| 193 | + // Get the FeedItemService, which loads the required classes. |
| 194 | + $feedItemService = $user->GetService('FeedItemService', ADWORDS_VERSION); |
| 195 | + |
| 196 | + $operations = array(); |
| 197 | + |
| 198 | + // Create operations to add FeedItems. |
| 199 | + $operations[] = CreateFeedItemAddOperation('Mars', '$1234.56', |
| 200 | + '20140601 000000', $adGroupIds[0], $dataHolder); |
| 201 | + $operations[] = CreateFeedItemAddOperation('Venus', '$1450.00', |
| 202 | + '20140615 120000', $adGroupIds[1], $dataHolder); |
| 203 | + |
| 204 | + $result = $feedItemService->mutate($operations); |
| 205 | + |
| 206 | + foreach ($result->value as $feedItem) { |
| 207 | + printf("FeedItem with feedItemId %d was added.\n", $feedItem->feedItemId); |
| 208 | + } |
| 209 | + |
| 210 | + return $dataHolder; |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * Creates a FeedItemOperation that will create a FeedItem with the specified |
| 215 | + * values and ad group target when sent to FeedItemService.mutate. |
| 216 | + * |
| 217 | + * @param string $name the value for the name attribute of the FeedItem |
| 218 | + * @param string $price the value for the price attribute of the FeedItem |
| 219 | + * @param string $date the value for the date attribute of the FeedItem |
| 220 | + * @param string $adGroupId the ID of the ad group to target with the FeedItem |
| 221 | + * @param array $dataHolder IDs associated to created customizer feed metadata |
| 222 | + */ |
| 223 | +function CreateFeedItemAddOperation($name, $price, $date, $adGroupId, |
| 224 | + $dataHolder) { |
| 225 | + // Create the FeedItemAttributeValues for our text values. |
| 226 | + $nameAttributeValue = new FeedItemAttributeValue(); |
| 227 | + $nameAttributeValue->feedAttributeId = |
| 228 | + $dataHolder['nameFeedAttributeId']; |
| 229 | + $nameAttributeValue->stringValue = $name; |
| 230 | + $priceAttributeValue = new FeedItemAttributeValue(); |
| 231 | + $priceAttributeValue->feedAttributeId = |
| 232 | + $dataHolder['priceFeedAttributeId']; |
| 233 | + $priceAttributeValue->stringValue = $price; |
| 234 | + $dateAttributeValue = new FeedItemAttributeValue(); |
| 235 | + $dateAttributeValue->feedAttributeId = $dataHolder['dateFeedAttributeId']; |
| 236 | + $dateAttributeValue->stringValue = $date; |
| 237 | + |
| 238 | + // Create the feed item and operation. |
| 239 | + $item = new FeedItem(); |
| 240 | + $item->feedId = $dataHolder['feedId']; |
| 241 | + $item->attributeValues = |
| 242 | + array($nameAttributeValue, $priceAttributeValue, $dateAttributeValue); |
| 243 | + |
| 244 | + $adGroupTargeting = new FeedItemAdGroupTargeting(); |
| 245 | + $adGroupTargeting->TargetingAdGroupId = $adGroupId; |
| 246 | + $item->adGroupTargeting = $adGroupTargeting; |
| 247 | + |
| 248 | + $operation = new FeedItemOperation(); |
| 249 | + $operation->operand = $item; |
| 250 | + $operation->operator = 'ADD'; |
| 251 | + return $operation; |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Creates a CustomerFeed that will associate the Feed with the ad customizers |
| 256 | + * placeholder type. |
| 257 | + * |
| 258 | + * @param AdWordsUser $user the user to run the example with |
| 259 | + * @param map $dataHolder IDs associated to created customizer feed metadata |
| 260 | + */ |
| 261 | +function CreateCustomerFeed($user, $dataHolder) { |
| 262 | + // Get the CustomerFeedService, which loads the required classes. |
| 263 | + $customerFeedService = $user->GetService('CustomerFeedService', |
| 264 | + ADWORDS_VERSION); |
| 265 | + |
| 266 | + $customerFeed = new CustomerFeed(); |
| 267 | + $customerFeed->feedId = $dataHolder['feedId']; |
| 268 | + $customerFeed->placeholderTypes = array(PLACEHOLDER_AD_CUSTOMIZER); |
| 269 | + |
| 270 | + // Create a matching function that will always evaluate to true. |
| 271 | + $customerMatchingFunction = new FeedFunction(); |
| 272 | + $constOperand = new ConstantOperand(); |
| 273 | + $constOperand->type = 'BOOLEAN'; |
| 274 | + $constOperand->booleanValue = 'TRUE'; |
| 275 | + $customerMatchingFunction->lhsOperand = array($constOperand); |
| 276 | + $customerMatchingFunction->operator = 'IDENTITY'; |
| 277 | + $customerFeed->matchingFunction = $customerMatchingFunction; |
| 278 | + |
| 279 | + // Create an operation to add the customer feed. |
| 280 | + $customerFeedOperation = new CustomerFeedOperation(); |
| 281 | + $customerFeedOperation->operand = $customerFeed; |
| 282 | + $customerFeedOperation->operator = 'ADD'; |
| 283 | + |
| 284 | + $operations = array($customerFeedOperation); |
| 285 | + |
| 286 | + $result = $customerFeedService->mutate($operations); |
| 287 | + foreach ($result->value as $savedCustomerFeed) { |
| 288 | + printf("Created a new CustomerFeed that's associated with feed ID %d.\n", |
| 289 | + $savedCustomerFeed->feedId); |
| 290 | + } |
| 291 | +} |
| 292 | + |
| 293 | +/** |
| 294 | + * Creates text ads that use ad customizations for the specified ad group IDs. |
| 295 | + * |
| 296 | + * @param AdWordsUser $user the user to run the example with |
| 297 | + * @param array $adGroupIds the IDs of the ad groups to target with the FeedItem |
| 298 | + */ |
| 299 | +function CreateAdsWithCustomizations(AdWordsUser $user, $adGroupIds) { |
| 300 | + // Get the service, which loads the required classes. |
| 301 | + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); |
| 302 | + |
| 303 | + $textAd = new TextAd(); |
| 304 | + $textAd->headline = 'Luxury Cruise to {=CustomizerFeed.Name}'; |
| 305 | + $textAd->description1 = 'Only {=CustomizerFeed.Price}'; |
| 306 | + $textAd->description2 = 'Offer ends in {=countdown(CustomizerFeed.Date)}!'; |
| 307 | + $textAd->url = 'http://www.example.com'; |
| 308 | + $textAd->displayUrl = 'www.example.com'; |
| 309 | + |
| 310 | + // We add the same ad to both ad groups. When they serve, they will show |
| 311 | + // different values, since they match different feed items. |
| 312 | + $operations = array(); |
| 313 | + |
| 314 | + foreach ($adGroupIds as $adGroupId) { |
| 315 | + // Create ad group ad. |
| 316 | + $adGroupAd = new AdGroupAd(); |
| 317 | + $adGroupAd->adGroupId = $adGroupId; |
| 318 | + $adGroupAd->ad = $textAd; |
| 319 | + |
| 320 | + // Create operation. |
| 321 | + $operation = new AdGroupAdOperation(); |
| 322 | + $operation->operand = $adGroupAd; |
| 323 | + $operation->operator = 'ADD'; |
| 324 | + $operations[] = $operation; |
| 325 | + } |
| 326 | + |
| 327 | + // Make the mutate request. |
| 328 | + $result = $adGroupAdService->mutate($operations); |
| 329 | + |
| 330 | + // Display results. |
| 331 | + foreach ($result->value as $adGroupAd) { |
| 332 | + printf("Text ad with headline '%s' and ID '%s' was added.\n", |
| 333 | + $adGroupAd->ad->headline, $adGroupAd->ad->id); |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +// Don't run the example if the file is being included. |
| 338 | +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { |
| 339 | + return; |
| 340 | +} |
| 341 | + |
| 342 | +try { |
| 343 | + // Get AdWordsUser from credentials in "../auth.ini" |
| 344 | + // relative to the AdWordsUser.php file's directory. |
| 345 | + $user = new AdWordsUser(); |
| 346 | + |
| 347 | + // Log every SOAP XML request and response. |
| 348 | + $user->LogAll(); |
| 349 | + |
| 350 | + // Run the example. |
| 351 | + AddAdCustomizerExample($user, $adGroupIds); |
| 352 | +} catch (Exception $e) { |
| 353 | + printf("An error has occurred: %s\n", $e->getMessage()); |
| 354 | +} |
0 commit comments