| 1 |
<?php |
| 2 |
/** |
| 3 |
* Site Traffic Section — sessions and active users from Google Analytics 4. |
| 4 |
* |
| 5 |
* Only renders when a GA4 property is connected; the report is built from |
| 6 |
* Search Console and this card is a bonus, not a requirement (#742). The |
| 7 |
* dashboard fetch covers the current window only, so no change is shown. |
| 8 |
* |
| 9 |
* @package ThinkRank |
| 10 |
* @subpackage SEO\Email_Report_Sections |
| 11 |
* @since 2.8.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
declare(strict_types=1); |
| 15 |
|
| 16 |
namespace ThinkRank\SEO\Email_Report_Sections; |
| 17 |
|
| 18 |
if (!defined('ABSPATH')) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
final class Site_Traffic_Section implements Email_Report_Section_Interface { |
| 23 |
|
| 24 |
public function key(): string { |
| 25 |
return 'site_traffic'; |
| 26 |
} |
| 27 |
|
| 28 |
public function label(): string { |
| 29 |
return __('Site traffic (Google Analytics 4)', 'thinkrank'); |
| 30 |
} |
| 31 |
|
| 32 |
public function default_enabled(): bool { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
public function requires_capability(): ?string { |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
public function renders_own_heading(): bool { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
public function collect(array $context): array { |
| 45 |
$shared = $context['shared'] ?? []; |
| 46 |
if (empty($shared['available'])) { |
| 47 |
return []; |
| 48 |
} |
| 49 |
$traffic = $shared['current']['traffic'] ?? []; |
| 50 |
if (!is_array($traffic) || $traffic === []) { |
| 51 |
return []; |
| 52 |
} |
| 53 |
return [ |
| 54 |
'sessions' => (int) ($traffic['sessions'] ?? 0), |
| 55 |
'users' => (int) ($traffic['active_users'] ?? $traffic['users'] ?? 0), |
| 56 |
'pageviews' => (int) ($traffic['pageviews'] ?? 0), |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public function has_data(array $payload): bool { |
| 61 |
return (int) ($payload['sessions'] ?? 0) > 0 || (int) ($payload['users'] ?? 0) > 0; |
| 62 |
} |
| 63 |
|
| 64 |
public function render(array $payload): string { |
| 65 |
if (!$this->has_data($payload)) { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
$tiles = [ |
| 69 |
Email_Report_Html::stat([ |
| 70 |
'label' => __('Sessions', 'thinkrank'), |
| 71 |
'value' => number_format_i18n((int) $payload['sessions']), |
| 72 |
'icon' => 'sessions', |
| 73 |
]), |
| 74 |
Email_Report_Html::stat([ |
| 75 |
'label' => __('Active users', 'thinkrank'), |
| 76 |
'value' => number_format_i18n((int) $payload['users']), |
| 77 |
'icon' => 'users', |
| 78 |
]), |
| 79 |
]; |
| 80 |
return Email_Report_Html::heading(__('Site traffic', 'thinkrank'), __('From Google Analytics 4, all channels', 'thinkrank')) |
| 81 |
. Email_Report_Html::stat_grid($tiles) |
| 82 |
. Email_Report_Html::link(__('Open the analytics dashboard', 'thinkrank'), Email_Report_Html::admin_link('analytics', 'dashboard')); |
| 83 |
} |
| 84 |
|
| 85 |
public function fallback_html(): string { |
| 86 |
return ''; |
| 87 |
} |
| 88 |
} |
| 89 |
|