-
Notifications
You must be signed in to change notification settings - Fork 770
/
AddPrices.php
212 lines (191 loc) · 7.9 KB
/
AddPrices.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
<?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\Extensions;
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\CustomerExtensionSetting;
use Google\AdsApi\AdWords\v201809\cm\CustomerExtensionSettingOperation;
use Google\AdsApi\AdWords\v201809\cm\CustomerExtensionSettingService;
use Google\AdsApi\AdWords\v201809\cm\DayOfWeek;
use Google\AdsApi\AdWords\v201809\cm\ExtensionSetting;
use Google\AdsApi\AdWords\v201809\cm\FeedItemCampaignTargeting;
use Google\AdsApi\AdWords\v201809\cm\FeedItemSchedule;
use Google\AdsApi\AdWords\v201809\cm\FeedItemScheduling;
use Google\AdsApi\AdWords\v201809\cm\FeedType;
use Google\AdsApi\AdWords\v201809\cm\MinuteOfHour;
use Google\AdsApi\AdWords\v201809\cm\Money;
use Google\AdsApi\AdWords\v201809\cm\MoneyWithCurrency;
use Google\AdsApi\AdWords\v201809\cm\Operator;
use Google\AdsApi\AdWords\v201809\cm\PriceExtensionPriceQualifier;
use Google\AdsApi\AdWords\v201809\cm\PriceExtensionPriceUnit;
use Google\AdsApi\AdWords\v201809\cm\PriceExtensionType;
use Google\AdsApi\AdWords\v201809\cm\PriceFeedItem;
use Google\AdsApi\AdWords\v201809\cm\PriceTableRow;
use Google\AdsApi\AdWords\v201809\cm\UrlList;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example adds a price extension and associates it with an account.
* Campaign targeting is also set using the specified campaign ID.
* To get campaigns, run GetCampaigns.php.
*/
class AddPrices
{
const CAMPAIGN_ID = 'INSERT_CAMPAIGN_ID_HERE';
const MICROS_PER_DOLLAR = 1000000;
public static function runExample(
AdWordsServices $adWordsServices,
AdWordsSession $session,
$campaignId
) {
$customerExtensionSettingService = $adWordsServices->get($session, CustomerExtensionSettingService::class);
// Create the price extension feed item.
$priceFeedItem = new PriceFeedItem();
$priceFeedItem->setPriceExtensionType(PriceExtensionType::SERVICES);
// Price qualifer is optional.
$priceFeedItem->setPriceQualifier(PriceExtensionPriceQualifier::FROM);
$priceFeedItem->setTrackingUrlTemplate(
'http://tracker.example.com/?u={lpurl}'
);
$priceFeedItem->setLanguage('en');
$priceFeedItem->setCampaignTargeting(
new FeedItemCampaignTargeting($campaignId)
);
$priceFeedItem->setScheduling(
new FeedItemScheduling(
[
new FeedItemSchedule(
DayOfWeek::SUNDAY,
10,
MinuteOfHour::ZERO,
18,
MinuteOfHour::ZERO
),
new FeedItemSchedule(
DayOfWeek::SATURDAY,
10,
MinuteOfHour::ZERO,
22,
MinuteOfHour::ZERO
)
]
)
);
// To create a price extension, at least three table rows are needed.
$tableRows = [];
$tableRows[] = self::createPriceTableRow(
'Scrubs',
'Body Scrub, Salt Scrub',
'http://www.example.com/scrubs',
60 * self::MICROS_PER_DOLLAR,
'USD',
PriceExtensionPriceUnit::PER_HOUR,
'http://m.example.com/scrubs'
);
$tableRows[] = self::createPriceTableRow(
'Hair Cuts',
'Once a month',
'http://www.example.com/haircuts',
75 * self::MICROS_PER_DOLLAR,
'USD',
PriceExtensionPriceUnit::PER_MONTH,
'http://m.example.com/haircuts'
);
$tableRows[] = self::createPriceTableRow(
'Skin Care Package',
'Four times a month',
'http://www.example.com/skincarepackage',
250 * self::MICROS_PER_DOLLAR,
'USD',
PriceExtensionPriceUnit::PER_MONTH
);
$priceFeedItem->setTableRows($tableRows);
// Create your customer extension settings. This associates the price
// extension to your account.
$customerExtensionSetting = new CustomerExtensionSetting();
$customerExtensionSetting->setExtensionType(FeedType::PRICE);
$customerExtensionSetting->setExtensionSetting(new ExtensionSetting());
$customerExtensionSetting->getExtensionSetting()->setExtensions(
[$priceFeedItem]
);
// Create a customer extension setting operation and add it to the list.
$operation = new CustomerExtensionSettingOperation();
$operation->setOperator(Operator::ADD);
$operation->setOperand($customerExtensionSetting);
$operations = [$operation];
// Add the price extension on the server.
$result = $customerExtensionSettingService->mutate($operations);
// Print out some information about the added extension setting.
$newExtensionSetting = $result->getValue()[0];
printf(
"Extension setting with type '%s' was added to your account.\n",
$newExtensionSetting->getExtensionType()
);
}
/**
* Creates a new price table row with the specified attributes.
*
* @param string $header the header of price table row
* @param string $description the description of price table row
* @param string $finalUrl the final URL of price table row
* @param integer $priceInMicros the price in micro amount
* @param string $currencyCode the 3-character currency code
* @param string $priceUnit the unit of shown price
* @param string|null $finalMobileUrl the mobile final URL of price table row
* @return PriceTableRow the created price table row
*/
private static function createPriceTableRow(
$header,
$description,
$finalUrl,
$priceInMicros,
$currencyCode,
$priceUnit,
$finalMobileUrl = null
) {
$priceTableRow = new PriceTableRow();
$priceTableRow->setHeader($header);
$priceTableRow->setDescription($description);
$priceTableRow->setFinalUrls(new UrlList([$finalUrl]));
$money = new Money();
$money->setMicroAmount($priceInMicros);
$moneyWithCurrency = new MoneyWithCurrency();
$moneyWithCurrency->setMoney($money);
$moneyWithCurrency->setCurrencyCode($currencyCode);
$priceTableRow->setPrice($moneyWithCurrency);
$priceTableRow->setPriceUnit($priceUnit);
if ($finalMobileUrl !== null) {
$priceTableRow->setFinalMobileUrls(new UrlList([$finalMobileUrl]));
}
return $priceTableRow;
}
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)
);
}
}
AddPrices::main();