PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.12.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.12.0
2.12.0 2.11.0 2.10.0 2.9.0 2.8.0 2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk All 53 releases
thinkrank / includes / integrations / class-google-search-console-client.php

class-google-search-console-client.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.12.0, at includes/integrations/class-google-search-console-client.php

671 lines 25.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Google Search Console Client Class
5 *
6 * Handles communication with Google Search Console API for search performance
7 * data retrieval and site verification. Extends the base Google API client with
8 * Search Console-specific functionality and rate limiting.
9 *
10 * @package ThinkRank\Integrations
11 * @since 1.0.0
12 */
13
14 declare(strict_types=1);
15
16 namespace ThinkRank\Integrations;
17
18 // Prevent direct access
19 if (!defined('ABSPATH')) {
20 exit;
21 }
22
23 /**
24 * Google Search Console Client Class
25 *
26 * Single Responsibility: Handle Google Search Console API communication
27 * Following ThinkRank HTTP client patterns from Claude_Client and OpenAI_Client
28 *
29 * @since 1.0.0
30 */
31 class Google_Search_Console_Client extends Google_API_Base_Client {
32
33 /**
34 * Google Search Console API base URL
35 */
36 private const API_BASE_URL = 'https://www.googleapis.com/webmasters/v3';
37
38 /**
39 * Rate limit transient key prefix
40 * Following ThinkRank option naming patterns
41 */
42 private const RATE_LIMIT_KEY = 'thinkrank_gsc_rate_limit';
43
44 /**
45 * Maximum requests per day (Google standard quota)
46 */
47 private const MAX_REQUESTS_PER_DAY = 2000;
48
49 /**
50 * Test API connection
51 * Following ThinkRank test_connection patterns from AI clients
52 *
53 * @return array Connection test results
54 */
55 public function test_connection(): array {
56 try {
57 $result = $this->list_sites();
58
59 return [
60 'success' => true,
61 'message' => 'Google Search Console API connection successful',
62 'sites_count' => count($result['siteEntry'] ?? [])
63 ];
64 } catch (\Exception $e) {
65 // `message` as well as `error`: the success branch above reports
66 // its outcome as `message`, and every consumer reads that key, so
67 // a failure that only set `error` was reported to the admin as an
68 // error with no reason at all (#852). `error` is kept for any
69 // caller that already reads it.
70 return [
71 'success' => false,
72 'message' => $e->getMessage(),
73 'error' => $e->getMessage()
74 ];
75 }
76 }
77
78 /**
79 * List verified sites in Search Console
80 *
81 * @return array List of verified sites
82 * @throws \Exception If API request fails
83 */
84 public function list_sites(): array {
85 $endpoint = '/sites';
86 // The API key (when no OAuth token) is sent via the x-goog-api-key
87 // header by the base client — not the query string.
88 $full_url = self::API_BASE_URL . $endpoint;
89 return $this->make_request($full_url, [], 'GET');
90 }
91
92 /**
93 * Verify site ownership in Search Console
94 *
95 * @param string $site_url Site URL to verify
96 * @param string $verification_method Verification method used
97 * @return array Verification results
98 */
99 public function verify_site(string $site_url, string $verification_method = 'meta'): array {
100 try {
101 // Check if the site is already verified by listing sites
102 $sites = $this->list_sites();
103 $site_verified = false;
104
105 foreach ($sites['siteEntry'] ?? [] as $site) {
106 if ($site['siteUrl'] === $site_url) {
107 $site_verified = true;
108 break;
109 }
110 }
111
112 return [
113 'success' => $site_verified,
114 'message' => $site_verified ? 'Site is verified in Search Console' : 'Site not found in Search Console',
115 'site_url' => $site_url,
116 'verification_method' => $verification_method
117 ];
118 } catch (\Exception $e) {
119 return [
120 'success' => false,
121 'error' => $e->getMessage()
122 ];
123 }
124 }
125
126 /**
127 * Get search performance data with flexible dimensions
128 *
129 * @param string $site_url Site URL to get data for
130 * @param string $date_range Date range ('7d', '30d', '90d')
131 * @param array $dimensions Dimensions to group by (query, page, country, device, searchAppearance)
132 * @param int $row_limit Maximum number of rows to return
133 * @return array Search performance data
134 * @throws \Exception If API request fails
135 */
136 public function get_search_performance(string $site_url, string $date_range = '30d', array $dimensions = ['query'], int $row_limit = 1000): array {
137 // GSC data for the current day is never complete; use yesterday as the end date
138 // so the N-day window matches exactly what the GSC dashboard shows.
139 $days = (int) str_replace('d', '', $date_range);
140 $end_date = gmdate('Y-m-d', strtotime('-2 days'));
141 $start_date = gmdate('Y-m-d', strtotime('-' . ($days - 1) . ' days', strtotime($end_date)));
142
143 $endpoint = '/sites/' . rawurlencode($site_url) . '/searchAnalytics/query';
144
145 $request_body = [
146 'startDate' => $start_date,
147 'endDate' => $end_date,
148 'dimensions' => $dimensions,
149 'rowLimit' => $row_limit,
150 'dataState' => 'all',
151 ];
152
153 $full_url = self::API_BASE_URL . $endpoint;
154 return $this->make_request($full_url, $request_body, 'POST');
155 }
156
157 /**
158 * Get aggregated search totals (clicks, impressions, ctr, position)
159 *
160 * @param string $site_url Site URL to get data for
161 * @param string $date_range Date range ('7d', '30d', '90d')
162 * @return array Aggregated totals
163 * @throws \Exception If API request fails
164 */
165 public function get_search_totals(string $site_url, string $date_range = '30d'): array {
166 // GSC data has a 2-day delay; use D-2 as end_date to match the GSC dashboard.
167 $days = (int) str_replace('d', '', $date_range);
168 $end_date = gmdate('Y-m-d', strtotime('-2 days'));
169 $start_date = gmdate('Y-m-d', strtotime('-' . ($days - 1) . ' days', strtotime($end_date)));
170
171 $endpoint = '/sites/' . rawurlencode($site_url) . '/searchAnalytics/query';
172
173 // diverse from get_search_performance: no dimensions, just totals
174 $request_body = [
175 'startDate' => $start_date,
176 'endDate' => $end_date,
177 'dimensions' => [], // Empty dimensions for aggregation
178 'rowLimit' => 1, // We only need the totals, but API might require at least 1
179 'dataState' => 'all',
180 ];
181
182 $full_url = self::API_BASE_URL . $endpoint;
183 $response = $this->make_request($full_url, $request_body, 'POST');
184
185 // The API returns rows even if we don't ask for dimensions?
186 // Actually, without dimensions, it returns one row with aggregated values if successful.
187 // Or sometimes it returns just the aggregates if available.
188 // Let's inspect the response format for GSC API v3.
189 // "If no dimensions are requested, the response will contain a single row with the aggregated values."
190
191 if (!empty($response['rows'])) {
192 $row = $response['rows'][0];
193 return [
194 'clicks' => $row['clicks'] ?? 0,
195 'impressions' => $row['impressions'] ?? 0,
196 'ctr' => round(($row['ctr'] ?? 0) * 100, 2),
197 'position' => round($row['position'] ?? 0, 1)
198 ];
199 }
200
201 return [
202 'clicks' => 0,
203 'impressions' => 0,
204 'ctr' => 0,
205 'position' => 0
206 ];
207 }
208
209 /**
210 * Get aggregated search totals for explicit start/end dates
211 *
212 * @param string $site_url Site URL to get data for
213 * @param string $start_date Start date (Y-m-d)
214 * @param string $end_date End date (Y-m-d)
215 * @return array Aggregated totals
216 * @throws \Exception If API request fails
217 */
218 public function get_search_totals_by_dates(string $site_url, string $start_date, string $end_date): array {
219 $endpoint = '/sites/' . rawurlencode($site_url) . '/searchAnalytics/query';
220
221 $request_body = [
222 'startDate' => $start_date,
223 'endDate' => $end_date,
224 'dimensions' => [],
225 'rowLimit' => 1,
226 'dataState' => 'all',
227 ];
228
229 $full_url = self::API_BASE_URL . $endpoint;
230 $response = $this->make_request($full_url, $request_body, 'POST');
231
232 if (!empty($response['rows'])) {
233 $row = $response['rows'][0];
234 return [
235 'clicks' => $row['clicks'] ?? 0,
236 'impressions' => $row['impressions'] ?? 0,
237 'ctr' => round(($row['ctr'] ?? 0) * 100, 2),
238 'position' => round($row['position'] ?? 0, 1),
239 ];
240 }
241
242 return ['clicks' => 0, 'impressions' => 0, 'ctr' => 0, 'position' => 0];
243 }
244
245 /**
246 * Get query-level search performance for explicit start/end dates
247 *
248 * @param string $site_url Site URL to get data for
249 * @param string $start_date Start date (Y-m-d)
250 * @param string $end_date End date (Y-m-d)
251 * @param int $row_limit Maximum rows to return
252 * @return array Raw rows from GSC API
253 * @throws \Exception If API request fails
254 */
255 public function get_search_performance_by_dates(string $site_url, string $start_date, string $end_date, int $row_limit = 500, array $dimensions = ['query']): array {
256 $endpoint = '/sites/' . rawurlencode($site_url) . '/searchAnalytics/query';
257
258 $request_body = [
259 'startDate' => $start_date,
260 'endDate' => $end_date,
261 'dimensions' => $dimensions,
262 'rowLimit' => $row_limit,
263 // 'all' includes both finalised data and fresh (still-processing)
264 // data — matches what the Search Console web UI displays, so the
265 // last 2-4 days aren't missing.
266 'dataState' => 'all',
267 ];
268
269 $full_url = self::API_BASE_URL . $endpoint;
270 $response = $this->make_request($full_url, $request_body, 'POST');
271
272 return $response['rows'] ?? [];
273 }
274
275 /**
276 * Get top search queries
277 *
278 * @param string $site_url Site URL to get data for
279 * @param int $limit Number of queries to retrieve
280 * @return array Top search queries
281 * @throws \Exception If API request fails
282 */
283 public function get_top_queries(string $site_url, int $limit = 10): array {
284 try {
285 $performance_data = $this->get_search_performance($site_url, '30d');
286 $queries = [];
287
288 foreach ($performance_data['rows'] ?? [] as $row) {
289 if (count($queries) >= $limit) {
290 break;
291 }
292
293 $queries[] = [
294 'query' => $row['keys'][0] ?? '',
295 'clicks' => $row['clicks'] ?? 0,
296 'impressions' => $row['impressions'] ?? 0,
297 'ctr' => $row['ctr'] ?? 0,
298 'position' => $row['position'] ?? 0
299 ];
300 }
301
302 return [
303 'queries' => $queries,
304 'site_url' => $site_url,
305 'limit' => $limit
306 ];
307 } catch (\Exception $e) {
308 return [
309 'queries' => [],
310 'site_url' => $site_url,
311 'limit' => $limit,
312 'error' => $e->getMessage()
313 ];
314 }
315 }
316
317 /**
318 * Get device performance breakdown for mobile SEO insights
319 *
320 * @param string $site_url Site URL to get data for
321 * @param string $date_range Date range for data
322 * @return array Device performance data
323 * @throws \Exception If API request fails
324 */
325 public function get_device_performance(string $site_url, string $date_range = '30d'): array {
326 $result = $this->get_search_performance($site_url, $date_range, ['device'], 10);
327
328 $devices = [];
329 $rows = $result['rows'] ?? [];
330
331 foreach ($rows as $row) {
332 $device = $row['keys'][0] ?? '';
333 $devices[$device] = [
334 'clicks' => $row['clicks'] ?? 0,
335 'impressions' => $row['impressions'] ?? 0,
336 'ctr' => round(($row['ctr'] ?? 0) * 100, 2),
337 'position' => round($row['position'] ?? 0, 1)
338 ];
339 }
340
341 return [
342 'devices' => $devices,
343 'site_url' => $site_url,
344 'date_range' => $date_range
345 ];
346 }
347
348 /**
349 * Get search appearance data for rich results tracking
350 *
351 * @param string $site_url Site URL to get data for
352 * @param string $date_range Date range for data
353 * @return array Search appearance data
354 * @throws \Exception If API request fails
355 */
356 public function get_search_appearance(string $site_url, string $date_range = '30d'): array {
357 $result = $this->get_search_performance($site_url, $date_range, ['searchAppearance'], 20);
358
359 $appearances = [];
360 $rows = $result['rows'] ?? [];
361
362 foreach ($rows as $row) {
363 $appearance = $row['keys'][0] ?? '';
364 $appearances[$appearance] = [
365 'clicks' => $row['clicks'] ?? 0,
366 'impressions' => $row['impressions'] ?? 0,
367 'ctr' => round(($row['ctr'] ?? 0) * 100, 2),
368 'position' => round($row['position'] ?? 0, 1)
369 ];
370 }
371
372 return [
373 'appearances' => $appearances,
374 'site_url' => $site_url,
375 'date_range' => $date_range
376 ];
377 }
378
379 /**
380 * Get keyword opportunities for SEO insights
381 * Identifies queries with high impressions but low CTR or position
382 *
383 * @param string $site_url Site URL to analyze
384 * @param string $date_range Date range for analysis
385 * @param int $min_impressions Minimum impressions threshold
386 * @return array Keyword opportunities
387 * @throws \Exception If API request fails
388 */
389 public function get_keyword_opportunities(string $site_url, string $date_range = '30d', int $min_impressions = 100): array {
390 $result = $this->get_search_performance($site_url, $date_range, ['query'], 500);
391
392 $opportunities = [];
393 $rows = $result['rows'] ?? [];
394
395 foreach ($rows as $row) {
396 $impressions = $row['impressions'] ?? 0;
397 $ctr = $row['ctr'] ?? 0;
398 $position = $row['position'] ?? 0;
399 $clicks = $row['clicks'] ?? 0;
400
401 // Identify opportunities: high impressions, low CTR, or position 4-10
402 if ($impressions >= $min_impressions) {
403 $opportunity_score = 0;
404 $opportunity_reasons = [];
405
406 // Low CTR opportunity
407 if ($ctr < 0.05 && $position <= 10) { // Less than 5% CTR in top 10
408 $opportunity_score += 30;
409 $opportunity_reasons[] = 'Low CTR for top 10 position';
410 }
411
412 // Position 4-10 opportunity (could reach top 3)
413 if ($position >= 4 && $position <= 10) {
414 $opportunity_score += 40;
415 $opportunity_reasons[] = 'Ranking 4-10, potential for top 3';
416 }
417
418 // High impressions, low clicks
419 if ($impressions > 500 && $clicks < 25) {
420 $opportunity_score += 20;
421 $opportunity_reasons[] = 'High impressions but low clicks';
422 }
423
424 if ($opportunity_score > 0) {
425 $opportunities[] = [
426 'query' => $row['keys'][0] ?? '',
427 'clicks' => $clicks,
428 'impressions' => $impressions,
429 'ctr' => round($ctr * 100, 2),
430 'position' => round($position, 1),
431 'opportunity_score' => $opportunity_score,
432 'reasons' => $opportunity_reasons
433 ];
434 }
435 }
436 }
437
438 // Sort by opportunity score (highest first)
439 usort($opportunities, function ($a, $b) {
440 return $b['opportunity_score'] <=> $a['opportunity_score'];
441 });
442
443 return [
444 'opportunities' => array_slice($opportunities, 0, 50), // Top 50 opportunities
445 'site_url' => $site_url,
446 'date_range' => $date_range,
447 'total_opportunities' => count($opportunities)
448 ];
449 }
450
451 /**
452 * Return branded vs non-branded click/impression split that matches the GSC platform.
453 *
454 * Uses two server-side aggregate calls per period (empty dimensions + dimensionFilterGroups)
455 * so the totals are exact — not limited by the 1 000-row query cap:
456 *
457 * • Call A: no filter → real site total clicks
458 * • Call B: query contains brand → branded clicks
459 * • Non-branded = A − B
460 *
461 * Also fetches the equivalent previous period so the frontend can render trend arrows.
462 *
463 * @param string $site_url Registered GSC property URL
464 * @param string $date_range '7d' | '30d' | '90d'
465 * @param string $brand_name Comma-separated brand keywords. Auto-derived from domain when empty.
466 * @return array {
467 * branded, non_branded, previous: { branded, non_branded },
468 * brand_terms, total_clicks, site_url, date_range
469 * }
470 */
471 public function get_branded_performance(string $site_url, string $date_range = '30d', string $brand_name = ''): array {
472 $days = max(1, (int) str_replace('d', '', $date_range));
473 $end = gmdate('Y-m-d', strtotime('-2 days'));
474 $start = gmdate('Y-m-d', strtotime('-' . ($days - 1) . ' days', strtotime($end)));
475
476 $prev_end = gmdate('Y-m-d', strtotime('-1 day', strtotime($start)));
477 $prev_start = gmdate('Y-m-d', strtotime('-' . ($days - 1) . ' days', strtotime($prev_end)));
478
479 // Auto-derive brand from domain when not provided.
480 // Handles both URL-prefix (https://example.com) and domain (sc-domain:example.com) formats.
481 if (empty($brand_name)) {
482 $stripped = preg_replace('#^sc-domain:#i', '', $site_url);
483 $host = wp_parse_url($stripped, PHP_URL_HOST) ?? wp_parse_url('https://' . $stripped, PHP_URL_HOST) ?? $stripped;
484 $host = preg_replace('/^www\./i', '', (string) $host);
485 $brand_name = strtolower(explode('.', $host)[0]);
486 }
487 $terms = array_values(array_filter(array_map('trim', explode(',', strtolower($brand_name)))));
488
489 // For hyphenated brands (e.g. "essential-blocks") also match the space variant
490 // ("essential blocks") since users type both forms in Google searches.
491 $extra = [];
492 foreach ($terms as $t) {
493 if (str_contains($t, '-')) {
494 $spaced = str_replace('-', ' ', $t);
495 if (!in_array($spaced, $terms, true)) {
496 $extra[] = $spaced;
497 }
498 }
499 }
500 $terms = array_values(array_merge($terms, $extra));
501
502 $split_cur = $this->gsc_split_by_brand($site_url, $start, $end, $terms);
503 $split_prev = $this->gsc_split_by_brand($site_url, $prev_start, $prev_end, $terms);
504
505 return [
506 'branded' => $split_cur['branded'],
507 'non_branded' => $split_cur['non_branded'],
508 'previous' => [
509 'branded' => $split_prev['branded'],
510 'non_branded' => $split_prev['non_branded'],
511 ],
512 'brand_terms' => $terms,
513 'total_clicks' => $split_cur['total_clicks'],
514 'site_url' => $site_url,
515 'date_range' => $date_range,
516 ];
517 }
518
519 /**
520 * Fetch all web query rows for a date window and split into branded / non-branded
521 * using a single API call. Both totals come from the same data set so the
522 * percentages always add up to 100 %.
523 *
524 * @param string $site_url GSC property URL
525 * @param string $start Start date (Y-m-d)
526 * @param string $end End date (Y-m-d)
527 * @param string[] $brand_terms Brand keywords to match (substring, case-insensitive)
528 * @return array { branded: {...}, non_branded: {...}, total_clicks: int }
529 */
530 private function gsc_split_by_brand(string $site_url, string $start, string $end, array $brand_terms): array {
531 $url = self::API_BASE_URL . '/sites/' . rawurlencode($site_url) . '/searchAnalytics/query';
532
533 $page_size = 25000;
534 $start_row = 0;
535 $total_clicks = 0;
536 $branded_clicks = 0;
537 $total_impr = 0;
538 $branded_impr = 0;
539
540 // Safety cap so a runaway query can never loop unbounded. With a 25k
541 // page size this stops after ~100k rows (4 pages), which is far beyond
542 // the query volume of any real site for a single date window.
543 $max_pages = 4;
544 $pages_fetched = 0;
545
546 do {
547 $response = $this->make_request($url, [
548 'startDate' => $start,
549 'endDate' => $end,
550 'type' => 'web',
551 'dimensions' => ['query'],
552 'rowLimit' => $page_size,
553 'startRow' => $start_row,
554 'dataState' => 'all',
555 ], 'POST');
556
557 $rows = $response['rows'] ?? [];
558 foreach ($rows as $row) {
559 $query = strtolower($row['keys'][0] ?? '');
560 $clicks = (int) ($row['clicks'] ?? 0);
561 $impr = (int) ($row['impressions'] ?? 0);
562
563 $total_clicks += $clicks;
564 $total_impr += $impr;
565
566 foreach ($brand_terms as $term) {
567 if (str_contains($query, $term)) {
568 $branded_clicks += $clicks;
569 $branded_impr += $impr;
570 break;
571 }
572 }
573 }
574
575 $fetched = count($rows);
576 $start_row += $fetched;
577 $pages_fetched++;
578 } while ($fetched === $page_size && $pages_fetched < $max_pages);
579
580 $non_branded_clicks = max(0, $total_clicks - $branded_clicks);
581 $non_branded_impr = max(0, $total_impr - $branded_impr);
582
583 return [
584 'branded' => [
585 'clicks' => $branded_clicks,
586 'impressions' => $branded_impr,
587 'percentage' => $total_clicks > 0 ? round($branded_clicks / $total_clicks * 100) : 0,
588 ],
589 'non_branded' => [
590 'clicks' => $non_branded_clicks,
591 'impressions' => $non_branded_impr,
592 'percentage' => $total_clicks > 0 ? round($non_branded_clicks / $total_clicks * 100) : 0,
593 ],
594 'total_clicks' => $total_clicks,
595 ];
596 }
597
598 /**
599 * Get top countries by clicks from Search Console.
600 *
601 * Queries with the `country` dimension and returns rows sorted by clicks
602 * descending, each enriched with a percentage share of the total clicks.
603 *
604 * @param string $site_url Site URL to query
605 * @param string $date_range Date range ('7d', '30d', '90d')
606 * @param int $row_limit Maximum countries to return (default 10)
607 * @return array { countries: array, total_clicks: int, site_url: string, date_range: string }
608 */
609 public function get_country_performance(string $site_url, string $date_range = '30d', int $row_limit = 10): array {
610 $result = $this->get_search_performance($site_url, $date_range, ['country'], $row_limit);
611 $rows = $result['rows'] ?? [];
612
613 $total_clicks = 0;
614 foreach ($rows as $row) {
615 $total_clicks += (int) ($row['clicks'] ?? 0);
616 }
617
618 $countries = [];
619 foreach ($rows as $row) {
620 $clicks = (int) ($row['clicks'] ?? 0);
621 $countries[] = [
622 'country' => strtolower($row['keys'][0] ?? ''),
623 'clicks' => $clicks,
624 'impressions' => (int) ($row['impressions'] ?? 0),
625 'ctr' => round(($row['ctr'] ?? 0) * 100, 2),
626 'position' => round($row['position'] ?? 0, 1),
627 'percentage' => $total_clicks > 0 ? round(($clicks / $total_clicks) * 100) : 0,
628 ];
629 }
630
631 return [
632 'countries' => $countries,
633 'total_clicks' => $total_clicks,
634 'site_url' => $site_url,
635 'date_range' => $date_range,
636 ];
637 }
638
639 /**
640 * Get rate limit configuration
641 * Following ThinkRank rate limiting patterns
642 *
643 * @return array Rate limit configuration
644 */
645 protected function get_rate_limits(): array {
646 return [
647 'max_requests_per_day' => self::MAX_REQUESTS_PER_DAY,
648 'reset_time' => get_transient(self::RATE_LIMIT_KEY . '_reset') ?: strtotime('tomorrow')
649 ];
650 }
651
652 /**
653 * Get rate limit transient key
654 * Following ThinkRank option naming patterns
655 *
656 * @return string Rate limit key
657 */
658 protected function get_rate_limit_key(): string {
659 return self::RATE_LIMIT_KEY;
660 }
661
662 /**
663 * Get rate limit error message
664 *
665 * @return string Error message
666 */
667 protected function get_rate_limit_error_message(): string {
668 return 'Google Search Console API rate limit exceeded. Try again tomorrow.';
669 }
670 }
671