|
2 | 2 |
|
3 | 3 | namespace SimpleAnalytics; |
4 | 4 |
|
5 | | -use SimpleAnalytics\Actions\AnalyticsCode; |
6 | 5 | use SimpleAnalytics\Settings\{Page, Tab}; |
| 6 | +use SimpleAnalytics\Actions\AddInactiveComment; |
| 7 | +use SimpleAnalytics\Actions\AddNoScriptTag; |
| 8 | +use SimpleAnalytics\Scripts\AnalyticsScript; |
| 9 | +use SimpleAnalytics\Scripts\AutomatedEventsScript; |
| 10 | +use SimpleAnalytics\Scripts\InactiveScript; |
7 | 11 |
|
8 | 12 | class Plugin |
9 | 13 | { |
10 | | - public function register(): void |
| 14 | + public function boot(): void |
11 | 15 | { |
12 | | - AnalyticsCode::register(); |
| 16 | + add_action('init', $this->onInit(...)); |
| 17 | + is_admin() && $this->defineAdminPage(); |
| 18 | + } |
| 19 | + |
| 20 | + public function onInit(): void |
| 21 | + { |
| 22 | + $shouldCollect = (new TrackingPolicy)->shouldCollectAnalytics(); |
| 23 | + |
| 24 | + $this->addScripts($shouldCollect); |
13 | 25 |
|
14 | | - $this->defineAdminPage(); |
| 26 | + if (! $shouldCollect) { |
| 27 | + AddInactiveComment::register(); |
| 28 | + } |
| 29 | + |
| 30 | + if ($shouldCollect && Setting::boolean(SettingName::NOSCRIPT)) { |
| 31 | + AddNoScriptTag::register(); |
| 32 | + } |
15 | 33 | } |
16 | 34 |
|
17 | | - protected function defineAdminPage(): void |
| 35 | + protected function addScripts(bool $collect): void |
18 | 36 | { |
19 | | - if (! is_admin()) return; |
| 37 | + $scripts = new ScriptManager; |
| 38 | + |
| 39 | + if ($collect) { |
| 40 | + $scripts->add(new AnalyticsScript); |
| 41 | + } else { |
| 42 | + $scripts->add(new InactiveScript); |
| 43 | + } |
20 | 44 |
|
| 45 | + if (Setting::boolean(SettingName::AUTOMATED_EVENTS)) { |
| 46 | + $scripts->add(new AutomatedEventsScript); |
| 47 | + } |
| 48 | + |
| 49 | + $scripts->register(); |
| 50 | + } |
| 51 | + |
| 52 | + protected function defineAdminPage(): void |
| 53 | + { |
21 | 54 | Page::title('Simple Analytics') |
22 | 55 | ->slug('simpleanalytics') |
23 | 56 | ->tab('General', function (Tab $tab) { |
@@ -62,6 +95,10 @@ protected function defineAdminPage(): void |
62 | 95 | ->description('In case you don’t want to auto collect page views, but via `sa_pageview` function in JavaScript.') |
63 | 96 | ->docs('https://docs.simpleanalytics.com/trigger-custom-page-views#use-custom-collection-anyway'); |
64 | 97 |
|
| 98 | + $tab->checkbox(SettingName::NOSCRIPT, 'Support no JavaScript mode') |
| 99 | + ->description('Collect analytics from visitors with disabled or no JavaScript.') |
| 100 | + ->default(false); |
| 101 | + |
65 | 102 | $tab->input(SettingName::ONLOAD_CALLBACK, 'Onload Callback') |
66 | 103 | ->description('JavaScript function to call when the script is loaded.') |
67 | 104 | ->placeholder('Example: sa_event("My event")') |
|
0 commit comments