Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added


Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* @package automattic/jetpack-mu-wpcom
*/

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection;
use Automattic\Jetpack\Jetpack_Mu_Wpcom;
use Automattic\Jetpack\Status\Host;

/**
* Add the Admin Interface Style setting on the General settings page.
Expand Down Expand Up @@ -273,3 +276,68 @@ function wpcom_show_admin_interface_notice() {
);
}
add_action( 'admin_notices', 'wpcom_show_admin_interface_notice' );

/**
* Check if the duplicate views experiment is enabled.
*
* @return boolean
*/
function wpcom_is_duplicate_views_experiment_enabled() {
// TODO: We don't know yet the experiment name.
$experiment_platform = 'calypso';
$experiment_name = "{$experiment_platform}_duplicate_views_placeholder";

static $is_enabled = null;
if ( $is_enabled !== null ) {
return $is_enabled;
}

if ( ( new Host() )->is_wpcom_simple() ) {
$is_enabled = 'treatment' === \ExPlat\assign_current_user( $experiment_name );
return $is_enabled;
}

$option_name = 'duplicate_views_experiment_assignment';
$variation = get_user_option( $option_name, get_current_user_id() );

if ( false !== $variation ) {
$is_enabled = 'treatment' === $variation;
return $is_enabled;
}

if ( ! ( new Jetpack_Connection() )->is_user_connected() ) {
$is_enabled = false;
return $is_enabled;
}

$request_path = add_query_arg(
array( 'experiment_name' => $experiment_name ),
"/experiments/0.1.0/assignments/{$experiment_platform}"
);
$response = Client::wpcom_json_api_request_as_user( $request_path, 'v2' );

if ( is_wp_error( $response ) ) {
$is_enabled = false;
return $is_enabled;
}

$response_code = wp_remote_retrieve_response_code( $response );

if ( 200 !== $response_code ) {
$is_enabled = false;
return $is_enabled;
}

$data = json_decode( wp_remote_retrieve_body( $response ), true );

if ( isset( $data['variations'] ) && isset( $data['variations'][ $experiment_name ] ) ) {
$variation = $data['variations'][ $experiment_name ];
update_user_option( get_current_user_id(), $option_name, $variation, true );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The endpoint will return null for the default experience, but it will also return null if the experiment is in staging. This means we will be caching the null experience for any existing users that have triggered this code, so it will skip the actual assignment, meaning we will only be testing users that haven't triggered this code before we started the experiment. Since this code has been in place for a while that might roughly mean existing users in general, and so we are only testing new or newly active users.

By caching here it also means the ExPlat experiment controls to disable an experiment won't work correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we identified this issue recently (p1736776189483689-slack-C04DZ8M0GHW, Automattic/wp-calypso#98341) and prepared a quick fix to bust the cached assignment 😄 (#41093)


$is_enabled = 'treatment' === $variation;
return $is_enabled;
} else {
$is_enabled = false;
return $is_enabled;
}
}
Loading