public
Last active

WordPress.com plugin for running Optimizely experiments

  • Download Gist
optimizely.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
<?php
/*
Plugin Name: Optimizely
Description: Runs Optimizely experiments.
Author: koop, evansolomon
Version: 0.2
*/
 
class WPCOM_Optimizely {
public $version;
public $url;
 
private $_checks = array();
 
function __construct( $project_id, $version ) {
$this->version = $version;
 
// Generate project JavaScript URL
$scheme = is_ssl() ? 'https' : 'http';
$this->url = "{$scheme}://cdn.optimizely.com/js/{$project_id}.js";
 
add_filter( 'optimizely_maybe_experiment', array( $this, 'checks' ), 10, 3 );
 
$this->add_check( array( $this, 'disabled_sites' ) );
$this->add_check( array( $this, 'language' ) );
$this->add_check( array( $this, 'front_page_only' ) );
$this->add_check( array( $this, 'logged_out_users' ) );
$this->add_check( array( $this, 'user_id_floor' ) );
}
 
/**
* Potentially run an experiment.
*
* The 'optimizely_maybe_experiment' filter determines whether to run a given experiment.
*
* If any experiment is added, the Optimizely project script will be included.
*
* For each added experiment, the wpcom_optimizely.$EXPERIMENT_SLUG variable will
* be defined in JavaScript.
*/
function maybe_experiment( $experiment ) {
// If the optimizely_maybe_experiment filter returns false, bail.
if ( ! apply_filters( 'optimizely_maybe_experiment', true, $experiment->args, $experiment ) )
return;
 
// Enqueue the Optimizely script
wp_enqueue_script( 'optimizely', $this->url, array('jquery'), $this->version );
 
// Register the slug through JavaScript
wp_localize_script( 'optimizely', 'wpcom_optimizely', array( $experiment->slug => true ) );
}
 
/**
* Provides a shorthand for validating an experiment via callbacks.
* If any check returns 'false' (explicitly), the experiment will not run.
*/
function add_check( $callback, $param = false ) {
$this->_checks[] = $callback;
}
 
/**
* Attached to the optimizely_maybe_experiment filter.
* Runs our checks. If any return 'false', bail!
*/
function checks( $enable_exp, $args, $experiment ) {
foreach ( $this->_checks as $check ) {
if ( false === call_user_func( $check, $args, $experiment ) )
return false;
}
return $enable_exp;
}
 
/**
* Disables all experiments for select sites.
*/
function disabled_sites( $args ) {
global $blog_id;
 
switch ( $blog_id ) {
case 24588526: // TechCrunch
return false;
}
}
 
/**
* Optionally limits targets by user language.
*
* Determined by the boolean 'language' argument. Default 'en', for ENglish.
*/
function language( $args ) {
// If a language isn't set, we always want the experiment to run.
if ( empty( $args['language'] ) )
return true;
 
if ( $args['language'] != get_bloginfo( 'language' ) )
return false;
}
 
/**
* Optionally limits targets only to front page.
*
* Determined by the boolean 'front_page_only' argument. Default false, for all pages.
*/
function front_page_only( $args ) {
if ( $args['front_page_only'] )
return is_home();
}
 
/**
* Optionally limits targets only to logged out users.
*
* Determined by the boolean 'logged_out_users' argument. Default false, for all users.
*/
function logged_out_users( $args ) {
if ( $args['logged_out_users'] )
return ! is_user_logged_in();
}
 
/**
* Optionally limits targets only to new users, defined by minimum user_id.
*
* Determined by the integer 'user_id_floor' argument. Default 0, for all users.
*/
function user_id_floor( $args ) {
if ( $args['user_id_floor'] ) {
global $current_user;
return ( $args['user_id_floor'] < $current_user->ID );
}
}
 
}
 
class WPCOM_Optimizely_Experiment {
public $slug;
public $args;
public $project;
 
/**
* Register a new Optimizely experiment.
*
* @param $slug string
* @param $args array - Optional.
*
* 'actions' => Evaluates the experiment on a specific actions.
* If not provided, experiment evaluates immediately. Default ''.
* Accepts a string or array. Arrays can be used to hook to multiple actions.
* 'language' => Limits an experiment by user language. Default 'en'.
*/
function __construct( $project, $slug, $args = array() ) {
$defaults = array(
'actions' => '',
'language' => 'en',
'front_page_only' => false,
'logged_out_users' => false,
'user_id_floor' => 0,
);
 
$args = wp_parse_args( $args, $defaults );
 
$this->project = $project;
$this->slug = $slug;
$this->args = $args;
 
if ( empty( $args['actions'] ) )
$this->maybe_load();
elseif( is_array( $args['actions'] ) ) {
foreach( $args['actions'] as $action ) {
add_action( $action, array( $this, 'maybe_load' ) );
}
}
else
add_action( $args['actions'], array( $this, 'maybe_load' ) );
}
 
function maybe_load() {
$this->project->maybe_experiment( $this );
}
 
}
 
 
$wpcom_optimizely = new WPCOM_Optimizely( 7144006, '201111130a' );
 
/**
* Add a new Optimizely experiment.
*/
function optimizely_experiment( $slug, $args = array() ) {
global $wpcom_optimizely;
 
new WPCOM_Optimizely_Experiment( $wpcom_optimizely, $slug, $args );
}
 
/**
* Example experiment using multiple actions
*/
optimizely_experiment( 'fake_test', array(
'actions' => array( 'load-themes.php', 'load-post-new.php' ),
) );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.