Loading...

Basic A/B Testing of Different Themes

Posted 3 February by admin 0  Comments

Error message

Notice: Undefined offset: 0 in include() (line 32 of /home4/jasonsaf/sites/jsafro.com/docroot/sites/all/themes/md_bigstream/templates/view/view_block/team/views-view-unformatted--md-team--random.tpl.php).
I developed the following code for a client to all them to do A/B testing on two different themes. This was a one-off, short term need so many values are hard-coded. /**
 * Implements hook_custom_theme()
 */
function esb_custom_custom_theme() {
  
  //----[ BEGIN Custom implementation of A/B testing ]-----
  // For sanity, only let this run once per page load
  if( !isset($GLOBALS['ran_ab_testing_theme_picker']) || empty($GLOBALS['ran_ab_testing_theme_picker']) ) :
    $GLOBALS['ran_ab_testing_theme_picker'] = TRUE;
  
    // Try to be sure we have a session
    if( !isset($_SESSION) ) :
      drupal_session_start();
      watchdog( __FUNCTION__, 'Had to drupal_session_start().', array(), WATCHDOG_DEBUG );
    endif;
    
    // If we have a session, let's do some A/B assignment
    if( isset($_SESSION) ) :
      // Assign A or B group
      if( !isset($_SESSION['a_b_group']) ) :
        watchdog( __FUNCTION__, 'Have to assign a group.', array(), WATCHDOG_DEBUG );

        $rand = rand( 1, 4 );
        switch( $rand ) :
          case 1:
            $_SESSION['a_b_group'] = 'a';
          break;
          
          case 2:
          case 3:
          case 4:
          default:
            $_SESSION['a_b_group'] = 'b';
          break;
        endswitch;
      endif;
      
      watchdog( __FUNCTION__, 'User is in group %group.', array('%group' => $_SESSION['a_b_group']), WATCHDOG_DEBUG );
      
      switch( $_SESSION['a_b_group'] ) :
        case 'a':
          $out_theme = 'esb_2016';
        break;
        
        case 'b':
        default:
          $out_theme = 'esb_2014';
        break;
      endswitch;
      
      watchdog( __FUNCTION__, 'Theme set to %theme.', array('%theme' => $out_theme), WATCHDOG_DEBUG );
      return $out_theme;
    else :
      watchdog( __FUNCTION__, 'Failed to find a $_SESSION', array(), WATCHDOG_WARNING );
    endif;
  else :
    // watchdog( __FUNCTION__, 'Blocked theme_picker from running multiple times.', array(), WATCHDOG_DEBUG );
  endif;
  //----[ END Custom implementation of A/B testing ]-----
}
1
Leave a comment