PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.7.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.7.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 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 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.7.0, at includes/integrations/class-google-search-console-client.php

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