| @@ -7,8 +7,11 @@ | ||
| 7 | 7 | namespace Extendify; |
| 8 | 8 | |
| 9 | 9 | defined('ABSPATH') || die('No direct access.'); |
| 10 | 10 | |
| 11 | +use Extendify\Mcp\Availability; | |
| 12 | +use Extendify\Mcp\Connections; | |
| 13 | +use Extendify\Mcp\Log; | |
| 11 | 14 | use Extendify\Shared\Services\Sanitizer; |
| 12 | 15 | use Extendify\PartnerData; |
| 13 | 16 | |
| 14 | 17 | /** |
| @@ -18,16 +21,31 @@ | ||
| 18 | 21 | |
| 19 | 22 | class Insights |
| 20 | 23 | { |
| 21 | 24 | /** |
| 22 | - * An array of active tests. 'A' should be the control. | |
| 23 | - * For weighted tests, try ['A', 'A', 'A', 'A', 'B'] | |
| 25 | + * Option name storing each site's A/B test assignments, keyed by the | |
| 26 | + * screen/feature under test (e.g. 'AutoLaunch.HideEnhanceAI'). | |
| 24 | 27 | * |
| 25 | - * @var array | |
| 28 | + * @var string | |
| 26 | 29 | */ |
| 27 | - protected $activeTests = []; | |
| 30 | + // phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound | |
| 31 | + const ACTIVE_TESTS_OPTION = 'extendify_active_tests'; | |
| 28 | 32 | |
| 29 | 33 | /** |
| 34 | + * Tests the plugin knows how to run. Each is rolled independently against | |
| 35 | + * its own rollout percentage, which the partner config supplies. | |
| 36 | + * | |
| 37 | + * @var string[] | |
| 38 | + */ | |
| 39 | + // phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound | |
| 40 | + const AVAILABLE_TESTS = [ | |
| 41 | + 'AutoLaunch.HideEnhanceAI', | |
| 42 | + 'AutoLaunch.SubmitCreateWebsite', | |
| 43 | + 'AutoLaunch.DescriptionPlaceholderLaw', | |
| 44 | + 'AutoLaunch.MigrateScreen', | |
| 45 | + ]; | |
| 46 | + | |
| 47 | + /** | |
| 30 | 48 | * Process the readme file to get version and name |
| 31 | 49 | * |
| 32 | 50 | * @return void |
| 33 | 51 | */ |
| @@ -51,30 +69,52 @@ | ||
| 51 | 69 | \spawn_cron(); |
| 52 | 70 | }); |
| 53 | 71 | } |
| 54 | 72 | |
| 55 | - $this->setUpActiveTests(); | |
| 56 | 73 | $this->filterExternalInsights(); |
| 57 | 74 | $this->setupAdminLoginInsights(); |
| 58 | 75 | } |
| 59 | 76 | |
| 60 | 77 | /** |
| 61 | - * Returns the active tests for the user, and sets up tests as needed. | |
| 78 | + * Assign A/B variants for the known tests based on the partner's active | |
| 79 | + * tests. Each active test is rolled once; | |
| 80 | + * inactive tests are dropped. | |
| 62 | 81 | * |
| 82 | + * @param string[] $activeTests Active tests in `Name:Percentage` form | |
| 83 | + * (e.g. 'AutoLaunch.HideEnhanceAI:20'); a bare | |
| 84 | + * name defaults to a 50% rollout. | |
| 63 | 85 | * @return void |
| 64 | 86 | */ |
| 65 | - public function setUpActiveTests() | |
| 87 | + public static function setup(array $activeTests = []) | |
| 66 | 88 | { |
| 67 | - // Make sure that the active tests are set. | |
| 68 | - $currentTests = \get_option('extendify_active_tests', []); | |
| 69 | - $newTests = array_map(function ($test) { | |
| 70 | - // Pick from value randomly. | |
| 71 | - return $test[array_rand($test)]; | |
| 72 | - }, array_diff_key($this->activeTests, $currentTests)); | |
| 73 | - $testsCombined = array_merge($currentTests, $newTests); | |
| 74 | - if ($newTests) { | |
| 75 | - \update_option('extendify_active_tests', Sanitizer::sanitizeArray($testsCombined)); | |
| 89 | + $assignments = \get_option(self::ACTIVE_TESTS_OPTION, []); | |
| 90 | + | |
| 91 | + $percentages = []; | |
| 92 | + foreach ($activeTests as $entry) { | |
| 93 | + list($key, $percentage) = array_pad(explode(':', $entry, 2), 2, null); | |
| 94 | + $percentages[$key] = is_numeric($percentage) ? (float) $percentage : 50.0; | |
| 76 | 95 | } |
| 96 | + | |
| 97 | + foreach (self::AVAILABLE_TESTS as $key) { | |
| 98 | + if (!array_key_exists($key, $percentages)) { | |
| 99 | + unset($assignments[$key]); | |
| 100 | + continue; | |
| 101 | + } | |
| 102 | + | |
| 103 | + // Roll once so the site keeps the same variant. | |
| 104 | + if (!isset($assignments[$key])) { | |
| 105 | + $assignments[$key] = [ | |
| 106 | + // The percentage is variant B's rollout share: 50 -> 50% A / 50% B, | |
| 107 | + // 20 -> 80% A / 20% B. | |
| 108 | + 'variant' => random_int(1, 10000) <= $percentages[$key] * 100 ? 'B' : 'A', | |
| 109 | + 'percentage' => $percentages[$key], | |
| 110 | + // ISO 8601 (UTC) | |
| 111 | + 'assignedAt' => gmdate('c'), | |
| 112 | + ]; | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + \update_option(self::ACTIVE_TESTS_OPTION, Sanitizer::sanitizeArray($assignments)); | |
| 77 | 117 | } |
| 78 | 118 | |
| 79 | 119 | /** |
| 80 | 120 | * Add additional data to the opt-in insights |
| @@ -112,8 +152,13 @@ | ||
| 112 | 152 | 'lastUpdatedPost' => $this->getLastUpdatedPost('post'), |
| 113 | 153 | 'lastLoginAdmin' => $this->getLastAdminLogin(), |
| 114 | 154 | 'hasImprint' => $this->hasImprint(), |
| 115 | 155 | ]); |
| 156 | + | |
| 157 | + if (Availability::offered()) { | |
| 158 | + $insights['mcp'] = ['usage' => Log::usage(), 'connections' => Connections::count()]; | |
| 159 | + } | |
| 160 | + | |
| 116 | 161 | return $insights; |
| 117 | 162 | }); |
| 118 | 163 | } |
| 119 | 164 | |