Skip to content

Instantly share code, notes, and snippets.

@borkweb
Created April 10, 2014 17:15
Show Gist options
  • Save borkweb/10403456 to your computer and use it in GitHub Desktop.
Save borkweb/10403456 to your computer and use it in GitHub Desktop.

I would assume that test data would be localized via wp_localize_script so the test data is fully available on page load. Here's what I'd imagine the enqueues to be:

wp_localize_script( 'go-ab-testing', 'go_ab_testing', array( 'tests' => $variations ) );
wp_enqueue_script( 'go-ab-testing' );
if ( 'undefined' == typeof go_ab_testing ) {
	var go_ab_testing = {};
}//end if

(function( $ ) {

	// set up the event object that'll hold bound functions
	go_ab_testing.event = {};

	// this will hold the user's variations. It should get populated by the cookie and/or go_ab_testing.select_variations.
	go_ab_testing.variations = {};

	/**
	 * handle the bootstrapping of this object
	 */
	go_ab_testing.init = function() {
		// initialize any commonly used dom elements as this.$something
		// bind any events of note. Bound events should have their function declared as go_ab_testing.event.funcname.
		// Ideally, event functions will simply e.preventDefault(); where appropriate and call go_ab_testing functions.
		
		// call any setup functions
		
		this.get_variations();
		this.set_variations();
		this.apply_variations();
		
		// in addition to applying the variations to the page in the above call, we'll need to broadcast
		// the variations out to various other sources. Let's do tha twith a this.variation_notifiy(); call
		this.variation_notify();
	};
	
	/**
	 * sets the testing cookie
	 *
	 * @param object variations (optional) Variations the user will maintain on subsequent page loads
	 */
	go_ab_testing.set_variations = function( variations ) {
		// this should set the cookie to whatever is passed in. If variations is NOT passed in, use what is
		// stored in this.variations.  If there isn't anything of note in there, unset the cookie.
	};
	
	/**
	 * reads the testing cookie for variations
	 */
	go_ab_testing.get_variations = function() {
		// read from cookie into this.variations
		// this should probably run this.clean_variations() regardless of whether or not if found
		// variations in the cookie.
	};
		
	/**
	 * ensures all the appropriate variations exist in this.variations
	 */
	go_ab_testing.clean_variations = function() {
		// this should compare the contents of this.variations with the data that exists in
		// this.test (which comes from wp_localize_script). Missing tests should be selected
		// (via this.select_variation( test ) ), variations that don't exist in this.tests should be removed.
		
		// the specs also talk about removing based on timestamp. Do that too.
	};
	
	/**
	 * select a variation from a test
	 */
	go_ab_testing.select_variation = function( test ) {
		// select a variation from the provided test and return the selected one.
	};
	
	/**
	 * applies variations to the page
	 */
	go_ab_testing.apply_variations = function() {
		// apply variations to the body class in some way
	};
	
	/**
	 * advertises the implementation of a variation on the page
	 */
	go_ab_testing.variation_notify = function() {
		// trigger custom events with the the variation data is probably the right plan, here.
		
		// For bstat, something like:
		// $( document ).trigger( 'bstat/track', data_to_send_to_bstat );
		//
		// This will, of course, require bstat to do something to handle that data, whether it be to
		// collect it into a bstat_t property and pass it along in the new bstat_t.step method that Casey
		// has proposed or if it is to make individual calls.  Casey will know more about what should be
		// done there.
		
		// likewise, we'll need something along those lines for google analytics:
		// $( document ).trigger( 'go-google-analytics/track', data_to_send_to_go_google_analytics );
		// with an appropriate listener in that plugin as well
	};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment