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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / react-admin / class-experimental-abtest.php
woocommerce / includes / react-admin Last commit date
emails 1 year ago class-experimental-abtest.php 1 year ago connect-existing-pages.php 4 months ago core-functions.php 3 years ago feature-config.php 4 weeks ago page-controller-functions.php 4 years ago wc-admin-update-functions.php 4 months ago
class-experimental-abtest.php
260 lines
1 <?php
2 /**
3 * NOTE: this is a temporary class and can be replaced by jetpack-abtest after
4 * https://github.com/Automattic/jetpack/issues/19596 has been fixed.
5 *
6 * A class that interacts with Explat A/B tests.
7 *
8 * This class is experimental. It is a fork of the jetpack-abtest package and
9 * updated for use with ExPlat. These changes are planned to be contributed
10 * back to the upstream Jetpack package. If accepted, this class should then
11 * be superseded by the Jetpack class using Composer.
12 *
13 * This class should not be used externally.
14 *
15 * @package WooCommerce\Admin
16 * @link https://packagist.org/packages/automattic/jetpack-abtest
17 */
18
19 namespace WooCommerce\Admin;
20
21 use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
22 use Automattic\Jetpack\Connection\Client as Jetpack_Connection_client;
23 use Automattic\WooCommerce\Admin\WCAdminHelper;
24
25 /**
26 * This class provides an interface to the Explat A/B tests.
27 *
28 * Usage:
29 *
30 * $anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : '';
31 * $allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
32 * $abtest = new \WooCommerce\Admin\Experimental_Abtest(
33 * $anon_id,
34 * 'woocommerce',
35 * $allow_tracking
36 * );
37 *
38 * OR use the helper function:
39 *
40 * WooCommerce\Admin\Experimental_Abtest::in_treatment('experiment_name');
41 *
42 *
43 * $isTreatment = $abtest->get_variation('your-experiment-name') === 'treatment';
44 *
45 * @internal This class is experimental and should not be used externally due to planned breaking changes.
46 */
47 final class Experimental_Abtest {
48
49 /**
50 * A variable to hold the tests we fetched, and their variations for the current user.
51 *
52 * @var array
53 */
54 private $tests = array();
55
56 /**
57 * ExPlat Anonymous ID.
58 *
59 * @var string
60 */
61 private $anon_id = null;
62
63 /**
64 * ExPlat Platform name.
65 *
66 * @var string
67 */
68 private $platform = 'woocommerce';
69
70 /**
71 * Whether trcking consent is given.
72 *
73 * @var bool
74 */
75 private $consent = false;
76
77 /**
78 * Request variation as a auth wpcom user or not.
79 *
80 * @var boolean
81 */
82 private $as_auth_wpcom_user = false;
83
84 /**
85 * Constructor.
86 *
87 * @param string $anon_id ExPlat anonymous ID.
88 * @param string $platform ExPlat platform name.
89 * @param bool $consent Whether tracking consent is given.
90 * @param bool $as_auth_wpcom_user Request variation as a auth wp user or not.
91 */
92 public function __construct( string $anon_id, string $platform, bool $consent, bool $as_auth_wpcom_user = false ) {
93 $this->anon_id = $anon_id;
94 $this->platform = $platform;
95 $this->consent = $consent;
96 $this->as_auth_wpcom_user = $as_auth_wpcom_user;
97 }
98
99 /**
100 * Returns true if the current user is in the treatment group of the given experiment.
101 *
102 * @param string $experiment_name Name of the experiment.
103 * @param bool $as_auth_wpcom_user Request variation as a auth wp user or not.
104 *
105 * @return bool True if the user is in the treatment group, false otherwise.
106 * @throws \Exception If there is an error retrieving the variation.
107 */
108 public static function in_treatment( string $experiment_name, bool $as_auth_wpcom_user = false ) {
109 $anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : '';
110 $allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
111 $abtest = new self(
112 $anon_id,
113 'woocommerce',
114 $allow_tracking,
115 $as_auth_wpcom_user
116 );
117
118 return $abtest->get_variation( $experiment_name ) === 'treatment';
119 }
120
121 /**
122 * Retrieve the test variation for a provided A/B test.
123 *
124 * @param string $test_name Name of the A/B test.
125 * @return mixed|null A/B test variation, or null on failure.
126 * @throws \Exception If there is an error retrieving the variation and the environment is not production.
127 */
128 public function get_variation( $test_name ) {
129 // Default to the control variation when users haven't consented to tracking.
130 if ( ! $this->consent ) {
131 return 'control';
132 }
133
134 $variation = $this->fetch_variation( $test_name );
135
136 // If there was an error retrieving a variation, conceal the error for the consumer.
137 // If there was an error retrieving a variation, throw an exception in non-production environments.
138 if ( is_wp_error( $variation ) ) {
139 if ( 'production' !== wp_get_environment_type() ) {
140 throw new \Exception( $variation->get_error_message() );
141 }
142 return 'control';
143 }
144
145 return $variation;
146 }
147
148
149 /**
150 * Perform the request for a experiment assignment of a provided A/B test from WP.com.
151 *
152 * @param array $args Arguments to pass to the request for A/B test.
153 * @return array|\WP_Error A/B test variation error on failure.
154 */
155 public function request_assignment( $args ) {
156 // Request as authenticated wp user.
157 if ( $this->as_auth_wpcom_user && class_exists( Jetpack_Connection_Manager::class ) ) {
158 $jetpack_connection_manager = new Jetpack_Connection_Manager();
159 if ( $jetpack_connection_manager->is_user_connected() ) {
160 $response = Jetpack_Connection_client::wpcom_json_api_request_as_user(
161 '/experiments/0.1.0/assignments/' . $this->platform,
162 '2',
163 $args
164 );
165 }
166 }
167
168 // Request as anonymous user.
169 if ( ! isset( $response ) ) {
170 if ( ! isset( $args['anon_id'] ) || empty( $args['anon_id'] ) ) {
171 return new \WP_Error( 'invalid_anon_id', 'anon_id must be an none empty string.' );
172 }
173
174 $url = add_query_arg(
175 $args,
176 sprintf(
177 'https://public-api.wordpress.com/wpcom/v2/experiments/0.1.0/assignments/%s',
178 $this->platform
179 )
180 );
181 $response = wp_remote_get( $url );
182 }
183
184 return $response;
185 }
186
187 /**
188 * Fetch and cache the test variation for a provided A/B test from WP.com.
189 *
190 * ExPlat returns a null value when the assigned variation is control or
191 * an assignment has not been set. In these instances, this method returns
192 * a value of "control".
193 *
194 * @param string $test_name Name of the A/B test.
195 * @return array|\WP_Error A/B test variation, or error on failure.
196 */
197 protected function fetch_variation( $test_name ) {
198 // Make sure test name exists.
199 if ( ! $test_name ) {
200 return new \WP_Error( 'test_name_not_provided', 'A/B test name has not been provided.' );
201 }
202
203 // Make sure test name is a valid one.
204 if ( ! preg_match( '/^[a-z0-9_]+$/', $test_name ) ) {
205 return new \WP_Error( 'invalid_test_name', 'Invalid A/B test name.' );
206 }
207
208 // Return internal-cached test variations.
209 if ( isset( $this->tests[ $test_name ] ) ) {
210 return $this->tests[ $test_name ];
211 }
212
213 // Return external-cached test variations.
214 if ( ! empty( get_transient( 'abtest_variation_' . $test_name ) ) ) {
215 return get_transient( 'abtest_variation_' . $test_name );
216 }
217
218 // Make the request to the WP.com API.
219 $args = array(
220 'experiment_name' => $test_name,
221 'anon_id' => rawurlencode( $this->anon_id ),
222 'woo_country_code' => rawurlencode( get_option( 'woocommerce_default_country', 'US:CA' ) ),
223 'woo_wcadmin_install_timestamp' => rawurlencode( get_option( WCAdminHelper::WC_ADMIN_TIMESTAMP_OPTION ) ),
224 );
225
226 /**
227 * Get additional request args.
228 *
229 * @since 6.5.0
230 */
231 $args = apply_filters( 'woocommerce_explat_request_args', $args );
232 $response = $this->request_assignment( $args );
233
234 // Bail if there was an error or malformed response.
235 if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
236 return new \WP_Error( 'failed_to_fetch_data', 'Unable to fetch the requested data.' );
237 }
238
239 // Decode the results.
240 $results = json_decode( $response['body'], true );
241
242 // Bail if there were no resultsreturned.
243 if ( ! is_array( $results ) ) {
244 return new \WP_Error( 'unexpected_data_format', 'Data was not returned in the expected format.' );
245 }
246
247 // Store the variation in our internal cache.
248 $this->tests[ $test_name ] = $results['variations'][ $test_name ] ?? null;
249
250 $variation = $results['variations'][ $test_name ] ?? 'control';
251 // Store the variation in our external cache.
252 if ( ! empty( $results['ttl'] ) ) {
253 set_transient( 'abtest_variation_' . $test_name, $variation, $results['ttl'] );
254 }
255
256 return $variation;
257 }
258 }
259
260