Skip to content

Instantly share code, notes, and snippets.

@bboyle
Created July 8, 2014 05:02
Show Gist options
  • Save bboyle/2de854350aea5af6b97f to your computer and use it in GitHub Desktop.
Save bboyle/2de854350aea5af6b97f to your computer and use it in GitHub Desktop.
Quick and dirty AB testing
(function( $ ) {
'use strict';
// split traffic between this page and a B-variant
function ABredirect( path, weight, variationFunction ) {
path = path || '/index.html';
weight = weight || 0.5;
// is this the B-variant?
if ( window.location.pathname.indexOf( path ) === -1 ) {
// A variant: redirect some traffic to B variant
if ( Math.random() < weight ) {
window.location.href = path;
}
} else {
// B variant: run function
if ( typeof variationFunction === 'function' ) {
// run onready
$( variationFunction );
}
}
}
// redirect
ABredirect( '/homepage/index.html', 0.5, function() {
$( '.content-row.announcements', '#content' )
.insertBefore( '#content .content-row.franchises' )
.addClass( 'featured-news' );
});
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment