|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SchulzeFelix\Stat\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use SchulzeFelix\Stat\Exceptions\ApiException; |
|
7
|
|
|
use SchulzeFelix\Stat\Objects\StatBulkJob; |
|
8
|
|
|
use SchulzeFelix\Stat\Objects\StatKeyword; |
|
9
|
|
|
use SchulzeFelix\Stat\Objects\StatKeywordEngineRanking; |
|
10
|
|
|
use SchulzeFelix\Stat\Objects\StatKeywordRanking; |
|
11
|
|
|
use SchulzeFelix\Stat\Objects\StatKeywordStats; |
|
12
|
|
|
use SchulzeFelix\Stat\Objects\StatLocalSearchTrend; |
|
13
|
|
|
use SchulzeFelix\Stat\Objects\StatProject; |
|
14
|
|
|
use SchulzeFelix\Stat\Objects\StatSite; |
|
15
|
|
|
use SchulzeFelix\Stat\Objects\StatTag; |
|
16
|
|
|
|
|
17
|
|
|
class StatBulk extends BaseStat |
|
18
|
|
|
{ |
|
19
|
|
|
public function list() |
|
20
|
|
|
{ |
|
21
|
|
|
$response = $this->performQuery('bulk/list'); |
|
22
|
|
|
|
|
23
|
|
|
$bulkJobs = collect(); |
|
24
|
|
|
|
|
25
|
|
|
if ($response['resultsreturned'] == 0) { |
|
26
|
|
|
return $bulkJobs; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($response['resultsreturned'] == 1) { |
|
30
|
|
|
$bulkJobs->push($response['Result']); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($response['resultsreturned'] > 1) { |
|
34
|
|
|
$bulkJobs = collect($response['Result']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $bulkJobs->transform(function ($job, $key) { |
|
|
|
|
|
|
38
|
|
|
return $this->transformBulkJobStatus($job); |
|
39
|
|
|
}); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Schedule the creation of a new bulk export job for ranks. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Carbon $date Note that you cannot create bulk jobs for the current date or dates in the future. |
|
46
|
|
|
* @param array|null $sites If no site IDs are passed in, ranks are reported for all sites in the system on the given date. |
|
47
|
|
|
* @param string $rankType This parameter changes the call between getting the highest ranks for the keywords for the date with the value 'highest', or getting all the ranks for each engine for a keyword for a date with the value 'all'. |
|
48
|
|
|
* @param array|null $engines This parameter lets you choose which search engines to include in the export, defaulting to Google, Yahoo, and Bing. Engines can be passed in a array to get multiple. ['google', 'yahoo', 'bing'] |
|
49
|
|
|
* @param bool $currentlyTrackedOnly This parameter will cause the API to output only keywords which currently have tracking on at the time the API request is generated. |
|
50
|
|
|
* @param bool $crawledKeywordsOnly This parameter causes the API to only include output for keywords that were crawled on the date parameter provided. |
|
51
|
|
|
* @return int |
|
52
|
|
|
*/ |
|
53
|
|
|
public function ranks(Carbon $date, array $sites = null, $rankType = 'highest', $engines = null, bool $currentlyTrackedOnly = false, bool $crawledKeywordsOnly = false) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->validateBulkDate($date); |
|
56
|
|
|
|
|
57
|
|
|
$arguments['date'] = $date->toDateString(); |
|
|
|
|
|
|
58
|
|
|
$arguments['rank_type'] = $rankType; |
|
59
|
|
|
$arguments['currently_tracked_only'] = $currentlyTrackedOnly; |
|
60
|
|
|
$arguments['crawled_keywords_only'] = $crawledKeywordsOnly; |
|
61
|
|
|
|
|
62
|
|
|
if (! is_null($sites) && count($sites) > 0) { |
|
63
|
|
|
$arguments['site_id'] = implode(',', $sites); |
|
64
|
|
|
; |
|
65
|
|
|
} |
|
66
|
|
|
if (! is_null($engines) && count($engines) > 0) { |
|
67
|
|
|
$arguments['engines'] = implode(',', $engines); |
|
68
|
|
|
} |
|
69
|
|
|
$response = $this->performQuery('bulk/ranks', $arguments); |
|
70
|
|
|
|
|
71
|
|
|
return (int) $response['Result']['Id']; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function status($bulkJobID) |
|
75
|
|
|
{ |
|
76
|
|
|
$response = $this->performQuery('bulk/status', ['id' => $bulkJobID]); |
|
77
|
|
|
|
|
78
|
|
|
return $this->transformBulkJobStatus($response['Result']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function delete($bulkJobID) |
|
82
|
|
|
{ |
|
83
|
|
|
$response = $this->performQuery('bulk/delete', ['id' => $bulkJobID]); |
|
84
|
|
|
|
|
85
|
|
|
return (int)$response['Result']['Id']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
public function siteRankingDistributions($date) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$this->validateBulkDate($date); |
|
91
|
|
|
|
|
92
|
|
|
$response = $this->performQuery('bulk/site_ranking_distributions', ['date' => $date->toDateString()]); |
|
93
|
|
|
return (int)$response['Result']['Id']; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
public function tagRankingDistributions($date) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$this->validateBulkDate($date); |
|
99
|
|
|
|
|
100
|
|
|
$response = $this->performQuery('bulk/tag_ranking_distributions', ['date' => $date->toDateString()]); |
|
101
|
|
|
return (int) $response['Result']['Id']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function get($bulkJobID) |
|
105
|
|
|
{ |
|
106
|
|
|
$bulkStatus = $this->status($bulkJobID); |
|
107
|
|
|
|
|
108
|
|
|
if ($bulkStatus['status'] != 'Completed') { |
|
109
|
|
|
throw ApiException::resultError('Bulk Job is not completed. Current status: ' . $bulkJobID['status'] . '.'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$bulkStream = $this->statClient->downloadBulkJobStream($bulkStatus['stream_url']); |
|
113
|
|
|
|
|
114
|
|
|
return $this->parseBulkJob($bulkStream['Response']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param Carbon $date |
|
119
|
|
|
* @throws ApiException |
|
120
|
|
|
*/ |
|
121
|
|
|
private function validateBulkDate(Carbon $date) |
|
122
|
|
|
{ |
|
123
|
|
|
if ($date->isSameDay(Carbon::now()) or $date->isFuture()) { |
|
|
|
|
|
|
124
|
|
|
throw ApiException::resultError('You cannot create bulk jobs for the current date or dates in the future.'); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
private function parseBulkJob($bulkStream) |
|
130
|
|
|
{ |
|
131
|
|
|
$projects = $this->getCollection($bulkStream['Project']); |
|
132
|
|
|
|
|
133
|
|
|
$projects->transform(function ($project, $key) { |
|
|
|
|
|
|
134
|
|
|
return $this->transformProject($project); |
|
135
|
|
|
}); |
|
136
|
|
|
|
|
137
|
|
|
return $projects; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function transformProject($project) |
|
141
|
|
|
{ |
|
142
|
|
|
$transformedProject = new StatProject(); |
|
143
|
|
|
$transformedProject->id = $project['Id']; |
|
144
|
|
|
$transformedProject->name = $project['Name']; |
|
145
|
|
|
$transformedProject->total_sites = $project['TotalSites']; |
|
146
|
|
|
$transformedProject->created_at = $project['CreatedAt']; |
|
147
|
|
|
|
|
148
|
|
|
if ($project['TotalSites'] == 0) { |
|
149
|
|
|
$transformedProject->sites = collect(); |
|
150
|
|
|
} |
|
151
|
|
|
if ($project['TotalSites'] == 1) { |
|
152
|
|
|
$transformedProject->sites = collect([$project['Site']]); |
|
153
|
|
|
} |
|
154
|
|
|
if ($project['TotalSites'] > 1) { |
|
155
|
|
|
$transformedProject->sites = collect($project['Site']); |
|
156
|
|
|
} |
|
157
|
|
|
$transformedProject->sites->transform(function ($site, $key) { |
|
|
|
|
|
|
158
|
|
|
return $this->transformSite($site); |
|
159
|
|
|
}); |
|
160
|
|
|
|
|
161
|
|
|
return $transformedProject; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
private function transformSite($site) |
|
166
|
|
|
{ |
|
167
|
|
|
$transformedSite = new StatSite(); |
|
168
|
|
|
$transformedSite->id = $site['Id']; |
|
169
|
|
|
$transformedSite->url = $site['Url']; |
|
170
|
|
|
$transformedSite->total_keywords = $site['TotalKeywords']; |
|
171
|
|
|
$transformedSite->created_at = $site['CreatedAt']; |
|
172
|
|
|
|
|
173
|
|
|
if (array_key_exists('Keyword', $site)) { |
|
174
|
|
|
$transformedSite->keywords = collect($site['Keyword'])->transform(function ($keyword, $key) { |
|
|
|
|
|
|
175
|
|
|
return $this->transformKeyword($keyword); |
|
176
|
|
|
}); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (array_key_exists('RankDistribution', $site)) { |
|
180
|
|
|
$transformedSite->rank_distribution = $this->transformRankDistribution($site['RankDistribution']); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if (array_key_exists('Tag', $site)) { |
|
184
|
|
|
$transformedSite->tags = $this->getCollection($site['Tag'])->transform(function ($tag, $key) { |
|
|
|
|
|
|
185
|
|
|
return $this->transformTag($tag); |
|
186
|
|
|
}); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
return $transformedSite; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
private function transformKeyword($keyword) |
|
194
|
|
|
{ |
|
195
|
|
|
$modifiedKeyword = new StatKeyword(); |
|
196
|
|
|
$modifiedKeyword->id = $keyword['Id']; |
|
197
|
|
|
$modifiedKeyword->keyword = $keyword['Keyword']; |
|
198
|
|
|
$modifiedKeyword->keyword_market = $keyword['KeywordMarket']; |
|
199
|
|
|
$modifiedKeyword->keyword_location = $keyword['KeywordLocation']; |
|
200
|
|
|
$modifiedKeyword->keyword_device = $keyword['KeywordDevice']; |
|
201
|
|
|
$modifiedKeyword->keyword_categories = $keyword['KeywordCategories']; |
|
202
|
|
|
|
|
203
|
|
View Code Duplication |
if (is_null($keyword['KeywordTags'])) { |
|
|
|
|
|
|
204
|
|
|
$modifiedKeyword->keyword_tags = collect(); |
|
205
|
|
|
} else { |
|
206
|
|
|
$modifiedKeyword->keyword_tags = collect(explode(',', $keyword['KeywordTags'])); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
View Code Duplication |
if (is_null($keyword['KeywordStats'])) { |
|
|
|
|
|
|
210
|
|
|
$modifiedKeyword->keyword_stats = null; |
|
211
|
|
|
} else { |
|
212
|
|
|
$localTrends = collect($keyword['KeywordStats']['LocalSearchTrendsByMonth'])->map(function ($searchVolume, $month) { |
|
213
|
|
|
return new StatLocalSearchTrend([ |
|
214
|
|
|
'month' => strtolower($month), |
|
215
|
|
|
'search_volume' => ($searchVolume == '-') ? null : $searchVolume, |
|
216
|
|
|
]); |
|
217
|
|
|
}); |
|
218
|
|
|
|
|
219
|
|
|
$modifiedKeyword->keyword_stats = new StatKeywordStats([ |
|
220
|
|
|
'advertiser_competition' => $keyword['KeywordStats']['AdvertiserCompetition'], |
|
221
|
|
|
'global_search_volume' => $keyword['KeywordStats']['GlobalSearchVolume'], |
|
222
|
|
|
'targeted_search_volume' => $keyword['KeywordStats']['TargetedSearchVolume'], |
|
223
|
|
|
'cpc' => $keyword['KeywordStats']['CPC'], |
|
224
|
|
|
'local_search_trends_by_month' => $localTrends->values(), |
|
225
|
|
|
]); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$modifiedKeyword->created_at = $keyword['CreatedAt']; |
|
229
|
|
|
|
|
230
|
|
|
$modifiedKeyword->ranking = new StatKeywordRanking([ |
|
231
|
|
|
'date' => $keyword['Ranking']['date'], |
|
232
|
|
|
'type' => $keyword['Ranking']['type'] |
|
233
|
|
|
]); |
|
234
|
|
|
|
|
235
|
|
View Code Duplication |
if (array_key_exists('Google', $keyword['Ranking'])) { |
|
|
|
|
|
|
236
|
|
|
$modifiedKeyword->ranking->google = $this->analyzeRanking($keyword['Ranking']['Google'], $keyword['Ranking']['type']); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
View Code Duplication |
if (array_key_exists('Yahoo', $keyword['Ranking'])) { |
|
|
|
|
|
|
240
|
|
|
$modifiedKeyword->ranking->yahoo = $this->analyzeRanking($keyword['Ranking']['Yahoo'], $keyword['Ranking']['type']); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
View Code Duplication |
if (array_key_exists('Bing', $keyword['Ranking'])) { |
|
|
|
|
|
|
244
|
|
|
$modifiedKeyword->ranking->bing = $this->analyzeRanking($keyword['Ranking']['Bing'], $keyword['Ranking']['type']); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $modifiedKeyword; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
private function analyzeRanking($rankingForEngine, $rankingType) |
|
251
|
|
|
{ |
|
252
|
|
|
if ($rankingType == 'highest') { |
|
253
|
|
|
return $this->transformRanking($rankingForEngine); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if (is_null($rankingForEngine['Result'])) { |
|
257
|
|
|
return null; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$rankings = $this->getCollection($rankingForEngine['Result'], 'Rank'); |
|
261
|
|
|
|
|
262
|
|
|
$rankings->transform(function ($ranking, $key) { |
|
|
|
|
|
|
263
|
|
|
return $this->transformRanking($ranking); |
|
264
|
|
|
}); |
|
265
|
|
|
|
|
266
|
|
|
return $rankings; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
private function transformRanking($ranking) |
|
270
|
|
|
{ |
|
271
|
|
|
$transformedRanking = new StatKeywordEngineRanking(); |
|
272
|
|
|
$transformedRanking->rank = $ranking['Rank']; |
|
273
|
|
|
if (array_key_exists('BaseRank', $ranking)) { |
|
274
|
|
|
$transformedRanking->base_rank = $ranking['BaseRank']; |
|
275
|
|
|
} |
|
276
|
|
|
$transformedRanking->url = $ranking['Url']; |
|
277
|
|
|
|
|
278
|
|
|
return $transformedRanking; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
private function transformTag($tag) |
|
282
|
|
|
{ |
|
283
|
|
|
$modifiedTag = new StatTag(); |
|
284
|
|
|
$modifiedTag->id = $tag['Id']; |
|
285
|
|
|
$modifiedTag->tag = $tag['Tag']; |
|
286
|
|
|
|
|
287
|
|
|
if (isset($tag['RankDistribution'])) { |
|
288
|
|
|
$modifiedTag->rank_distribution = $this->transformRankDistribution($tag['RankDistribution']); |
|
289
|
|
|
} else { |
|
290
|
|
|
$modifiedTag->rank_distribution = null; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
return $modifiedTag; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
private function transformBulkJobStatus($job) |
|
297
|
|
|
{ |
|
298
|
|
|
$bulkJob = new StatBulkJob(); |
|
299
|
|
|
$bulkJob->id = $job['Id']; |
|
300
|
|
|
$bulkJob->job_type = $job['JobType']; |
|
301
|
|
|
$bulkJob->format = $job['Format']; |
|
302
|
|
|
|
|
303
|
|
|
if(array_has($job, ['Project', 'Folder', 'SiteTitle', 'SiteUrl'])){ |
|
304
|
|
|
$bulkJob->project = $job['Project']; |
|
305
|
|
|
$bulkJob->folder = $job['Folder']; |
|
306
|
|
|
$bulkJob->site_title = $job['SiteTitle']; |
|
307
|
|
|
$bulkJob->site_url = $job['SiteUrl']; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$bulkJob->date = $job['Date']; |
|
311
|
|
|
|
|
312
|
|
|
$bulkJob->sites = collect(); |
|
313
|
|
|
if (array_has($job, 'SiteId')) { |
|
314
|
|
|
$bulkJob->sites = collect(explode(',', $job['SiteId'])) |
|
315
|
|
|
->transform(function ($site, $key) { |
|
|
|
|
|
|
316
|
|
|
return (int)$site; |
|
317
|
|
|
}); |
|
318
|
|
|
} |
|
319
|
|
|
$bulkJob->status = $job['Status']; |
|
320
|
|
|
$bulkJob->url = array_get($job, 'Url', null); |
|
321
|
|
|
$bulkJob->stream_url = array_get($job, 'StreamUrl', null); |
|
322
|
|
|
$bulkJob->created_at = $job['CreatedAt']; |
|
323
|
|
|
|
|
324
|
|
|
return $bulkJob; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.