| 1 |
<?php |
| 2 |
|
| 3 |
namespace SimpleAnalytics; |
| 4 |
|
| 5 |
use SimpleAnalytics\Settings\{Page, Tab}; |
| 6 |
use SimpleAnalytics\Actions\AddInactiveComment; |
| 7 |
use SimpleAnalytics\Actions\AddNoScriptTag; |
| 8 |
use SimpleAnalytics\Actions\AddPluginSettingsLink; |
| 9 |
use SimpleAnalytics\Scripts\AnalyticsScript; |
| 10 |
use SimpleAnalytics\Scripts\AutomatedEventsScript; |
| 11 |
use SimpleAnalytics\Scripts\InactiveScript; |
| 12 |
|
| 13 |
final class Plugin |
| 14 |
{ |
| 15 |
use PluginLifecycle; |
| 16 |
|
| 17 |
protected function onBoot(): void |
| 18 |
{ |
| 19 |
if (is_admin()) { |
| 20 |
$this->defineAdminPage(); |
| 21 |
AddPluginSettingsLink::register(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function onActivation(): void |
| 26 |
{ |
| 27 |
$this->addOptions(); |
| 28 |
} |
| 29 |
|
| 30 |
public function onUninstall(): void |
| 31 |
{ |
| 32 |
$this->deleteOptions(); |
| 33 |
} |
| 34 |
|
| 35 |
public function onInit(): void |
| 36 |
{ |
| 37 |
$shouldCollect = (new TrackingPolicy)->shouldCollectAnalytics(); |
| 38 |
$this->addScripts($shouldCollect); |
| 39 |
if (! $shouldCollect) { |
| 40 |
AddInactiveComment::register(); |
| 41 |
} |
| 42 |
if ($shouldCollect && Setting::boolean(SettingName::NOSCRIPT)) { |
| 43 |
AddNoScriptTag::register(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register plugin options and autoload them. |
| 49 |
* |
| 50 |
* @note Eliminates any unnecessary database lookups by preloading on each request. |
| 51 |
* @note Thanks to option autoloading, we don't need to use one serialized option, but several. |
| 52 |
* This allows the usage of native hooks and filters and reduces the chance of data corruption. |
| 53 |
*/ |
| 54 |
protected function addOptions(): void |
| 55 |
{ |
| 56 |
// Add options with default values |
| 57 |
add_option(SettingName::ENABLED, '1', null, true); |
| 58 |
|
| 59 |
// Add all remaining options |
| 60 |
$optionNames = array_diff(SettingName::cases(), [SettingName::ENABLED]); |
| 61 |
foreach ($optionNames as $name) { |
| 62 |
add_option($name, null, null, true); |
| 63 |
} |
| 64 |
|
| 65 |
// Legacy. Update the option introduced in a previous release to use autoloading. |
| 66 |
update_option(SettingName::CUSTOM_DOMAIN, get_option(SettingName::CUSTOM_DOMAIN), true); |
| 67 |
} |
| 68 |
|
| 69 |
protected function deleteOptions(): void |
| 70 |
{ |
| 71 |
foreach (SettingName::cases() as $name) { |
| 72 |
delete_option($name); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
protected function addScripts(bool $collect): void |
| 77 |
{ |
| 78 |
$scripts = new ScriptManager; |
| 79 |
|
| 80 |
if ($collect) { |
| 81 |
$scripts->add(new AnalyticsScript); |
| 82 |
} elseif (is_user_logged_in()) { |
| 83 |
$scripts->add(new InactiveScript); |
| 84 |
} |
| 85 |
|
| 86 |
if (Setting::boolean(SettingName::AUTOMATED_EVENTS)) { |
| 87 |
$scripts->add(new AutomatedEventsScript); |
| 88 |
} |
| 89 |
|
| 90 |
$scripts->register(); |
| 91 |
} |
| 92 |
|
| 93 |
protected function defineAdminPage(): void |
| 94 |
{ |
| 95 |
Page::title('Simple Analytics') |
| 96 |
->slug('simpleanalytics') |
| 97 |
->tab('General', function (Tab $tab) { |
| 98 |
$tab->input(SettingName::CUSTOM_DOMAIN, 'Custom Domain') |
| 99 |
->placeholder('Enter your custom domain or leave it empty.') |
| 100 |
->description('E.g. api.example.com. Leave empty to use the default domain (most users).') |
| 101 |
->docs('https://docs.simpleanalytics.com/bypass-ad-blockers'); |
| 102 |
}) |
| 103 |
->tab('Ignore Rules', function (Tab $tab) { |
| 104 |
$tab->icon(get_icon('eye-slash')); |
| 105 |
|
| 106 |
$tab->input(SettingName::IGNORE_PAGES, 'Ignore Pages') |
| 107 |
->description('Comma separated list of pages to ignore. E.g. /contact, /about') |
| 108 |
->placeholder('Example: /page1, /page2, /category/*') |
| 109 |
->docs('https://docs.simpleanalytics.com/ignore-pages'); |
| 110 |
|
| 111 |
$tab->callout('IP and role exclusion only works when there is no page caching.'); |
| 112 |
|
| 113 |
$tab->multiCheckbox(SettingName::EXCLUDED_ROLES, 'Exclude User Roles') |
| 114 |
->options(function () { |
| 115 |
return wp_roles()->get_names(); |
| 116 |
}); |
| 117 |
|
| 118 |
$tab->ipList(SettingName::EXCLUDED_IP_ADDRESSES, 'Exclude IP Addresses') |
| 119 |
->placeholder("127.0.0.1\n192.168.0.1") |
| 120 |
->description('IP addresses to exclude from tracking.'); |
| 121 |
}) |
| 122 |
->tab('Advanced', function (Tab $tab) { |
| 123 |
$tab->icon(get_icon('cog')); |
| 124 |
|
| 125 |
$tab->checkbox(SettingName::COLLECT_DNT, 'Collect Do Not Track') |
| 126 |
->description('If you want to collect visitors with Do Not Track enabled, turn this on.') |
| 127 |
->docs('https://docs.simpleanalytics.com/dnt'); |
| 128 |
|
| 129 |
$tab->checkbox(SettingName::HASH_MODE, 'Hash mode') |
| 130 |
->description('If your website uses hash (#) navigation, turn this on. On most WordPress websites this is not relevant.') |
| 131 |
->docs('https://docs.simpleanalytics.com/hash-mode'); |
| 132 |
|
| 133 |
$tab->checkbox(SettingName::MANUAL_COLLECT, 'Manually collect page views') |
| 134 |
->description('In case you don’t want to auto collect page views, but via `sa_pageview` function in JavaScript.') |
| 135 |
->docs('https://docs.simpleanalytics.com/trigger-custom-page-views#use-custom-collection-anyway'); |
| 136 |
|
| 137 |
$tab->checkbox(SettingName::NOSCRIPT, 'Support no JavaScript mode') |
| 138 |
->description('Collect analytics from visitors with disabled or no JavaScript.'); |
| 139 |
|
| 140 |
$tab->input(SettingName::ONLOAD_CALLBACK, 'Onload Callback') |
| 141 |
->description('JavaScript function to call when the script is loaded.') |
| 142 |
->placeholder('Example: sa_event("My event")') |
| 143 |
->docs('https://docs.simpleanalytics.com/trigger-custom-page-views#use-custom-collection-anyway'); |
| 144 |
|
| 145 |
$tab->input(SettingName::HOSTNAME, 'Overwrite domain name') |
| 146 |
->description('Override the domain that is sent to Simple Analytics. Useful for multi-domain setups.') |
| 147 |
->placeholder('Example: example.com') |
| 148 |
->docs('https://docs.simpleanalytics.com/overwrite-domain-name'); |
| 149 |
|
| 150 |
$tab->input(SettingName::SA_GLOBAL, 'Global variable name') |
| 151 |
->description('Change the global variable name of Simple Analytics. Default is `sa_event`.') |
| 152 |
->placeholder('Example: ba_event') |
| 153 |
->docs('https://docs.simpleanalytics.com/events#the-variable-sa_event-is-already-used'); |
| 154 |
}) |
| 155 |
->tab('Events', function (Tab $tab) { |
| 156 |
$tab->title('Automated events') |
| 157 |
->icon(get_icon('events')) |
| 158 |
->description("It will track outbound links, email addresses clicks, |
| 159 |
and amount of downloads for common files (pdf, csv, docx, xIsx). |
| 160 |
Events will appear on your events page on simpleanalytics.com"); |
| 161 |
|
| 162 |
$tab->checkbox(SettingName::AUTOMATED_EVENTS, 'Collect automated events'); |
| 163 |
|
| 164 |
$tab->input(SettingName::EVENT_COLLECT_DOWNLOADS, 'Auto collect downloads') |
| 165 |
->placeholder('Example: outbound,emails,downloads') |
| 166 |
->docs('https://docs.simpleanalytics.com/automated-events'); |
| 167 |
|
| 168 |
$tab->input(SettingName::EVENT_EXTENSIONS, 'Download file extensions') |
| 169 |
->description('Comma separated list of file extensions to track as downloads. E.g. pdf, zip') |
| 170 |
->placeholder('Example: pdf, zip') |
| 171 |
->docs('https://docs.simpleanalytics.com/automated-events'); |
| 172 |
|
| 173 |
$tab->checkbox(SettingName::EVENT_USE_TITLE, 'Use titles of page') |
| 174 |
->description('Use the title of the page as the event name. Default is the URL.') |
| 175 |
->docs('https://docs.simpleanalytics.com/automated-events'); |
| 176 |
|
| 177 |
$tab->checkbox(SettingName::EVENT_FULL_URLS, 'Use full URLs') |
| 178 |
->description('Use full URLs instead of the path. Default is the path.') |
| 179 |
->docs('https://docs.simpleanalytics.com/automated-events'); |
| 180 |
|
| 181 |
$tab->input(SettingName::EVENT_SA_GLOBAL, 'Override global') |
| 182 |
->description('Override the global variable name of Simple Analytics. Default is `sa_event`.') |
| 183 |
->placeholder('Example: ba_event') |
| 184 |
->docs('https://docs.simpleanalytics.com/events#the-variable-sa_event-is-already-used'); |
| 185 |
}) |
| 186 |
->register(); |
| 187 |
} |
| 188 |
} |
| 189 |
|