/
CreateNativeCreatives.php
188 lines (163 loc) · 7.22 KB
/
CreateNativeCreatives.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
<?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\AdManager\v202402\CreativeService;
require __DIR__ . '/../../../../vendor/autoload.php';
use Google\AdsApi\AdManager\AdManagerSession;
use Google\AdsApi\AdManager\AdManagerSessionBuilder;
use Google\AdsApi\AdManager\v202402\AssetCreativeTemplateVariableValue;
use Google\AdsApi\AdManager\v202402\CreativeAsset;
use Google\AdsApi\AdManager\v202402\ServiceFactory;
use Google\AdsApi\AdManager\v202402\Size;
use Google\AdsApi\AdManager\v202402\StringCreativeTemplateVariableValue;
use Google\AdsApi\AdManager\v202402\TemplateCreative;
use Google\AdsApi\AdManager\v202402\UrlCreativeTemplateVariableValue;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* Creates native creatives.
*
* This example is meant to be run from a command line (not as a webpage) and
* requires that you've setup an `adsapi_php.ini` file in your home directory
* with your API credentials and settings. See `README.md` for more info.
*/
class CreateNativeCreatives
{
const ADVERTISER_ID = 'INSERT_ADVERTISER_ID_HERE';
public static function runExample(
ServiceFactory $serviceFactory,
AdManagerSession $session,
int $advertiserId
) {
$creativeService = $serviceFactory->createCreativeService($session);
// Use the system defined native app install creative template.
$nativeAppInstallTemplateId = 10004400;
// Use 1x1 as the size for native creatives.
$size = new Size();
$size->setWidth(1);
$size->setHeight(1);
$size->setIsAspectRatio(false);
// Create a native app install creative for the Pie Noon app.
$nativeAppInstallCreative = new TemplateCreative();
$nativeAppInstallCreative->setName('Native creative #' . uniqid());
$nativeAppInstallCreative->setAdvertiserId($advertiserId);
$nativeAppInstallCreative->setDestinationUrl(
'https://play.google.com/store/apps/details?id=com.google.fpl.'
. 'pie_noon'
);
$nativeAppInstallCreative->setCreativeTemplateId(
$nativeAppInstallTemplateId
);
$nativeAppInstallCreative->setSize($size);
// Set the headline.
$headlineVariableValue = new StringCreativeTemplateVariableValue();
$headlineVariableValue->setUniqueName('Headline');
$headlineVariableValue->setValue('Pie Noon');
$variableValues = [$headlineVariableValue];
// Set the body text.
$bodyVariableValue = new StringCreativeTemplateVariableValue();
$bodyVariableValue->setUniqueName('Body');
$bodyVariableValue->setValue('Try multi-screen mode!');
$variableValues[] = $bodyVariableValue;
// Set the image asset.
$imageVariableValue = new AssetCreativeTemplateVariableValue();
$imageVariableValue->setUniqueName('Image');
$imageAsset = new CreativeAsset();
$imageAsset->setFileName('image' . uniqid() . '.png');
$imageAsset->setAssetByteArray(
file_get_contents(
'https://lh4.ggpht.com/GIGNKdGHMEHFDw6TM2bgAUDKPQQRIReKZPqEpMeE'
. 'hZOPYnTdOQGaSpGSEZflIFs0iw=h300'
)
);
$imageVariableValue->setAsset($imageAsset);
$variableValues[] = $imageVariableValue;
// Set the price.
$priceVariableValue = new StringCreativeTemplateVariableValue();
$priceVariableValue->setUniqueName('Price');
$priceVariableValue->setValue('Free');
$variableValues[] = $priceVariableValue;
// Set app icon image asset.
$appIconVariableValue = new AssetCreativeTemplateVariableValue();
$appIconVariableValue->setUniqueName('Appicon');
$appIconAsset = new CreativeAsset();
$appIconAsset->setFileName('icon' . uniqid() . '.png');
$appIconAsset->setAssetByteArray(
file_get_contents(
'https://lh6.ggpht.com/Jzvjne5CLs6fJ1MHF-XeuUfpABzl0YNMlp4RpHn'
. 'vPRCIj4--eTDwtyouwUDzVVekXw=w300'
)
);
$appIconVariableValue->setAsset($appIconAsset);
$variableValues[] = $appIconVariableValue;
// Set the call to action text.
$callToActionVariableValue = new StringCreativeTemplateVariableValue();
$callToActionVariableValue->setUniqueName('Calltoaction');
$callToActionVariableValue->setValue('Install');
$variableValues[] = $callToActionVariableValue;
// Set the star rating.
$starRatingVariableValue = new StringCreativeTemplateVariableValue();
$starRatingVariableValue->setUniqueName('Starrating');
$starRatingVariableValue->setValue('4');
$variableValues[] = $starRatingVariableValue;
// Set the store type.
$storeVariableValue = new StringCreativeTemplateVariableValue();
$storeVariableValue->setUniqueName('Store');
$storeVariableValue->setValue('Google Play');
$variableValues[] = $storeVariableValue;
// Set the deep link URL.
$deepLinkVariableValue = new UrlCreativeTemplateVariableValue();
$deepLinkVariableValue->setUniqueName('DeeplinkclickactionURL');
$deepLinkVariableValue->setValue(
'market://details?id=com.google.fpl.pie_noon');
$variableValues[] = $deepLinkVariableValue;
$nativeAppInstallCreative->setCreativeTemplateVariableValues(
$variableValues
);
// Create the native creatives on the server.
$results = $creativeService->createCreatives(
[$nativeAppInstallCreative]
);
// Print out some information for each created native creative.
foreach ($results as $i => $nativeAppInstallCreative) {
printf(
"%d) Native creative with ID %d and name '%s' was created and "
. "can be previewed at: '%s'.%s",
$i,
$nativeAppInstallCreative->getId(),
$nativeAppInstallCreative->getName(),
$nativeAppInstallCreative->getPreviewUrl(),
PHP_EOL
);
}
}
public static function main()
{
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()
->build();
// Construct an API session configured from an `adsapi_php.ini` file
// and the OAuth2 credentials above.
$session = (new AdManagerSessionBuilder())->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
self::runExample(
new ServiceFactory(),
$session,
intval(self::ADVERTISER_ID)
);
}
}
CreateNativeCreatives::main();