-
Notifications
You must be signed in to change notification settings - Fork 770
/
EstimateKeywordTraffic.php
275 lines (237 loc) · 9.94 KB
/
EstimateKeywordTraffic.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
<?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\Optimization;
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\Keyword;
use Google\AdsApi\AdWords\v201809\cm\KeywordMatchType;
use Google\AdsApi\AdWords\v201809\cm\Language;
use Google\AdsApi\AdWords\v201809\cm\Location;
use Google\AdsApi\AdWords\v201809\cm\Money;
use Google\AdsApi\AdWords\v201809\o\AdGroupEstimateRequest;
use Google\AdsApi\AdWords\v201809\o\CampaignEstimateRequest;
use Google\AdsApi\AdWords\v201809\o\KeywordEstimateRequest;
use Google\AdsApi\AdWords\v201809\o\StatsEstimate;
use Google\AdsApi\AdWords\v201809\o\TrafficEstimatorSelector;
use Google\AdsApi\AdWords\v201809\o\TrafficEstimatorService;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* This example gets traffic estimates for new keywords.
*/
class EstimateKeywordTraffic
{
public static function runExample(
AdWordsServices $adWordsServices,
AdWordsSession $session
) {
$trafficEstimatorService = $adWordsServices->get($session, TrafficEstimatorService::class);
// Create keywords. Up to 2000 keywords can be passed in a single request.
$keywords = [];
$keyword = new Keyword();
$keyword->setText('mars cruise');
$keyword->setMatchType(KeywordMatchType::BROAD);
$keywords[] = $keyword;
$keyword = new Keyword();
$keyword->setText('cheap cruise');
$keyword->setMatchType(KeywordMatchType::PHRASE);
$keywords[] = $keyword;
$keyword = new Keyword();
$keyword->setText('cruise');
$keyword->setMatchType(KeywordMatchType::EXACT);
$keywords[] = $keyword;
// Create a keyword estimate request for each keyword.
$keywordEstimateRequests = [];
foreach ($keywords as $keyword) {
$keywordEstimateRequest = new KeywordEstimateRequest();
$keywordEstimateRequest->setKeyword($keyword);
$keywordEstimateRequests[] = $keywordEstimateRequest;
}
// Negative keywords don't return estimates, but adjust the estimates of the
// other keywords in the hypothetical ad group.
$negativeKeywords = [];
$keyword = new Keyword();
$keyword->setText('moon walk');
$keyword->setMatchType(KeywordMatchType::BROAD);
$negativeKeywords[] = $keyword;
// Create a keyword estimate request for each negative keyword.
foreach ($negativeKeywords as $negativeKeyword) {
$keywordEstimateRequest = new KeywordEstimateRequest();
$keywordEstimateRequest->setKeyword($negativeKeyword);
$keywordEstimateRequest->setIsNegative(true);
$keywordEstimateRequests[] = $keywordEstimateRequest;
}
// Create ad group estimate requests.
$adGroupEstimateRequest = new AdGroupEstimateRequest();
$adGroupEstimateRequest->setKeywordEstimateRequests(
$keywordEstimateRequests
);
$money = new Money();
$money->setMicroAmount(1000000);
$adGroupEstimateRequest->setMaxCpc($money);
// Create campaign estimate requests.
$campaignEstimateRequest = new CampaignEstimateRequest();
$campaignEstimateRequest->setAdGroupEstimateRequests(
[$adGroupEstimateRequest]
);
// Optional: Set additional criteria for filtering estimates.
// See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html
// for a detailed list of country codes.
// Set targeting criteria. Only locations and languages are supported.
$unitedStates = new Location();
$unitedStates->setId(2840);
// See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html
// for a detailed list of language codes.
$english = new Language();
$english->setId(1000);
$campaignEstimateRequest->setCriteria([$unitedStates, $english]);
// Create selector.
$selector = new TrafficEstimatorSelector();
$selector->setCampaignEstimateRequests([$campaignEstimateRequest]);
// Optional: Request a list of campaign level estimates segmented by
// platform.
$selector->setPlatformEstimateRequested(true);
$result = $trafficEstimatorService->get($selector);
$platformEstimates = $result->getCampaignEstimates()[0]->getPlatformEstimates();
if ($platformEstimates !== null) {
foreach ($platformEstimates as $platformEstimate) {
if ($platformEstimate->getMinEstimate() !== null
&& $platformEstimate->getMaxEstimate() !== null) {
printf(
"Results for the platform with ID %d and name '%s':\n",
$platformEstimate->getPlatform()->getId(),
$platformEstimate->getPlatform()->getPlatformName()
);
self::printMeanEstimate(
$platformEstimate->getMinEstimate(),
$platformEstimate->getMaxEstimate()
);
}
}
}
$keywordEstimates = $result->getCampaignEstimates()[0]->getAdGroupEstimates()[0]->getKeywordEstimates();
$estimatesCount = count($keywordEstimates);
for ($i = 0; $i < $estimatesCount; $i++) {
$keywordEstimateRequest = $keywordEstimateRequests[$i];
// Skip negative keywords, since they don't return estimates.
if ($keywordEstimateRequest->getIsNegative() !== true) {
$keyword = $keywordEstimateRequest->getKeyword();
$keywordEstimate = $keywordEstimates[$i];
if ($keywordEstimate->getMin() !== null
&& $keywordEstimate->getMax() !== null) {
// Print the mean of the min and max values.
printf(
"Results for the keyword with text '%s' and match type '%s':\n",
$keyword->getText(),
$keyword->getMatchType()
);
self::printMeanEstimate(
$keywordEstimate->getMin(),
$keywordEstimate->getMax()
);
}
}
}
}
/**
* Prints estimated average CPC, ad position, daily clicks, and daily costs
* between the provided lower bound and upper bound of estimated stats.
*
* @param StatsEstimate $minEstimate the lower bound on the estimated stats
* @param StatsEstimate $maxEstimate the upper bound on the estimated stats
*/
private static function printMeanEstimate(
StatsEstimate $minEstimate,
StatsEstimate $maxEstimate
) {
$meanAverageCpc = self::calculateMeanMicroAmount(
$minEstimate->getAverageCpc(),
$maxEstimate->getAverageCpc()
);
$meanAveragePosition = self::calculateMean(
$minEstimate->getAveragePosition(),
$maxEstimate->getAveragePosition()
);
$meanClicks = self::calculateMean(
$minEstimate->getClicksPerDay(),
$maxEstimate->getClicksPerDay()
);
$meanTotalCost = self::calculateMeanMicroAmount(
$minEstimate->getTotalCost(),
$maxEstimate->getTotalCost()
);
printf(
" Estimated average CPC: %s\n",
self::formatMean($meanAverageCpc)
);
printf(
" Estimated ad position: %s\n",
self::formatMean($meanAveragePosition)
);
printf(" Estimated daily clicks: %s\n", self::formatMean($meanClicks));
printf(
" Estimated daily cost: %s\n\n",
self::formatMean($meanTotalCost)
);
}
/**
* Returns the mean of the two numbers if neither is null, else returns null.
*/
private static function calculateMean($min, $max)
{
if ($min === null || $max === null) {
return null;
}
return ($min + $max) / 2;
}
/**
* Returns the mean of the two object's microAmounts if neither is null, else
* returns null.
*/
private static function calculateMeanMicroAmount($min, $max)
{
if ($min === null || $max === null) {
return null;
}
if ($min->getMicroAmount() === null
|| $max->getMicroAmount() === null) {
return null;
}
return ($min->getMicroAmount() + $max->getMicroAmount()) / 2;
}
/**
* Returns a formatted version of the mean value, handling nulls.
*/
private static function formatMean($mean)
{
if ($mean === null) {
return 'null';
}
return sprintf("%.2f", $mean);
}
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);
}
}
EstimateKeywordTraffic::main();