|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +namespace Google\AdsApi\Examples\Dfp\v201702\CreativeService; |
| 18 | + |
| 19 | +require '../../../../vendor/autoload.php'; |
| 20 | + |
| 21 | +use Google\AdsApi\Common\OAuth2TokenBuilder; |
| 22 | +use Google\AdsApi\Dfp\DfpServices; |
| 23 | +use Google\AdsApi\Dfp\DfpSession; |
| 24 | +use Google\AdsApi\Dfp\DfpSessionBuilder; |
| 25 | +use Google\AdsApi\Dfp\v201702\AssetCreativeTemplateVariableValue; |
| 26 | +use Google\AdsApi\Dfp\v201702\CreativeAsset; |
| 27 | +use Google\AdsApi\Dfp\v201702\CreativeService; |
| 28 | +use Google\AdsApi\Dfp\v201702\Size; |
| 29 | +use Google\AdsApi\Dfp\v201702\StringCreativeTemplateVariableValue; |
| 30 | +use Google\AdsApi\Dfp\v201702\TemplateCreative; |
| 31 | +use Google\AdsApi\Dfp\v201702\UrlCreativeTemplateVariableValue; |
| 32 | + |
| 33 | +/** |
| 34 | + * Creates native creatives. |
| 35 | + * |
| 36 | + * This example is meant to be run from a command line (not as a webpage) and |
| 37 | + * requires that you've setup an `adsapi_php.ini` file in your home directory |
| 38 | + * with your API credentials and settings. See `README.md` for more info. |
| 39 | + */ |
| 40 | +class CreateNativeCreatives { |
| 41 | + |
| 42 | + const ADVERTISER_ID = 'INSERT_ADVERTISER_ID_HERE'; |
| 43 | + |
| 44 | + public static function runExample(DfpServices $dfpServices, |
| 45 | + DfpSession $session, $advertiserId) { |
| 46 | + $creativeService = $dfpServices->get($session, CreativeService::class); |
| 47 | + |
| 48 | + // Use the system defined native app install creative template. |
| 49 | + $nativeAppInstallTemplateId = 10004400; |
| 50 | + |
| 51 | + // Use 1x1 as the size for native creatives. |
| 52 | + $size = new Size(); |
| 53 | + $size->setWidth(1); |
| 54 | + $size->setHeight(1); |
| 55 | + $size->setIsAspectRatio(false); |
| 56 | + |
| 57 | + // Create a native app install creative for the Pie Noon app. |
| 58 | + $nativeAppInstallCreative = new TemplateCreative(); |
| 59 | + $nativeAppInstallCreative->setName('Native creative #' . uniqid()); |
| 60 | + $nativeAppInstallCreative->setAdvertiserId($advertiserId); |
| 61 | + $nativeAppInstallCreative->setDestinationUrl( |
| 62 | + 'https://play.google.com/store/apps/details?id=com.google.fpl.pie_noon' |
| 63 | + ); |
| 64 | + $nativeAppInstallCreative |
| 65 | + ->setCreativeTemplateId($nativeAppInstallTemplateId); |
| 66 | + $nativeAppInstallCreative->setSize($size); |
| 67 | + |
| 68 | + // Set the headline. |
| 69 | + $headlineVariableValue = new StringCreativeTemplateVariableValue(); |
| 70 | + $headlineVariableValue->setUniqueName('Headline'); |
| 71 | + $headlineVariableValue->setValue('Pie Noon'); |
| 72 | + $variableValues = [$headlineVariableValue]; |
| 73 | + |
| 74 | + // Set the body text. |
| 75 | + $bodyVariableValue = new StringCreativeTemplateVariableValue(); |
| 76 | + $bodyVariableValue->setUniqueName('Body'); |
| 77 | + $bodyVariableValue->setValue('Try multi-screen mode!'); |
| 78 | + $variableValues[] = $bodyVariableValue; |
| 79 | + |
| 80 | + // Set the image asset. |
| 81 | + $imageVariableValue = new AssetCreativeTemplateVariableValue(); |
| 82 | + $imageVariableValue->setUniqueName('Image'); |
| 83 | + $imageAsset = new CreativeAsset(); |
| 84 | + $imageAsset->setFileName('image' . uniqid() . '.png'); |
| 85 | + $imageAsset->setAssetByteArray(file_get_contents( |
| 86 | + 'https://lh4.ggpht.com/GIGNKdGHMEHFDw6TM2bgAUDKPQQRIReKZPqEpMeEhZOPYnTd' |
| 87 | + . 'OQGaSpGSEZflIFs0iw=h300' |
| 88 | + )); |
| 89 | + $imageVariableValue->setAsset($imageAsset); |
| 90 | + $variableValues[] = $imageVariableValue; |
| 91 | + |
| 92 | + // Set the price. |
| 93 | + $priceVariableValue = new StringCreativeTemplateVariableValue(); |
| 94 | + $priceVariableValue->setUniqueName('Price'); |
| 95 | + $priceVariableValue->setValue('Free'); |
| 96 | + $variableValues[] = $priceVariableValue; |
| 97 | + |
| 98 | + // Set app icon image asset. |
| 99 | + $appIconVariableValue = new AssetCreativeTemplateVariableValue(); |
| 100 | + $appIconVariableValue->setUniqueName('Appicon'); |
| 101 | + $appIconAsset = new CreativeAsset(); |
| 102 | + $appIconAsset->setFileName('icon' . uniqid() . '.png'); |
| 103 | + $appIconAsset->setAssetByteArray(file_get_contents( |
| 104 | + 'https://lh6.ggpht.com/Jzvjne5CLs6fJ1MHF-XeuUfpABzl0YNMlp4RpHnvPRCIj4--' |
| 105 | + . 'eTDwtyouwUDzVVekXw=w300' |
| 106 | + )); |
| 107 | + $appIconVariableValue->setAsset($appIconAsset); |
| 108 | + $variableValues[] = $appIconVariableValue; |
| 109 | + |
| 110 | + // Set the call to action text. |
| 111 | + $callToActionVariableValue = new StringCreativeTemplateVariableValue(); |
| 112 | + $callToActionVariableValue->setUniqueName('Calltoaction'); |
| 113 | + $callToActionVariableValue->setValue('Install'); |
| 114 | + $variableValues[] = $callToActionVariableValue; |
| 115 | + |
| 116 | + // Set the star rating. |
| 117 | + $starRatingVariableValue = new StringCreativeTemplateVariableValue(); |
| 118 | + $starRatingVariableValue->setUniqueName('Starrating'); |
| 119 | + $starRatingVariableValue->setValue('4'); |
| 120 | + $variableValues[] = $starRatingVariableValue; |
| 121 | + |
| 122 | + // Set the store type. |
| 123 | + $storeVariableValue = new StringCreativeTemplateVariableValue(); |
| 124 | + $storeVariableValue->setUniqueName('Store'); |
| 125 | + $storeVariableValue->setValue('Google Play'); |
| 126 | + $variableValues[] = $storeVariableValue; |
| 127 | + |
| 128 | + // Set the deep link URL. |
| 129 | + $deepLinkVariableValue = new UrlCreativeTemplateVariableValue(); |
| 130 | + $deepLinkVariableValue->setUniqueName('DeeplinkclickactionURL'); |
| 131 | + $deepLinkVariableValue |
| 132 | + ->setValue('market://details?id=com.google.fpl.pie_noon'); |
| 133 | + $variableValues[] = $deepLinkVariableValue; |
| 134 | + |
| 135 | + $nativeAppInstallCreative |
| 136 | + ->setCreativeTemplateVariableValues($variableValues); |
| 137 | + |
| 138 | + // Create the native creatives on the server. |
| 139 | + $results = $creativeService->createCreatives([$nativeAppInstallCreative]); |
| 140 | + |
| 141 | + // Print out some information for each created native creative. |
| 142 | + foreach ($results as $i => $nativeAppInstallCreative) { |
| 143 | + printf( |
| 144 | + "%d) Native creative with ID %d and name '%s' was created and can be " |
| 145 | + . "previewed at: '%s'.\n", |
| 146 | + $i, |
| 147 | + $nativeAppInstallCreative->getId(), |
| 148 | + $nativeAppInstallCreative->getName(), |
| 149 | + $nativeAppInstallCreative->getPreviewUrl() |
| 150 | + ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public static function main() { |
| 155 | + // Generate a refreshable OAuth2 credential for authentication. |
| 156 | + $oAuth2Credential = (new OAuth2TokenBuilder()) |
| 157 | + ->fromFile() |
| 158 | + ->build(); |
| 159 | + |
| 160 | + // Construct an API session configured from a properties file and the OAuth2 |
| 161 | + // credentials above. |
| 162 | + $session = (new DfpSessionBuilder()) |
| 163 | + ->fromFile() |
| 164 | + ->withOAuth2Credential($oAuth2Credential) |
| 165 | + ->build(); |
| 166 | + |
| 167 | + self::runExample(new DfpServices(), $session, intval(self::ADVERTISER_ID)); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +CreateNativeCreatives::main(); |
0 commit comments